-
Notifications
You must be signed in to change notification settings - Fork 251
/
host.go
69 lines (62 loc) · 2.24 KB
/
host.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
// Package host provides facilities for interfacing with the host operating
// system, logging activities performed.
package host
import (
"log"
"os/exec"
"strings"
"github.com/taskcluster/shell"
)
// Run is equivalent to exec.Command(name, arg...).Run() but with logging.
func Run(name string, arg ...string) (err error) {
_, err = RunCommand(exec.Command(name, arg...))
return
}
// CombinedOutput is equivalent to exec.Command(name, arg...).CombinedOutput()
// but with logging.
func CombinedOutput(name string, arg ...string) (combinedOutput string, err error) {
return RunCommand(exec.Command(name, arg...))
}
// RunBatch calls Run for each command in commands, in sequence. If allowFail
// is false it will return if an error is returned from Run. All errors are
// logged regardless of allowFail. The returned error is the result of the last
// Run call.
func RunBatch(allowFail bool, commands ...[]string) (err error) {
for _, command := range commands {
err = Run(command[0], command[1:]...)
if err != nil {
log.Printf("%v", err)
if !allowFail {
return err
}
}
}
return err
}
// RunIgnoreError calls CombinedOutput(comand, args...). If errString is found
// in the command output, found is true and err is nil. Otherwise found is
// false, and err is the error returned from CombinedOutput (possibly nil).
func RunIgnoreError(errString string, command string, args ...string) (found bool, err error) {
output, err := CombinedOutput(command, args...)
if err != nil {
if strings.Contains(output, errString) {
return true, nil
}
}
return false, err
}
// RunCommand logs cmd.Args, calls cmd.CombinedOutput(), and if an error
// occurs, logs the command output. It does not log the error, it is expected
// that the caller takes care of logging error, if required. The caller is not
// expected to log the command output in the case of failure, since this
// function has already done that. The combined output is cast to a string and
// returned together with the error.
func RunCommand(cmd *exec.Cmd) (combinedOutput string, err error) {
log.Print("Running command: " + shell.Escape(cmd.Args...))
out, err := cmd.CombinedOutput()
if err != nil {
log.Print("Error running command:")
log.Print(string(out))
}
return string(out), err
}