-
Notifications
You must be signed in to change notification settings - Fork 24
/
command.go
59 lines (47 loc) · 1.14 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
package clients
import (
"os/exec"
"time"
"github.com/hashicorp/go-hclog"
)
type Command interface {
Execute(string, ...string) error
}
// Command executes local commands
type CommandImpl struct {
timeout time.Duration
log hclog.Logger
}
// NewCommand creates a new command with the given logger and maximum command time
func NewCommand(maxCommandTime time.Duration, l hclog.Logger) Command {
return &CommandImpl{maxCommandTime, l}
}
// Execute the given command
func (c *CommandImpl) Execute(command string, args ...string) error {
cmd := exec.Command(
command,
args...,
)
// set the standard out and error to the logger
cmd.Stdout = c.log.StandardWriter(&hclog.StandardLoggerOptions{InferLevels: true})
cmd.Stderr = c.log.StandardWriter(&hclog.StandardLoggerOptions{InferLevels: true})
/*
// wait for timeout
t := time.AfterFunc(c.timeout, func() {
// kill the running process
cmd.Process.Kill()
return fmt.Errorf("Command timed out before completing")
})
*/
err := cmd.Run()
if err != nil {
return err
}
// command has completed clear the timeout timer
/*
if t!= nil {
t.Stop()
}
*/
return nil
}