forked from netzkern/butler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
358 lines (309 loc) · 7.47 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"fmt"
"net/url"
"os"
"runtime"
"sort"
"github.com/skratchdot/open-golang/open"
logy "github.com/apex/log"
"github.com/netzkern/butler/commands/confluence"
"github.com/netzkern/butler/commands/confluence/builder"
"github.com/netzkern/butler/commands/confluence/space"
"github.com/netzkern/butler/commands/githook"
"github.com/netzkern/butler/commands/template"
"github.com/netzkern/butler/config"
"github.com/netzkern/butler/updater"
"github.com/urfave/cli"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/core"
yaml "gopkg.in/yaml.v2"
)
const (
appName = "Butler"
appDesc = "Welcome to Butler, your personal assistent to scaffold new projects.\n"
githubIssueLink = "https://github.com/netzkern/butler/issues/new"
author = "netzkern AG"
repository = "netzkern/butler"
surveyFilename = "butler-survey.yml"
configName = "butler.yml"
)
type commandSelection struct {
Action string
}
var (
cfg *config.Config
version = "0.9.0"
primaryCommands = []string{
"Create Project",
"Create Confluence Space",
"Create Git Hooks",
"Maintenance",
"Exit",
}
devCommands = []string{
"Dump config",
"Auto Update",
"Report a bug",
"Version",
}
primaryQs = []*survey.Question{
{
Name: "action",
Validate: survey.Required,
Prompt: &survey.Select{
Message: "How can I help you, Sir?",
Options: primaryCommands,
},
},
}
devQs = []*survey.Question{
{
Name: "action",
Validate: survey.Required,
Prompt: &survey.Select{
Message: "How can I help you, Sir?",
Options: devCommands,
},
},
}
)
func init() {
logy.SetLevel(logy.InfoLevel)
cfg = config.ParseConfig(configName)
// TERM contains a identifier for the text window’s capabilities (UNIX).
if os.Getenv("TERM") == "xterm-256color" {
return
}
// Fallback for Windows
if runtime.GOOS == "windows" {
core.ErrorIcon = "x"
core.HelpIcon = "????"
core.QuestionIcon = "?"
core.SelectFocusIcon = ">"
core.MarkedOptionIcon = "[x]"
core.UnmarkedOptionIcon = "[ ]"
}
}
func showPrimaryCommands() *commandSelection {
answer := &commandSelection{}
err := survey.Ask(primaryQs, answer)
if err != nil {
logy.Fatal(err.Error())
}
return answer
}
func showMaintananceCommands() *commandSelection {
answer := &commandSelection{}
err := survey.Ask(devQs, answer)
if err != nil {
logy.Fatal(err.Error())
}
return answer
}
func listMaintananceCommands() {
answer := showMaintananceCommands()
switch taskType := answer.Action; taskType {
case "Dump config":
dumpConfig()
case "Auto Update":
updater.ConfirmAndSelfUpdate(repository, version)
case "Report a bug":
err := open.Start(githubIssueLink)
if err != nil {
logy.WithError(err).Error("report a bug")
return
}
case "Version":
fmt.Printf("Version: %s\n", version)
default:
logy.Infof("Command %s is not implemented!", taskType)
}
}
func interactiveCliMode() {
fmt.Println(appDesc)
answer := showPrimaryCommands()
cd, err := os.Getwd()
if err != nil {
logy.WithError(err).Error("getwd")
return
}
switch taskType := answer.Action; taskType {
case "Create Project":
command := template.New(
template.WithTemplates(cfg.Templates),
template.WithVariables(cfg.Variables),
template.SetConfigName(surveyFilename),
template.WithButlerVersion(version),
template.WithCwd(cd),
)
err := command.StartCommandSurvey()
if err != nil {
logy.WithError(err).Error("start survey")
return
}
err = command.Run()
if err != nil {
logy.WithError(err).Error("run command")
return
}
fmt.Println()
command.TaskTracker.PrintSummary(os.Stdout)
case "Create Confluence Space":
// validate confluence configuration
if cfg.ConfluenceAuthMethod != "" {
if cfg.ConfluenceAuthMethod == "basic" {
if len(cfg.ConfluenceBasicAuth) != 2 {
logy.WithField("ENV", "CONFLUENCE_BASIC_AUTH").
Fatalf("invalid basic auth credentials")
}
} else {
logy.WithField("ENV", "CONFLUENCE_AUTH_METHOD").
Fatalf("only basic auth is currently supported")
}
} else {
logy.Fatalf(
"invalid confluence settings. For more information see https://github.com/netzkern/butler/tree/master/docs/confluence.md",
)
}
// validate confluence url
if cfg.ConfluenceURL != "" {
_, err := url.ParseRequestURI(cfg.ConfluenceURL)
if err != nil {
logy.WithField("ENV", "BUTLER_CONFLUENCE_URL").
Fatalf("invalid url")
}
}
client := confluence.NewClient(
confluence.WithAuth(
confluence.BasicAuth(
cfg.ConfluenceBasicAuth[0],
cfg.ConfluenceBasicAuth[1],
),
),
)
createSpaceCmd := space.NewSpace(
space.WithEndpoint(cfg.ConfluenceURL),
space.WithClient(client),
)
err := createSpaceCmd.StartCommandSurvey()
if err != nil {
logy.WithError(err).Error("start survey")
return
}
spaceData, err := createSpaceCmd.Run()
if err != nil {
logy.WithError(err).Error("run command")
return
}
// skip tree builder when no template exist
if len(cfg.Confluence.Templates) > 0 {
treeBuilderCmd := builder.NewTreeBuilder(
builder.WithTemplates(cfg.Confluence.Templates),
builder.WithClient(client),
builder.WithEndpoint(cfg.ConfluenceURL),
builder.WithSpaceKey(spaceData.Key),
)
err = treeBuilderCmd.StartCommandSurvey()
if err != nil {
logy.WithError(err).Error("start survey")
return
}
if err := treeBuilderCmd.Run(); err != nil {
logy.WithError(err).Error("run command")
return
}
}
case "Create Git Hooks":
command := githook.New(githook.WithCwd(cd))
err := command.StartCommandSurvey()
if err != nil {
logy.WithError(err).Error("start survey")
return
}
err = command.Run()
if err != nil {
logy.WithError(err).Error("run command")
return
}
case "Maintenance":
listMaintananceCommands()
case "Exit":
os.Exit(0)
default:
logy.Infof("Command '%s' is not implemented!", taskType)
return
}
fmt.Println("Command executed successfully")
}
func dumpConfig() error {
str, err := yaml.Marshal(cfg)
if err != nil {
return err
}
fmt.Println(string(str))
return nil
}
func cliMode() {
type surveyResult map[string]interface{}
app := cli.NewApp()
app.Name = appName
app.Usage = "your personal assistent to scaffold new projects."
app.Author = author
app.Version = version
app.Description = appDesc
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "logLevel",
Value: "info",
Usage: "Log level",
EnvVar: "BUTLER_LOG_LEVEL",
},
}
app.Commands = []cli.Command{
{
Name: "interactive",
Aliases: []string{"ui"},
Usage: "Enable interactive cli",
Action: func(c *cli.Context) error {
setLogLevel(c.GlobalString("logLevel"))
interactiveCliMode()
return nil
},
},
{
Name: "dump-config",
Usage: "Dumps the final config file",
Action: func(c *cli.Context) error { return dumpConfig() },
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
err := app.Run(os.Args)
if err != nil {
logy.Fatalf("Failed executing command, see %s", err)
}
}
func setLogLevel(level string) {
switch level {
case "info":
logy.SetLevel(logy.InfoLevel)
case "debug":
logy.SetLevel(logy.DebugLevel)
case "fatal":
logy.SetLevel(logy.FatalLevel)
case "error":
logy.SetLevel(logy.ErrorLevel)
case "warn":
logy.SetLevel(logy.WarnLevel)
default:
logy.Fatalf("Invalid log level '%s'", level)
}
}
func main() {
if len(os.Args[1:]) > 0 {
cliMode()
return
}
interactiveCliMode()
}