-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutil_windows.go
50 lines (45 loc) · 1.12 KB
/
util_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//go:build windows
// +build windows
package main
import (
"os/exec"
"strconv"
"strings"
)
// stop stops the command and all its child processes.
func stop(cmd *exec.Cmd) {
// https://stackoverflow.com/a/44551450
killCmd := exec.Command("taskkill.exe", "/t", "/f", "/pid", strconv.Itoa(cmd.Process.Pid))
_ = killCmd.Run()
}
// setpgid is a no-op on windows.
func setpgid(cmd *exec.Cmd) {}
// joinArgs joins the arguments of the command into a string which can then be
// passed to `exec.Command("pwsh.exe", "-command", $STRING)`. Examples:
//
// ["echo", "foo"] => echo foo
//
// ["echo", "hello goodbye"] => echo 'hello goodbye'
func joinArgs(args []string) string {
// references:
// https://www.rlmueller.net/PowerShellEscape.htm
// https://stackoverflow.com/a/11231504
var b strings.Builder
for i, arg := range args {
if i == 0 {
b.WriteString(arg)
continue
}
b.WriteString(" ")
if arg == "" {
b.WriteString("''")
continue
}
if !strings.ContainsAny(arg, " '`$(){}<>|&;*") {
b.WriteString(arg)
continue
}
b.WriteString("'" + strings.ReplaceAll(arg, "'", "''") + "'")
}
return b.String()
}