-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
128 lines (117 loc) · 2.83 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
package main
import (
"fmt"
"os"
"runtime"
"github.com/fatih/color"
"github.com/go-gilbert/gilbert/cli/maintenance"
"github.com/go-gilbert/gilbert/cli/scaffold"
"github.com/go-gilbert/gilbert/cli/tasks"
"github.com/go-gilbert/gilbert/log"
"github.com/go-gilbert/gilbert/scope"
"github.com/urfave/cli"
)
var (
// These values will override by linker
version = "dev"
commit = "local build"
)
// FlagNoColor disables color output
const FlagNoColor = "no-color"
var (
// unfortunately, urfave/cli ignores '--verbose' global flag :(
// so it should be defined implicitly in each task
verboseFlag = cli.BoolFlag{
Name: "verbose",
Usage: "shows debug information, useful for troubleshooting",
Destination: &scope.Debug,
}
noColorFlag = cli.BoolFlag{
Name: FlagNoColor,
Usage: "disable color output in terminal",
}
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
app := cli.NewApp()
app.Name = "gilbert"
app.Usage = "Build automation tool for Go"
app.Version = version
app.HideVersion = true
app.Commands = []cli.Command{
{
Name: "version",
Description: "shows application version",
Usage: "Shows application version",
Action: printVersion,
},
{
Name: "run",
Description: "Runs a task declared in manifest file",
Usage: "Runs a task declared in manifest file",
Action: tasks.RunTask,
Before: bootstrap,
Flags: []cli.Flag{
verboseFlag,
noColorFlag,
cli.StringSliceFlag{
Name: tasks.OverrideVarFlag,
},
},
},
{
Name: "ls",
Description: "Lists all tasks defiled in gilbert.yaml",
Usage: "Lists all tasks defiled in gilbert.yaml",
Action: tasks.ListTasksAction,
Before: bootstrap,
Flags: []cli.Flag{
verboseFlag,
cli.BoolFlag{
Name: tasks.FlagJSON,
Usage: "Print output in JSON format",
},
},
},
{
Name: "init",
Description: "Scaffolds a new gilbert.yaml file",
Usage: "Scaffolds a new gilbert.yaml file",
Action: scaffold.RunScaffoldManifest,
Before: bootstrap,
Flags: []cli.Flag{
verboseFlag,
},
},
{
Name: "clean",
Description: "Clean cached files and objects",
Usage: "Clean cached files and objects",
Action: maintenance.ClearCacheAction,
Before: bootstrap,
Flags: []cli.Flag{
verboseFlag,
maintenance.ClearAllFlag,
maintenance.ClearPluginsFlag,
},
},
}
err := app.Run(os.Args)
if err != nil {
color.Red("ERROR: %v", err)
os.Exit(1)
}
}
func bootstrap(c *cli.Context) error {
level := log.LevelInfo
if scope.Debug {
level = log.LevelDebug
}
noColor := c.Bool(FlagNoColor)
log.UseConsoleLogger(level, noColor)
return nil
}
func printVersion(_ *cli.Context) error {
fmt.Printf("Gilbert version %s (%s)\n", version, commit)
return nil
}