Skip to content

Commit 9463e27

Browse files
committed
[WIP] Initial commit
1 parent cd94df6 commit 9463e27

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# yaml-script-runner
22
Just a basic script runner to easily abort/continue on previous step failure
3+
4+
# WIP: Working on first implementation

main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
)
8+
9+
func main() {
10+
if len(os.Args) < 2 {
11+
log.Fatal("The first command-line argument must be the YAML file path.")
12+
}
13+
14+
yamlFilePath := os.Args[1]
15+
16+
setup, err := ParseYamlFile(yamlFilePath)
17+
if err != nil {
18+
log.Fatal(err)
19+
}
20+
21+
cmds, err := setup.GetExecCommandsFromSteps()
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
26+
for _, c := range cmds {
27+
out, err := c.CombinedOutput()
28+
if err != nil {
29+
errMsg := fmt.Sprintf("ERROR (continue=%t): %s. OUT: %s\n", setup.ContinueIfStepFailed, err.Error(), string(out))
30+
if !setup.ContinueIfStepFailed {
31+
log.Fatalln(errMsg)
32+
} else {
33+
log.Println(errMsg)
34+
continue
35+
}
36+
}
37+
38+
fmt.Println(string(out))
39+
}
40+
}

setup.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"github.com/ghodss/yaml"
5+
"github.com/golang-devops/parsecommand"
6+
"io/ioutil"
7+
"os/exec"
8+
)
9+
10+
type setup struct {
11+
ContinueIfStepFailed bool `json:"continue_if_step_failed"`
12+
Executor []string
13+
Steps []string
14+
}
15+
16+
func (s *setup) GetExecCommandsFromSteps() ([]*exec.Cmd, error) {
17+
cmds := []*exec.Cmd{}
18+
19+
for _, step := range s.Steps {
20+
splittedStep, err := parsecommand.Parse(step)
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
allArgs := s.Executor
26+
allArgs = append(allArgs, splittedStep...)
27+
28+
exe := allArgs[0]
29+
args := []string{}
30+
if len(allArgs) > 1 {
31+
args = allArgs[1:]
32+
}
33+
34+
cmds = append(cmds, exec.Command(exe, args...))
35+
}
36+
37+
return cmds, nil
38+
}
39+
40+
func ParseYamlFile(filePath string) (*setup, error) {
41+
yamlBytes, err := ioutil.ReadFile(filePath)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
s := &setup{}
47+
err = yaml.Unmarshal(yamlBytes, s)
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
return s, nil
53+
}

0 commit comments

Comments
 (0)