-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
205 lines (191 loc) · 5.03 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
package main
import (
"fmt"
"os"
"executor/internal/commands"
"executor/internal/config"
"executor/internal/terminal"
"github.com/urfave/cli/v2"
)
type Output string
func (o Output) String() string {
return string(o)
}
const (
whichCommandName = "which"
runCommandName = "run"
portCommandName = "port"
webCommandName = "web"
)
var version string
func main() {
app := &cli.App{
Name: "executor",
Usage: "Execute commands in fancy way",
Version: version,
DefaultCommand: runCommandName,
CommandNotFound: func(c *cli.Context, command string) {
terminal.Error(fmt.Errorf("command not found: %s", command))
terminal.CleanUp()
os.Exit(1)
},
Commands: []*cli.Command{
{
Name: whichCommandName,
Usage: "Check if a command exists in the system path",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "silent",
Aliases: []string{"s"},
Usage: "Silent if command is found",
Value: false,
},
&cli.BoolFlag{
Name: "no-color",
Aliases: []string{"nc"},
Value: false,
Usage: "Disable color output and spinner",
HasBeenSet: true,
},
&cli.BoolFlag{
Name: "show-config",
Aliases: []string{"sc"},
Usage: "Show config before start",
Value: false,
},
&cli.StringFlag{
Name: "cmd",
Aliases: []string{"c"},
Usage: "Command to check",
},
&cli.StringFlag{
Name: "not-found-msg",
Aliases: []string{"m"},
Usage: "Text to show if command not found, typically some install hint",
Value: "Command not found, please install it now.",
},
},
Action: newActionFunc(commands.Which),
},
{
Name: runCommandName,
Usage: "Run a command",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "desc",
Aliases: []string{"d"},
Usage: "Command description",
Required: true,
},
&cli.BoolFlag{
Name: "show-env",
Aliases: []string{"se"},
Usage: "Show enviroment before start",
Value: false,
},
&cli.BoolFlag{
Name: "show-config",
Aliases: []string{"sc"},
Usage: "Show config before start",
},
&cli.StringFlag{
Name: "spinner-style",
Aliases: []string{"st"},
Usage: "Spinner style: " + terminal.SpinnerStylesString(),
Value: "dots",
},
&cli.BoolFlag{
Name: "show-output",
Aliases: []string{"so"},
Usage: "Show command output when command it's successful",
Value: false,
},
&cli.BoolFlag{
Name: "show-output-on-error",
Aliases: []string{"soe"},
Usage: "Set false to hide command output when command it's not successful",
Value: true,
},
&cli.StringFlag{
Name: "env-file",
Aliases: []string{"n"},
Usage: "Enviroment file ('none' to disable)",
Value: ".env",
},
&cli.IntFlag{
Name: "env-recurse-levels",
Aliases: []string{
"r",
},
Usage: "How many levels we should recurse back looking for the env file, if its not an absolute path",
Value: 5,
},
&cli.BoolFlag{
Name: "no-color",
Aliases: []string{"nc"},
Value: false,
Usage: "Disable color output and spinner",
HasBeenSet: true,
},
&cli.StringFlag{
Name: "cmd",
Aliases: []string{"c"},
Usage: "Command to run",
Required: true,
},
},
Action: newActionFunc(commands.Run),
},
{
Name: portCommandName,
Usage: "Check if a port is open",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "desc",
Aliases: []string{"d"},
Usage: "Port check description",
Required: true,
},
&cli.StringFlag{Name: "host", Aliases: []string{"i"}, Usage: "Host to check", Value: "localhost"},
&cli.IntFlag{Name: "port", Aliases: []string{"p"}, Usage: "Port to check", Required: true},
&cli.IntFlag{Name: "timeout", Aliases: []string{"t"}, Usage: "Timeout in seconds", Value: 5},
},
Action: newActionFunc(commands.Port),
},
{
Name: webCommandName,
Usage: "Check if a web page is running and responding successfully",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "desc",
Aliases: []string{"d"},
Usage: "URL check description",
Required: true,
},
&cli.StringFlag{Name: "url", Aliases: []string{"u"}, Usage: "URL to check", Required: true},
&cli.IntFlag{Name: "timeout", Aliases: []string{"t"}, Usage: "Timeout in seconds", Value: 5},
},
Action: newActionFunc(commands.Web),
},
},
}
defer terminal.CleanUp()
err := app.Run(os.Args)
if err != nil {
fmt.Println()
terminal.Error(err)
fmt.Println()
terminal.CleanUp()
os.Exit(1)
}
}
type actionFunc func(cfg *config.Config) error
func newActionFunc(fn actionFunc) cli.ActionFunc {
return func(c *cli.Context) error {
cfg := config.New(c)
if c.Bool("show-config") {
cfg.Print()
}
return fn(cfg)
}
}