-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
225 lines (209 loc) · 6.66 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//go:generate go run data/generate.go
package main
import (
"bufio"
"fmt"
"os"
"path"
"strings"
"sync"
shellquote "github.com/kballard/go-shellquote"
)
func main() {
app := initCLI()
app.Start(os.Args, func(config *Config) {
godev := InitGoDev(config)
godev.Start()
})
}
// InitGoDev initialises the application using a configuration
// struct and creating a logger
func InitGoDev(config *Config) *GoDev {
return &GoDev{
config: config,
logger: InitLogger(&LoggerConfig{
Name: "main",
Format: "production",
Level: config.LogLevel,
}),
}
}
// GoDev holds the logic and values needed for GoDev to run
type GoDev struct {
config *Config
logger *Logger
watcher *Watcher
runner *Runner
}
// Start should only be called once and triggers the pipeline
// and watcher
func (godev *GoDev) Start() {
defer godev.logger.Infof("godev has ended")
godev.logger.Infof("godev has started")
if godev.config.RunDefault || godev.config.RunTest {
godev.startWatching()
} else if godev.config.RunInit {
godev.initialiseDirectory()
}
}
func (godev *GoDev) createPipeline() []*ExecutionGroup {
var pipeline []*ExecutionGroup
for execGroupIndex, execGroup := range godev.config.ExecGroups {
executionGroup := &ExecutionGroup{}
var executionCommands []*Command
commands := strings.Split(execGroup, godev.config.CommandsDelimiter)
for _, command := range commands {
if sections, err := shellquote.Split(command); err != nil {
panic(err)
} else {
arguments := sections[1:]
if execGroupIndex == len(godev.config.ExecGroups)-1 {
arguments = append(arguments, godev.config.CommandArguments...)
}
executionCommands = append(
executionCommands,
InitCommand(&CommandConfig{
Application: sections[0],
Arguments: arguments,
Directory: godev.config.WorkDirectory,
Environment: godev.config.EnvVars,
LogLevel: godev.config.LogLevel,
}),
)
}
}
executionGroup.commands = executionCommands
pipeline = append(pipeline, executionGroup)
}
return pipeline
}
func (godev *GoDev) eventHandler(events *[]WatcherEvent) bool {
for _, e := range *events {
godev.logger.Trace(e)
}
godev.runner.Trigger()
return true
}
func (godev *GoDev) initialiseInitialisers() []Initialiser {
return []Initialiser{
InitGitInitialiser(&GitInitialiserConfig{
Path: path.Join(godev.config.WorkDirectory),
}),
InitFileInitialiser(&FileInitialiserConfig{
Path: path.Join(godev.config.WorkDirectory, "/.gitignore"),
Data: []byte(DataDotGitignore),
Question: "seed a .gitignore?",
}),
InitFileInitialiser(&FileInitialiserConfig{
Path: path.Join(godev.config.WorkDirectory, "/go.mod"),
Data: []byte(DataGoDotMod),
Question: "seed a go.mod?",
}),
InitFileInitialiser(&FileInitialiserConfig{
Path: path.Join(godev.config.WorkDirectory, "/main.go"),
Data: []byte(DataMainDotgo),
Question: "seed a main.go?",
}),
InitFileInitialiser(&FileInitialiserConfig{
Path: path.Join(godev.config.WorkDirectory, "/Dockerfile"),
Data: []byte(DataDockerfile),
Question: "seed a Dockerfile?",
}),
InitFileInitialiser(&FileInitialiserConfig{
Path: path.Join(godev.config.WorkDirectory, "/.dockerignore"),
Data: []byte(DataDotDockerignore),
Question: "seed a .dockerignore?",
}),
InitFileInitialiser(&FileInitialiserConfig{
Path: path.Join(godev.config.WorkDirectory, "/Makefile"),
Data: []byte(DataMakefile),
Question: "seed a Makefile?",
}),
}
}
// initialiseDirectory assists in initialising the working directory
func (godev *GoDev) initialiseDirectory() {
if !directoryExists(godev.config.WorkDirectory) {
godev.logger.Errorf("the directory at '%s' does not exist - create it first with:\n mkdir -p %s", godev.config.WorkDirectory, godev.config.WorkDirectory)
os.Exit(1)
}
initialisers := godev.initialiseInitialisers()
for i := 0; i < len(initialisers); i++ {
initialiser := initialisers[i]
if initialiser.Check() {
err := initialiser.Handle(true)
if err != nil {
fmt.Println(Color("red", err.Error()))
}
} else {
reader := bufio.NewReader(os.Stdin)
if initialiser.Confirm(reader) {
fmt.Println(Color("green", "godev> sure thing"))
initialiser.Handle()
} else {
fmt.Println(Color("yellow", "godev> lets skip that then"))
}
}
}
}
func (godev *GoDev) initialiseRunner() {
godev.runner = InitRunner(&RunnerConfig{
Pipeline: godev.createPipeline(),
LogLevel: godev.config.LogLevel,
})
}
func (godev *GoDev) initialiseWatcher() {
godev.watcher = InitWatcher(&WatcherConfig{
FileExtensions: godev.config.FileExtensions,
IgnoredNames: godev.config.IgnoredNames,
RefreshRate: godev.config.Rate,
LogLevel: godev.config.LogLevel,
})
godev.watcher.RecursivelyWatch(godev.config.WatchDirectory)
}
func (godev *GoDev) logUniversalConfigurations() {
godev.logger.Debugf("flag - init : %v", godev.config.RunInit)
godev.logger.Debugf("flag - test : %v", godev.config.RunTest)
godev.logger.Debugf("flag - view : %v", godev.config.RunView)
godev.logger.Debugf("watch directory : %s", godev.config.WatchDirectory)
godev.logger.Debugf("work directory : %s", godev.config.WorkDirectory)
godev.logger.Debugf("build output : %s", godev.config.BuildOutput)
}
func (godev *GoDev) logWatchModeConfigurations() {
config := godev.config
logger := godev.logger
logger.Debugf("environment : %v", config.EnvVars)
logger.Debugf("file extensions : %v", config.FileExtensions)
logger.Debugf("ignored names : %v", config.IgnoredNames)
logger.Debugf("refresh interval : %v", config.Rate)
logger.Debugf("execution delim : %s", config.CommandsDelimiter)
logger.Debug("execution groups as follows...")
for execGroupIndex, execGroup := range config.ExecGroups {
logger.Debugf(" %v) %s", execGroupIndex+1, execGroup)
commands := strings.Split(execGroup, config.CommandsDelimiter)
for commandIndex, command := range commands {
sections, err := shellquote.Split(command)
if err != nil {
panic(err)
}
application := sections[0]
arguments := sections[1:]
if execGroupIndex == len(config.ExecGroups)-1 {
arguments = append(arguments, config.CommandArguments...)
}
logger.Debugf(" %v > %s %v", commandIndex+1, application, arguments)
}
}
}
func (godev *GoDev) startWatching() {
godev.logUniversalConfigurations()
godev.logWatchModeConfigurations()
godev.initialiseWatcher()
godev.initialiseRunner()
var wg sync.WaitGroup
godev.watcher.BeginWatch(&wg, godev.eventHandler)
godev.logger.Infof("working dir : '%s'", godev.config.WorkDirectory)
godev.logger.Infof("watching dir: '%s'", godev.config.WatchDirectory)
godev.runner.Trigger()
wg.Wait()
}