-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli_windows.go
61 lines (50 loc) · 1.11 KB
/
cli_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
51
52
53
54
55
56
57
58
59
60
61
// +build windows
package main
import (
"errors"
"os"
"os/exec"
)
var supportedCLIs = map[string]string{
// "bash": "wsl.exe",
"powershell": "powershell.exe",
"cmd": "cmd.exe",
}
func new(c string) (*cli, error) {
if ci, ok := supportedCLIs[c]; ok {
ps, err := exec.LookPath(ci)
if err != nil {
return nil, err
}
return &cli{
shell: ps,
cli: c,
}, nil
}
return nil, errors.New("unsupported cli")
}
func (p *cli) execute(env map[string]string, cmds ...string) (exitCode int, err error) {
if p.cli == "powershell" {
cmds = append([]string{"-NoProfile", "-NonInteractive"}, cmds...)
}
if p.cli == "cmd" {
cmds = append([]string{"/C"}, cmds...)
}
if p.cli == "bash" {
cmds = append([]string{"bash", "-c"}, cmds...)
}
cmd := exec.Command(p.shell, cmds...)
cmd.Env = os.Environ()
for name, value := range env {
cmd.Env = append(cmd.Env, name+"="+value)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
return exitError.ExitCode(), err
}
}
return 0, nil
}