-
Notifications
You must be signed in to change notification settings - Fork 111
/
command.go
48 lines (40 loc) · 1.13 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
package gatherlogs
import (
"io"
"strings"
log "github.com/sirupsen/logrus"
"github.com/chef/automate/lib/platform/command"
)
// Command represents a system command
type Command struct {
Name string // friendly name (used for uniq ref, file name, etc)
Cmd string // command to run
Args []string // arguments to the command
OutputHandler func() (io.WriteCloser, error)
}
func (c *Command) execute() error {
cmdString := c.Cmd + " " + strings.Join(c.Args, " ")
logctx := log.WithField("command", cmdString)
out, err := c.OutputHandler()
if err != nil {
logctx.WithError(err).Error("failed to open to output file")
return err
}
defer out.Close()
logctx.Info("Collecting output for support bundle")
_, err = io.WriteString(out, cmdString+"\n\n")
if err != nil {
logctx.WithError(err).Error("failed to write to output file")
return err
}
err = command.Run(c.Cmd,
command.Args(c.Args...),
command.Envvar("HAB_LICENSE", "accept-no-persist"),
command.Stdout(out),
command.Stderr(out))
if err != nil {
logctx.WithError(err).Info("Failed to execute command")
return err
}
return nil
}