-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (104 loc) · 2.72 KB
/
main.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
118
119
package main
import (
"flag"
"log"
"os"
"path"
)
var (
config = flag.String("config", "config.json", "configuration file containing job declarations")
outDir = flag.String("outdir", "out", "output directory for jobs")
)
func main() {
log.SetOutput(os.Stdout)
flag.Parse()
if err := run(); err != nil {
log.Printf("error: %v\n", err)
}
}
// run contains the core logic of crudeci
func run() error {
cwd, err := os.Getwd()
if err != nil {
return err
}
outDir := path.Join(cwd, *outDir)
cfg := path.Join(cwd, *config)
log.Printf("[INFO] Parsing jobs...")
jobs, err := ParseJobs(outDir, cfg)
if err != nil {
return err
}
for _, job := range jobs {
err := InitJob(job)
if err != nil {
return err
}
}
runSequentially(jobs)
successTable := successTable()
for _, job := range jobs {
log.Printf("Job %s - %v\n", job.Name, successTable[job.Successful])
}
return nil
}
// runSequentially runs the jobs in a sequential manner.
func runSequentially(jobs []*Job) {
for _, job := range jobs {
log.Printf("[INFO] Running pipeline for %s", job.Name)
for _, step := range job.Pipeline.Steps {
// log.Printf("TASK IDX %d\n", idx)
out, err := step.Run()
if err != nil {
log.Printf("> %s step\n❌ %s\n%s", step.(*Step).Name, out, err)
job.Successful = false
break
}
job.Successful = true
log.Printf("> %s step\n✅ %s", step.(*Step).Name, out)
}
}
}
// runConcurrently runs the jobs in a concurrent manner. Runs an anonymous
// goroutine until a SIGINT or SIGTERM signal are sent, at which point the
// quitChannel channel unblocks, allowing the function to return.
//
// func runConcurrently(jobs []Job, intervalInSeconds int) {
// go func() {
// for {
// for _, job := range jobs {
// log.Printf("[INFO] Running pipeline for %s", job.Name)
// for _, step := range job.pipeline.Steps {
// out, err := step.Run()
// if err != nil {
// log.Printf("> %s step\n❌ %s\n%s", step.(*Step).Name, out, err)
// job.successful = false
// break
// }
// job.successful = true
// log.Printf("> %s step\n✅ %s", step.(*Step).Name, out)
// }
// log.Println("Job status:", job)
// }
// time.Sleep(time.Second * time.Duration(intervalInSeconds))
// }
// }()
// quit := make(chan os.Signal, 1)
// signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
// <-quit
// log.Println("goodbye!")
// }
// Exists returns true if file/directory passed exist; false otherwise
func Exists(fn string) (bool, error) {
_, err := os.Open(fn)
if err != nil {
return false, err
}
return true, nil
}
func successTable() map[bool]string {
successTable := make(map[bool]string)
successTable[true] = "PASS"
successTable[false] = "FAIL"
return successTable
}