-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
52 lines (39 loc) · 827 Bytes
/
util.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
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
log "github.com/sirupsen/logrus"
)
func executeCommand(command string, args []string) (string, error) {
cmd := exec.Command(command, args...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Debugf(
"\ncommand = '%s', \nargs = '%s', \nerror = '%s'",
command, args, stderr.String(),
)
return "", fmt.Errorf("command `%s` failed: %v", command, err)
}
return out.String(), nil
}
func env(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func isString(val interface{}) (string, bool) {
if val == nil {
return "", false
}
switch val := val.(type) {
case string:
return val, true
}
return "", false
}