-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
57 lines (51 loc) · 1.06 KB
/
Copy pathcommand.go
File metadata and controls
57 lines (51 loc) · 1.06 KB
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
package main
import (
"fmt"
"os/exec"
"strings"
)
type Executor struct {
Force, Log, Done bool
Errors []error
}
func NewExecutor() Executor {
return Executor{Force: false, Log: false, Done: false, Errors: make([]error, 0)}
}
func (e *Executor) Execute(command string, args ...string) (output string) {
if e.Done {
return ""
}
for index, value := range args {
command = strings.Replace(command, fmt.Sprintf("$%d", index+1), value, -1)
}
if e.Log {
fmt.Println("Executor running: " + command)
}
parts := strings.Split(command, " ")
cmd := exec.Command(parts[0], parts[1:]...)
out, err := cmd.CombinedOutput()
if err != nil {
if e.Force {
e.Done = true
}
e.Errors = append(e.Errors, err)
}
if e.Log {
fmt.Println(string(out))
}
return string(out)
}
func (e Executor) DidError() (errored bool) {
return len(e.Errors) > 0 || e.Done
}
func (e Executor) FormatErrors() (output string) {
output = ""
for _, err := range e.Errors {
if output == "" {
output += err.Error()
} else {
output += "\n" + err.Error()
}
}
return ""
}