-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
165 lines (131 loc) · 3.05 KB
/
utils.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package gitdir
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
"github.com/gliderlabs/ssh"
"github.com/rs/zerolog"
)
type multiError struct {
errors []error
}
func newMultiError(errors ...error) error {
var ret multiError
for _, err := range errors {
if err != nil {
ret.errors = append(ret.errors, err)
}
}
if len(ret.errors) == 0 {
return nil
}
return &ret
}
func (ce *multiError) Error() string {
buf := bytes.NewBuffer(nil)
for _, err := range ce.errors {
buf.WriteString("- ")
buf.WriteString(err.Error())
buf.WriteString("\n")
}
return buf.String()
}
func listContainsStr(list []string, target string) bool {
for _, val := range list {
if val == target {
return true
}
}
return false
}
func handlePanic(logger *zerolog.Logger) {
if r := recover(); r != nil {
logger.Error().Err(fmt.Errorf("%s", r)).Msg("Caught panic")
}
}
func writeStringFmt(w io.Writer, format string, args ...interface{}) error {
_, err := io.WriteString(w, fmt.Sprintf(format, args...))
return err
}
func getExitStatusFromError(err error) int {
if err == nil {
return 0
}
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
return 1
}
return exitErr.ProcessState.ExitCode()
}
func sanitize(in string) string {
// TODO: this should do more
return strings.ToLower(in)
}
// TODO: see if this can be cleaned up.
func runCommand( //nolint:funlen
log *zerolog.Logger,
cwd string,
session ssh.Session,
args []string,
environ []string,
) int {
// NOTE: we are explicitly ignoring gosec here because we *only* pass in
// known commands here.
cmd := exec.Command(args[0], args[1:]...) //nolint:gosec
cmd.Dir = cwd
cmd.Env = append(cmd.Env, environ...)
cmd.Env = append(cmd.Env, "PATH="+os.Getenv("PATH"))
stdin, err := cmd.StdinPipe()
if err != nil {
log.Error().Err(err).Msg("Failed to get stdin pipe")
return 1
}
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Error().Err(err).Msg("Failed to get stdout pipe")
return 1
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Error().Err(err).Msg("Failed to get stderr pipe")
return 1
}
wg := &sync.WaitGroup{}
wg.Add(2)
if err = cmd.Start(); err != nil {
log.Error().Err(err).Msg("Failed to start command")
return 1
}
go func() {
defer stdin.Close()
if _, stdinErr := io.Copy(stdin, session); stdinErr != nil {
log.Error().Err(err).Msg("Failed to write session to stdin")
}
}()
go func() {
defer wg.Done()
if _, stdoutErr := io.Copy(session, stdout); stdoutErr != nil {
log.Error().Err(err).Msg("Failed to write stdout to session")
}
}()
go func() {
defer wg.Done()
if _, stderrErr := io.Copy(session.Stderr(), stderr); stderrErr != nil {
log.Error().Err(err).Msg("Failed to write stderr to session")
}
}()
// Ensure all the output has been written before we wait on the command to
// exit.
wg.Wait()
// Wait for the command to exit and log any errors we get
err = cmd.Wait()
if err != nil {
log.Error().Err(err).Msg("Failed to wait for command exit")
}
return getExitStatusFromError(err)
}