-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
210 lines (193 loc) · 7.62 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
package main
import (
"os"
"sort"
"github.com/codegangsta/cli"
log "github.com/Sirupsen/logrus"
"reflect"
"github.com/matthieudelaro/nut/persist"
Utils "github.com/matthieudelaro/nut/utils"
Config "github.com/matthieudelaro/nut/config"
)
func main() {
log.SetLevel(log.ErrorLevel)
// log.SetLevel(log.DebugLevel)
// get the folder where nut has been called
pwd, err := os.Getwd()
if err != nil {
log.Fatal("find path error: ", err)
}
context, err := Utils.NewContext(pwd, pwd)
if err != nil {
log.Fatal("Error with context: ", err)
}
log.Debug("main first context: ", context)
// try to parse the folder's configuration to add macros
var macros []cli.Command
project, projectContext, err := Config.FindProject(context)
log.Debug("main second context: ", projectContext)
projectMacros := map[string]Config.Macro{}
initFlag := false
logsFlag := false
cleanFlag := false
execFlag := ""
inheriteConfigMacroFlag := ""
gitHubFlag := ""
macroFlag := ""
useDockerCLIFlag := DOCKERCLI_FLAG_DEFAULT_VALUE
if err == nil {
// Macros are stored in a random order.
// But we want to display them in the same order everytime.
// So sort the names of the macros.
projectMacros = Config.GetMacros(project)
macroNamesOrdered := make([]string, 0, len(projectMacros))
for key, _ := range projectMacros {
macroNamesOrdered = append(macroNamesOrdered, key)
}
sort.Strings(macroNamesOrdered)
macros = make([]cli.Command, len(projectMacros))
log.Debug(len(macroNamesOrdered), " macros")
for index, name := range macroNamesOrdered {
macro := projectMacros[name]
log.Debug("macro ", name, ": ", macro)
// If the nut file containes a macro which has not any field defined,
// then the macro will be nil.
// So check value of macro:
if macro == reflect.Zero(reflect.TypeOf(macro)).Interface() { // just check whether it macro is nil
// it seems uselessly complicated, but:
// if macro == nil { // doesn't work the trivial way one could expect
// if macro.(*MacroBase) == nil { // panic: interface conversion: main.Macro is *main.MacroV3, not *main.MacroBase
// Checking for nil doesn't seems like a good solution. TODO: ? require at least a "usage" field for each macro in the nut file?
log.Warn("Undefined properties of macro " + name + ".")
} else {
nameClosure := name
usage := "macro: "
if macroUsage := Config.GetUsage(macro); macroUsage == "" {
usage += "undefined usage. define one with 'usage' property for this macro."
} else {
usage += macroUsage
}
macros[index] = cli.Command{
Name: nameClosure,
Usage: usage,
Aliases: Config.GetAliases(macro),
UsageText: Config.GetUsageText(macro),
Description: Config.GetDescription(macro),
Action: func(c *cli.Context) error {
if logsFlag {
log.SetLevel(log.DebugLevel)
}
execMacro(macro, projectContext, useDockerCLIFlag != DOCKERCLI_FLAG_DEFAULT_VALUE)
return nil
},
}
}
}
} else {
log.Error("Could not parse configuration: " + err.Error())
macros = []cli.Command{}
}
app := cli.NewApp()
app.Name = "nut"
app.Version = "0.1.5 dev" //RELEASE_BUILD_PLACEHOLDER//
app.Usage = "the development environment, containerized"
// app.EnableBashCompletion = true
app.Flags = []cli.Flag {
cli.BoolFlag{
Name: "clean",
Usage: "clean all data stored in .nut",
Destination: &cleanFlag,
},
cli.BoolFlag{
Name: "init",
Usage: "initialize a nut project",
Destination: &initFlag,
},
cli.StringFlag{
Name: "github",
Usage: "Use with --init: provide a GitHub repository to initialize Nut.",
Destination: &gitHubFlag,
},
cli.BoolFlag{
Name: "logs",
Usage: "Use with --exec: display log messages. Useful for contributors and to report an issue",
Destination: &logsFlag,
},
cli.StringFlag{
Name: "exec",
Usage: "execute a command in a container.",
Destination: &execFlag,
},
cli.StringFlag{
Name: "macro",
Usage: "Name of the macro to execute. Use with --logs.",
Destination: ¯oFlag,
},
cli.StringFlag{
Name: "extend-macro",
Usage: "Use with --exec: name of the macro from which to inherite the configuration",
Destination: &inheriteConfigMacroFlag,
},
}
if DOCKERCLI_FLAG_DEFAULT_VALUE == false {
app.Flags = append(app.Flags, cli.BoolFlag{
Name: "dockercli",
Usage: "Use Docker CLI, instead of using Docker API to reach the host directly.",
Destination: &useDockerCLIFlag,
})
} else {
app.Flags = append(app.Flags, cli.BoolFlag{
Name: "dockerapi",
Usage: "Use Docker API to reach the host directly, instead of using Docker CLI.",
Destination: &useDockerCLIFlag,
})
}
defaultAction := app.Action
app.Action = func(c *cli.Context) error {
if logsFlag {
log.SetLevel(log.DebugLevel)
}
if cleanFlag {
if project != nil {
persist.CleanStoreFromProject(projectContext.GetRootDirectory())
}
} else if initFlag {
log.Debug("main context for init: ", context)
initSubcommand(c, context, gitHubFlag)
} else if execFlag != "" {
if project != nil {
if inheriteConfigMacroFlag == "" {
execInContainer([]string{execFlag}, project, projectContext, useDockerCLIFlag != DOCKERCLI_FLAG_DEFAULT_VALUE)
} else if macro, ok := projectMacros[inheriteConfigMacroFlag]; ok && project != nil {
execInContainer([]string{execFlag}, macro, projectContext, useDockerCLIFlag != DOCKERCLI_FLAG_DEFAULT_VALUE)
} else {
log.Error("Undefined macro " + macroFlag)
cli.ShowAppHelp(c)
return cli.NewExitError("", 42)
}
} else {
log.Error("Could not find nut configuration.")
cli.ShowAppHelp(c)
return cli.NewExitError("", 42)
}
} else if macroFlag != "" {
if macro, ok := projectMacros[macroFlag]; ok && project != nil {
execMacro(macro, projectContext, useDockerCLIFlag != DOCKERCLI_FLAG_DEFAULT_VALUE)
} else {
log.Error("Undefined macro " + macroFlag)
cli.ShowAppHelp(c)
return cli.NewExitError("", 42)
}
} else {
cli.HandleAction(defaultAction, c)
}
return nil
}
app.Commands = macros
app.CommandNotFound = func(c *cli.Context, macroName string) {
log.Error("Undefined macro " + macroName)
cli.ShowAppHelp(c)
os.Exit(42)
}
app.Run(os.Args)
}