-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep.go
55 lines (47 loc) · 1.13 KB
/
step.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
package main
import (
"bytes"
"os/exec"
)
// A pipeline step.
type step struct {
name string // step name
exe string // executable name of the external tool we want to execute
args []string // arguments for the executable
message string // output message in case of success
proj string // target project on which to execute the task
}
// newStep instantiates and returns new step.
func newStep(name, exe, message, proj string, args []string) step {
return step{
name: name,
exe: exe,
args: args,
message: message,
proj: proj,
}
}
func (s step) execute() (string, error) {
cmd := exec.Command(s.exe, s.args...) //nolint:gosec
cmd.Dir = s.proj
var stderr, stdout bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", &stepError{
step: s.name,
msg: "failed to execute: " + stderr.String(),
cause: err,
}
}
if s.name == "go fmt" {
// gofmt -l will list unformatted files
if stdout.Len() > 0 {
return "", &stepError{
step: s.name,
msg: "invalid format: " + stdout.String(),
}
}
}
return s.message, nil
}