-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravis-exec.go
117 lines (100 loc) · 2.76 KB
/
travis-exec.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"os"
"io/ioutil"
"github.com/smallfish/simpleyaml"
"fmt"
"strings"
"os/exec"
"bufio"
)
func setEnv(envvar string) {
fmt.Println("\n[+] Adding to environment variables: " + envvar)
vars := strings.Split(envvar, " ")
for i := range vars {
currentVar := strings.Split(vars[i], "=")
// If PATH => prefix
if currentVar[0] == "PATH" {
os.Setenv(currentVar[0], strings.Replace(currentVar[1], "\"", "", -1) + ":" + os.Getenv("PATH"))
} else {
os.Setenv(currentVar[0], currentVar[1])
}
//fmt.Println(os.Getenv(currentVar[0]))
}
}
func runCmd(command string) {
fmt.Println("\n[+] Running command: " + command)
// Prepare cmd to run with args
cmdArgs := strings.Fields(command)
cmd := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
// Stream lines output to stdout
stdout, _ := cmd.StdoutPipe()
cmd.Start()
scanner := bufio.NewScanner(stdout)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
m := scanner.Text()
fmt.Println(m)
}
cmd.Wait()
}
func runSection(yaml simpleyaml.Yaml, key string) {
list, _ := yaml.Get(key).Array()
for item := range list {
current, _ := yaml.Get(key).GetIndex(item).String()
runCmd(current)
}
}
func checkTravisLifecycleExists(yaml *simpleyaml.Yaml, travisLifecycle map[string]bool, lifecycle string) {
if _, ok := travisLifecycle[lifecycle]; ok {
runSection(*yaml, lifecycle)
}
}
func main() {
var travisField string
var travisKeys []string
travisLifecycle := map[string]bool {
"before_install": true,
"install": true,
"before_script": true,
"script": true,
"after_success": true,
}
// Check args
if len(os.Args) < 2 {
fmt.Println("Usage: travis-exec <travis_file> <optional travis_field>")
fmt.Println("Example: go run tests/k8s-euft/travis-exec.go .travis.yml install")
os.Exit(1)
}
filename := os.Args[1]
if len(os.Args) == 3 {
travisField = os.Args[2]
}
source, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
// Loading Yaml config
yaml, err := simpleyaml.NewYaml(source)
if err != nil {
panic(err)
}
// Run all tests (install/script...) for all envs or the specified one
allEnv, _ := yaml.Get("env").Array()
if len(travisField) == 0 {
travisKeys, _ = yaml.GetMapKeys()
} else {
travisKeys = append(travisKeys, travisField)
}
// for each env, do:
for currentIndex := range allEnv {
envName, _ := yaml.Get("env").GetIndex(currentIndex).String()
setEnv(envName)
// run lifecycles in travis order
checkTravisLifecycleExists(yaml, travisLifecycle, "before_install")
checkTravisLifecycleExists(yaml, travisLifecycle, "install")
checkTravisLifecycleExists(yaml, travisLifecycle, "before_script")
checkTravisLifecycleExists(yaml, travisLifecycle, "script")
checkTravisLifecycleExists(yaml, travisLifecycle, "after_success")
}
}