-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
234 lines (188 loc) · 10.5 KB
/
command.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package foundation
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"github.com/logrusorgru/aurora"
"github.com/rs/zerolog/log"
)
// HandleError logs a fatal when the error is not nil
func HandleError(err error) {
if err != nil {
log.Fatal().Err(err).Msg("Fatal error")
}
}
// RunCommand runs a full command string and replaces placeholders with the arguments; it logs a fatal on error
// RunCommand(ctx, "kubectl logs -l app=%v -n %v", app, namespace)
func RunCommand(ctx context.Context, command string, args ...interface{}) {
c, a := getSeparateCommandAndArgs(ctx, command, args)
RunCommandWithArgs(ctx, c, a)
}
// RunCommandExtended runs a full command string and replaces placeholders with the arguments; it returns an error if command execution failed
// err := RunCommandExtended(ctx, "kubectl logs -l app=%v -n %v", app, namespace)
func RunCommandExtended(ctx context.Context, command string, args ...interface{}) error {
c, a := getSeparateCommandAndArgs(ctx, command, args)
return RunCommandWithArgsExtended(ctx, c, a)
}
// RunCommandExtended runs a full command string and replaces placeholders with the arguments; it returns an error combined stderr if command execution failed
// err := RunCommandExtended(ctx, "kubectl logs -l app=%v -n %v", app, namespace)
func RunCommandExtendedCombinedStdErr(ctx context.Context, command string, args ...interface{}) error {
c, a := getSeparateCommandAndArgs(ctx, command, args)
return RunCommandWithArgsExtendedCombinedStdErr(ctx, c, a)
}
// GetCommandOutput runs a full command string and replaces placeholders with the arguments; it returns the output as a string and an error if command execution failed
// output, err := GetCommandOutput(ctx, "kubectl logs -l app=%v -n %v", app, namespace)
func GetCommandOutput(ctx context.Context, command string, args ...interface{}) (string, error) {
c, a := getSeparateCommandAndArgs(ctx, command, args)
return GetCommandWithArgsOutput(ctx, c, a)
}
// RunCommandWithArgs runs a single command and passes the arguments; it logs a fatal on error
// RunCommandWithArgs(ctx, "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func RunCommandWithArgs(ctx context.Context, command string, args []string) {
err := RunCommandWithArgsExtended(ctx, command, args)
HandleError(err)
}
// RunCommandWithArgsExtended runs a single command and passes the arguments; it returns an error if command execution failed
// err := RunCommandWithArgsExtended(ctx, "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func RunCommandWithArgsExtended(ctx context.Context, command string, args []string) error {
log.Debug().Msg(aurora.Sprintf(aurora.Gray(18, "> %v %v"), command, strings.Join(args, " ")))
cmd := exec.CommandContext(ctx, command, args...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
}
// RunCommandWithArgsExtendedCombinedStdErr runs a single command and passes the arguments; it returns an error combined stderr if command execution failed
// err := RunCommandWithArgsExtended(ctx, "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func RunCommandWithArgsExtendedCombinedStdErr(ctx context.Context, command string, args []string) error {
log.Debug().Msg(aurora.Sprintf(aurora.Gray(18, "> %v %v"), command, strings.Join(args, " ")))
cmd := exec.CommandContext(ctx, command, args...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("%s: %s", err, stderr.String())
}
return nil
}
// GetCommandWithArgsOutput runs a single command and passes the arguments; it returns the output as a string and an error if command execution failed
// output, err := GetCommandWithArgsOutput(ctx, "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func GetCommandWithArgsOutput(ctx context.Context, command string, args []string) (string, error) {
log.Debug().Msg(aurora.Sprintf(aurora.Gray(18, "> %v %v"), command, strings.Join(args, " ")))
cmd := exec.CommandContext(ctx, command, args...)
cmd.Env = os.Environ()
output, err := cmd.CombinedOutput()
return string(output), err
}
// RunCommandInDirectory runs a full command string and replaces placeholders with the arguments from the specified directory; it logs a fatal on error
// RunCommandInDirectory(ctx, "directory other than working dir", "kubectl logs -l app=%v -n %v", app, namespace)
func RunCommandInDirectory(ctx context.Context, dir string, command string, args ...interface{}) {
c, a := getSeparateCommandAndArgs(ctx, command, args)
RunCommandInDirectoryWithArgs(ctx, dir, c, a)
}
// RunCommandInDirectoryExtended runs a full command string and replaces placeholders with the arguments from the specified directory; it returns an error if command execution failed
// err := RunCommandInDirectoryExtended(ctx, "directory other than working dir", "kubectl logs -l app=%v -n %v", app, namespace)
func RunCommandInDirectoryExtended(ctx context.Context, dir string, command string, args ...interface{}) error {
c, a := getSeparateCommandAndArgs(ctx, command, args)
return RunCommandInDirectoryWithArgsExtended(ctx, dir, c, a)
}
// RunCommandInDirectoryWithArgs runs a single command and passes the arguments from the specified directory; it logs a fatal on error
// RunCommandInDirectoryWithArgs(ctx, "directory other than working dir", "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func RunCommandInDirectoryWithArgs(ctx context.Context, dir string, command string, args []string) {
err := RunCommandInDirectoryWithArgsExtended(ctx, dir, command, args)
HandleError(err)
}
// RunCommandInDirectoryWithArgsExtended runs a single command and passes the arguments from the specified directory; it returns an error if command execution failed
// err := RunCommandInDirectoryWithArgsExtended(ctx, "directory other than working dir", "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func RunCommandInDirectoryWithArgsExtended(ctx context.Context, dir string, command string, args []string) error {
log.Debug().Msg(aurora.Sprintf(aurora.Gray(18, "[%v] > %v %v"), dir, command, strings.Join(args, " ")))
cmd := exec.CommandContext(ctx, command, args...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
err := cmd.Run()
return err
}
// RunCommandInDirectoryExtended runs a full command string and replaces placeholders with the arguments from the specified directory; it returns an error combined stderr if command execution failed
// err := RunCommandInDirectoryExtended(ctx, "directory other than working dir", "kubectl logs -l app=%v -n %v", app, namespace)
func RunCommandInDirectoryExtendedCombinedStdErr(ctx context.Context, dir string, command string, args ...interface{}) error {
c, a := getSeparateCommandAndArgs(ctx, command, args)
return RunCommandInDirectoryWithArgsExtendedCombinedStdErr(ctx, dir, c, a)
}
// RunCommandInDirectoryWithArgsExtended runs a single command and passes the arguments from the specified directory; it returns an error combined stderr if command execution failed
// err := RunCommandInDirectoryWithArgsExtended(ctx, "directory other than working dir", "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func RunCommandInDirectoryWithArgsExtendedCombinedStdErr(ctx context.Context, dir string, command string, args []string) error {
log.Debug().Msg(aurora.Sprintf(aurora.Gray(18, "[%v] > %v %v"), dir, command, strings.Join(args, " ")))
cmd := exec.CommandContext(ctx, command, args...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
var stderr bytes.Buffer
cmd.Stderr = &stderr
cmd.Dir = dir
err := cmd.Run()
if err != nil {
return fmt.Errorf("%s: %s", err, stderr.String())
}
return nil
}
// GetCommandInDirectoryOutput runs a full command string and replaces placeholders with the arguments from the specified directory; it returns the output as a string and an error if command execution failed
// output, err := GetCommandInDirectoryOutput(ctx, "directory other than working dir", "kubectl logs -l app=%v -n %v", app, namespace)
func GetCommandInDirectoryOutput(ctx context.Context, dir string, command string, args ...interface{}) (string, error) {
c, a := getSeparateCommandAndArgs(ctx, command, args)
return GetCommandWithArgsInDirectoryOutput(ctx, dir, c, a)
}
// GetCommandWithArgsInDirectoryOutput runs a single command and passes the arguments from the specified directory; it returns the output as a string and an error if command execution failed
// output, err := GetCommandWithArgsOutput(ctx, "directory other than working dir", "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func GetCommandWithArgsInDirectoryOutput(ctx context.Context, dir string, command string, args []string) (string, error) {
log.Debug().Msg(aurora.Sprintf(aurora.Gray(18, "[%v] > %v %v"), dir, command, strings.Join(args, " ")))
cmd := exec.CommandContext(ctx, command, args...)
cmd.Env = os.Environ()
cmd.Dir = dir
output, err := cmd.CombinedOutput()
return string(output), err
}
func getSeparateCommandAndArgs(ctx context.Context, command string, args []interface{}) (c string, a []string) {
command = fmt.Sprintf(command, args...)
// trim spaces and de-dupe spaces in string
command = strings.ReplaceAll(command, " ", " ")
command = strings.Trim(command, " ")
// split into actual command and arguments
commandArray := strings.Split(command, " ")
// remove empty items
var cleanedCommandArray []string
for _, str := range commandArray {
if str != "" {
cleanedCommandArray = append(cleanedCommandArray, str)
}
}
if len(cleanedCommandArray) > 0 {
c = cleanedCommandArray[0]
}
if len(cleanedCommandArray) > 1 {
a = cleanedCommandArray[1:]
}
return
}
// RunCommandWithArgsWithoutLog runs a single command and passes the arguments; it logs a fatal on error without any log output
// RunCommandWithArgsWithoutLog(ctx, "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func RunCommandWithArgsWithoutLog(ctx context.Context, command string, args []string) {
err := RunCommandWithArgsExtendedWithoutLog(ctx, command, args)
HandleError(err)
}
// RunCommandWithArgsExtendedWithoutLog runs a single command and passes the arguments; it returns an error if command execution failed without any log output
// err := RunCommandWithArgsExtendedWithoutLog(ctx, "kubectl", []string{"logs", "-l", "app="+app, "-n", namespace)
func RunCommandWithArgsExtendedWithoutLog(ctx context.Context, command string, args []string) error {
cmd := exec.CommandContext(ctx, command, args...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
}