-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwist.go
208 lines (179 loc) · 4.5 KB
/
twist.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// This file is part of twist
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"io/ioutil"
)
const (
manifestFile = "manifest.json"
)
type step struct {
File string
RawText string
ProcessedText string
LineNo int
Args []string
}
var availableSteps []*step
type manifest struct {
Language string
}
func getProjectManifest() *manifest {
contents := readFileContents(manifestFile)
dec := json.NewDecoder(strings.NewReader(contents))
var m manifest
for {
if err := dec.Decode(&m); err == io.EOF {
break
} else if err != nil {
fmt.Printf("Failed to read: %s. %s\n", manifestFile, err.Error())
os.Exit(1)
}
}
return &m
}
func findScenarioFiles(fileChan chan <- string) {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
walkFn := func(filePath string, info os.FileInfo, err error) error {
ext := path.Ext(info.Name())
if strings.ToLower(ext) == ".scn" {
fileChan <- filePath
}
return nil
}
filepath.Walk(pwd, walkFn)
fileChan <- "done"
}
func parseScenarioFiles(fileChan <-chan string) {
for {
scenarioFilePath := <-fileChan
if scenarioFilePath == "done" {
break
}
tokens, err := parse(readFileContents(scenarioFilePath))
if se, ok := err.(*syntaxError); ok {
fmt.Printf("%s:%d:%d %s\n", scenarioFilePath, se.lineNo, se.colNo, se.message)
} else {
for _, token := range tokens {
if token.kind == typeWorkflowStep {
s := &step{File: scenarioFilePath, RawText: token.line, ProcessedText: token.value, LineNo: token.lineNo, Args: token.args}
availableSteps = append(availableSteps, s)
}
}
}
}
}
func makeListOfAvailableSteps() {
fileChan := make(chan string)
go findScenarioFiles(fileChan)
go parseScenarioFiles(fileChan)
}
func startAPIService() {
http.HandleFunc("/steps", func(w http.ResponseWriter, r *http.Request) {
js, err := json.Marshal(availableSteps)
if err != nil {
io.WriteString(w, err.Error())
} else {
w.Header()["Content-Type"] = []string{"application/json"}
w.Write(js)
}
})
log.Fatal(http.ListenAndServe(":8889", nil))
}
func createProjectTemplate(projectName string) {
if exist,_ := exists(projectName); !exist {
fmt.Println("Creating directory ", projectName)
err := os.Mkdir(projectName, 0766)
if err != nil {
panic(err)
}
}
fmt.Println("Copying artifacts")
for artifact, _ := range _bindata {
copyFileToProject(projectName, artifact)
}
fmt.Println("Copying complete")
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return false, err
}
func copyFileToProject(projectName string, relativePath string) {
paths := strings.Split(relativePath, "/")[1:]
pathWithFile := strings.Join(paths[:len(paths)], "/")
pathWithoutFile := strings.Join(paths[:len(paths)-1], "/")
err := os.MkdirAll(projectName+"/"+pathWithoutFile, 0766);
if err != nil {
panic(err)
}
fileContent, err := Asset(relativePath)
if err != nil {
panic(err)
}
fmt.Println("copying ", projectName+"/"+pathWithFile)
ioutil.WriteFile(projectName+"/"+pathWithFile, fileContent, 0766)
}
// Command line flags
var daemonize = flag.Bool("daemonize", false, "Run as a daemon")
var create = flag.String("create", "", "Create a template")
var wd = flag.String("wd", "", "the working directory from which the executable has to run")
func printUsage() {
fmt.Fprintf(os.Stderr, "usage: twist [options] scenario\n")
flag.PrintDefaults()
os.Exit(2)
}
func main() {
flag.Parse()
if *wd != "" {
os.Chdir(*wd)
value, _ := os.Getwd()
fmt.Println("Current working dir", value)
}
if *daemonize {
makeListOfAvailableSteps()
startAPIService()
} else if *create != "" {
createProjectTemplate(*create)
} else {
if len(flag.Args()) == 0 {
printUsage()
}
scenarioFile := flag.Arg(0)
tokens, err := parse(readFileContents(scenarioFile))
if se, ok := err.(*syntaxError); ok {
fmt.Printf("%s:%d:%d %s\n", scenarioFile, se.lineNo, se.colNo, se.message)
os.Exit(1)
}
manifest := getProjectManifest()
_, err = startRunner(manifest)
if err != nil {
fmt.Printf("Failed to start a runner. %s\n", err.Error())
os.Exit(1)
}
conn, err := acceptConnection()
if err != nil {
fmt.Printf("Failed to get a runner. %s\n", err.Error())
os.Exit(1)
}
execution := newExecution(manifest, tokens, conn)
err = execution.start()
if err != nil {
fmt.Printf("Execution failed. %s\n", err.Error())
os.Exit(1)
}
}
}