forked from ashwanthkumar/gocd-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
67 lines (60 loc) · 1.48 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
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/spf13/cobra"
)
const SpecFile = ".gocd-ci.yml"
var ciCommand = &cobra.Command{
Use: "gocd-ci",
Short: "Run your CI pipeline from " + SpecFile,
Long: `Run your CI pipeline from ` + SpecFile,
Run: func(cmd *cobra.Command, args []string) {
Log.Debugf("Trying to open %s\n", SpecFile)
contents, err := ioutil.ReadFile(SpecFile)
if err != nil {
handleError(cmd, err)
}
Log.Debugf("Parsing the %s as YAML\n", SpecFile)
spec, err := DecodeCISpecFromBytes(contents)
if err != nil {
handleError(cmd, err)
}
// Set all the Env values
for key, value := range spec.Env {
Log.Infof("Setting Environment variable '%s' to '%s'\n", key, value)
os.Setenv(key, value)
}
var hasAtleastOneCommandFailed bool
for _, command := range spec.Cmd {
Log.Infof("Executing %s\n", command)
actualCmd := exec.Command("sh", "-c", command)
actualCmd.Stdout = os.Stdout
actualCmd.Stderr = os.Stderr
err = actualCmd.Start()
if err != nil {
hasAtleastOneCommandFailed = true
Log.Errorf("%v\n", err)
continue
}
err = actualCmd.Wait()
if err != nil {
hasAtleastOneCommandFailed = true
Log.Errorf("%v\n", err)
continue
}
}
if hasAtleastOneCommandFailed {
Log.Criticalf("Some errors were found while building. Check the above logs.\n")
os.Exit(1)
}
},
}
func handleError(cmd *cobra.Command, err error) {
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
}