2016-10-24 00:44:56 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2017-10-15 09:32:59 +00:00
|
|
|
"fmt"
|
2016-10-24 00:44:56 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-10-22 16:09:47 +00:00
|
|
|
"strings"
|
2021-10-25 09:46:59 +00:00
|
|
|
"sync/atomic"
|
2017-01-07 16:30:31 +00:00
|
|
|
"syscall"
|
2016-10-24 00:44:56 +00:00
|
|
|
)
|
|
|
|
|
2021-10-25 09:46:59 +00:00
|
|
|
var shellPath atomic.Value
|
|
|
|
|
2021-10-22 16:09:47 +00:00
|
|
|
// ExecCommand executes the given command with $SHELL
|
2018-09-27 06:27:08 +00:00
|
|
|
func ExecCommand(command string, setpgid bool) *exec.Cmd {
|
2021-10-25 09:46:59 +00:00
|
|
|
var shell string
|
|
|
|
if cached := shellPath.Load(); cached != nil {
|
|
|
|
shell = cached.(string)
|
|
|
|
} else {
|
|
|
|
shell = os.Getenv("SHELL")
|
|
|
|
if len(shell) == 0 {
|
|
|
|
shell = "cmd"
|
|
|
|
} else if strings.Contains(shell, "/") {
|
|
|
|
out, err := exec.Command("cygpath", "-w", shell).Output()
|
|
|
|
if err == nil {
|
|
|
|
shell = strings.Trim(string(out), "\n")
|
|
|
|
}
|
2021-10-22 16:09:47 +00:00
|
|
|
}
|
2021-10-25 09:46:59 +00:00
|
|
|
shellPath.Store(shell)
|
2021-10-22 16:09:47 +00:00
|
|
|
}
|
|
|
|
return ExecCommandWith(shell, command, setpgid)
|
2017-09-28 14:05:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 16:09:47 +00:00
|
|
|
// ExecCommandWith executes the given command with the specified shell
|
2018-09-27 06:27:08 +00:00
|
|
|
// FIXME: setpgid is unused. We set it in the Unix implementation so that we
|
|
|
|
// can kill preview process with its child processes at once.
|
2021-10-22 16:09:47 +00:00
|
|
|
// NOTE: For "powershell", we should ideally set output encoding to UTF8,
|
|
|
|
// but it is left as is now because no adverse effect has been observed.
|
|
|
|
func ExecCommandWith(shell string, command string, setpgid bool) *exec.Cmd {
|
2021-10-27 07:50:40 +00:00
|
|
|
var cmd *exec.Cmd
|
2021-10-22 16:09:47 +00:00
|
|
|
if strings.Contains(shell, "cmd") {
|
2021-10-27 07:50:40 +00:00
|
|
|
cmd = exec.Command(shell)
|
2021-10-22 16:09:47 +00:00
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
HideWindow: false,
|
2021-10-27 07:50:40 +00:00
|
|
|
CmdLine: fmt.Sprintf(` /v:on/s/c "%s"`, command),
|
2021-10-22 16:09:47 +00:00
|
|
|
CreationFlags: 0,
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
2021-10-27 07:50:40 +00:00
|
|
|
|
|
|
|
if strings.Contains(shell, "pwsh") || strings.Contains(shell, "powershell") {
|
|
|
|
cmd = exec.Command(shell, "-NoProfile", "-Command", command)
|
|
|
|
} else {
|
|
|
|
cmd = exec.Command(shell, "-c", command)
|
|
|
|
}
|
2017-10-02 15:36:19 +00:00
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
2017-10-15 09:32:59 +00:00
|
|
|
HideWindow: false,
|
|
|
|
CreationFlags: 0,
|
2017-10-02 15:36:19 +00:00
|
|
|
}
|
|
|
|
return cmd
|
2016-11-06 17:15:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 06:27:08 +00:00
|
|
|
// KillCommand kills the process for the given command
|
|
|
|
func KillCommand(cmd *exec.Cmd) error {
|
|
|
|
return cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
|
2016-11-06 17:15:34 +00:00
|
|
|
// IsWindows returns true on Windows
|
|
|
|
func IsWindows() bool {
|
|
|
|
return true
|
2016-10-24 00:44:56 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
|
2018-09-27 06:27:08 +00:00
|
|
|
// SetNonblock executes syscall.SetNonblock on file descriptor
|
2017-01-07 16:30:31 +00:00
|
|
|
func SetNonblock(file *os.File, nonblock bool) {
|
|
|
|
syscall.SetNonblock(syscall.Handle(file.Fd()), nonblock)
|
|
|
|
}
|
2017-05-24 16:36:59 +00:00
|
|
|
|
|
|
|
// Read executes syscall.Read on file descriptor
|
|
|
|
func Read(fd int, b []byte) (int, error) {
|
|
|
|
return syscall.Read(syscall.Handle(fd), b)
|
|
|
|
}
|