-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.go
100 lines (89 loc) · 2.3 KB
/
run.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package goclitools
import (
"bytes"
"log"
"os"
"os/exec"
"github.com/urfave/cli"
)
// RunInDir ...
func RunInDir(cmd, dir string) ([]byte, error) {
if os.Getenv("DEBUG") != "" {
log.Println(cmd)
}
command := exec.Command("sh", "-c", "set -o pipefail && "+cmd)
command.Dir = dir
output, err := command.CombinedOutput()
if err != nil {
return output, cli.NewExitError(err, 1)
}
return output, nil
}
// Run ...
func Run(cmd string) ([]byte, error) {
return RunInDir(cmd, ".")
}
// RunInteractiveInDir ...
func RunInteractiveInDir(cmd, dir string) error {
if os.Getenv("DEBUG") != "" {
log.Println(cmd)
}
command := exec.Command("sh", "-c", "set -o pipefail && "+cmd)
command.Stdout = os.Stdout
command.Stdin = os.Stdin
command.Stderr = os.Stderr
command.Dir = dir
err := command.Run()
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
// RunWithInput ...
func RunWithInput(cmd string, input []byte) ([]byte, error) {
command := exec.Command("sh", "-c", cmd)
command.Stdin = bytes.NewReader(input)
data, err := command.Output()
if err != nil {
return data, cli.NewExitError(err.Error(), 1)
}
return data, nil
}
// RunInteractive ...
func RunInteractive(cmd string) error {
return RunInteractiveInDir(cmd, "")
}
// RunSecureInDir ...
func RunSecureInDir(cmd, dir string, secrets []string) ([]byte, error) {
if os.Getenv("DEBUG") != "" {
log.Println(SecureString(cmd, secrets))
}
command := exec.Command("sh", "-c", "set -o pipefail && "+cmd)
command.Dir = dir
out, err := command.CombinedOutput()
return SecureByteArray(out, secrets), err
}
// RunSecure ...
func RunSecure(cmd string, secrets []string) ([]byte, error) {
return RunSecureInDir(cmd, "", secrets)
}
// RunSecureInteractiveInDir ...
func RunSecureInteractiveInDir(cmd, dir string, secrets []string) error {
if os.Getenv("DEBUG") != "" {
log.Println(SecureString(cmd, secrets))
}
command := exec.Command("sh", "-c", "set -o pipefail && "+cmd)
command.Stdout = SecureStd(os.Stdout, secrets)
command.Stdin = os.Stdin
command.Stderr = SecureStd(os.Stderr, secrets)
command.Dir = dir
err := command.Run()
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
// RunSecureInteractive ...
func RunSecureInteractive(cmd string, secrets []string) error {
return RunSecureInteractiveInDir(cmd, "", secrets)
}