-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
205 lines (188 loc) · 5.95 KB
/
cli.go
File metadata and controls
205 lines (188 loc) · 5.95 KB
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 (
"context"
"errors"
"fmt"
"net"
"os"
"slices"
"strconv"
"github.com/urfave/cli/v3"
)
const (
ShortUsage = "A web server for prototyping web interfaces from sketches"
LongDescription = `The firstgo server uses a configuration yaml file with templates in
assets/templates and css in assets/static to describe clickable zones
on images in assets/images to create an interactive website.`
)
// Applicator is an interface to the central coordinator for the project
// (concretely provided by App in app.go) to allow for testing.
type Applicator interface {
Serve(address, port, configFile string) error
Init(directory string) error
Demo(address, port string) error
ServeInDevelopment(address, port string, templateSuffixes []string, configFile string) error
}
// BuildCLI creates a cli app to run the capabilities provided by
// an Applicator dependency.
func BuildCLI(app Applicator) *cli.Command {
// Define the common flags.
addressFlag := &cli.StringFlag{
Name: "address",
Aliases: []string{"a"},
Value: "127.0.0.1",
Usage: "server network address",
}
portFlag := &cli.StringFlag{
Name: "port",
Aliases: []string{"p"},
Value: "8000",
Usage: "server network port",
}
serveCmd := &cli.Command{
Name: "serve",
Usage: "Serve content on disk",
ArgsUsage: "CONFIG_FILE",
// use the common flags
Flags: []cli.Flag{
addressFlag,
portFlag,
},
// Before runs verification before "Action" is run
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
if c.NArg() < 1 {
return ctx, fmt.Errorf("missing required argument: CONFIG_FILE")
}
configFile := c.Args().First()
if _, err := os.Stat(configFile); err != nil {
return ctx, fmt.Errorf("config file %q not found", configFile)
}
if a := net.ParseIP(c.String("address")); a == nil {
return ctx, fmt.Errorf("invalid IP address: %s", c.String("address"))
}
if _, err := strconv.Atoi(c.String("port")); err != nil {
return ctx, fmt.Errorf("invalid port: %s", c.String("port"))
}
return ctx, nil
},
Action: func(ctx context.Context, c *cli.Command) error {
configFile := c.Args().First()
return app.Serve(c.String("address"), c.String("port"), configFile)
},
}
serveInDevelopmentCmd := &cli.Command{
Name: "develop",
Usage: "Serve content on disk with automatic file reloads",
Description: `Presently only the yaml file, with a '.yaml' extension, together with
(by default) the files with a '.html' extension in templates are
automatically reloaded. The latter can be changed with -s flags.`,
ArgsUsage: "CONFIG_FILE",
// use common flags
Flags: []cli.Flag{
addressFlag,
portFlag,
&cli.StringSliceFlag{
Name: "suffix",
Aliases: []string{"s"},
Value: []string{"html"},
Usage: "template directory suffixes",
},
},
// Before runs verification before "Action" is run
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
if c.NArg() < 1 {
return ctx, fmt.Errorf("missing required argument: CONFIG_FILE")
}
configFile := c.Args().First()
if _, err := os.Stat(configFile); err != nil {
return ctx, fmt.Errorf("config file %q not found", configFile)
}
if a := net.ParseIP(c.String("address")); a == nil {
return ctx, fmt.Errorf("invalid IP address: %s", c.String("address"))
}
if _, err := strconv.Atoi(c.String("port")); err != nil {
return ctx, fmt.Errorf("invalid port: %s", c.String("port"))
}
if c.StringSlice("suffix") == nil {
return ctx, errors.New("no suffixes provided")
}
if slices.Contains(c.StringSlice("suffix"), "") {
return ctx, errors.New("empty suffix argument provided")
}
return ctx, nil
},
Action: func(ctx context.Context, c *cli.Command) error {
configFile := c.Args().First()
return app.ServeInDevelopment(c.String("address"), c.String("port"), c.StringSlice("suffix"), configFile)
},
}
initCmd := &cli.Command{
Name: "init",
Usage: "Initialize a new project from the embedded demo assets",
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
dir := c.String("directory")
d, err := os.Stat(dir)
if err != nil {
return ctx, fmt.Errorf("directory %q does not exist", dir)
}
if !d.IsDir() {
return ctx, fmt.Errorf("%q is not a directory", dir)
}
return ctx, nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "directory",
Aliases: []string{"d"},
Value: ".", // better than os.Getwd
Usage: "directory to write files",
},
},
Action: func(ctx context.Context, c *cli.Command) error {
return app.Init(c.String("directory"))
},
}
demoCmd := &cli.Command{
Name: "demo",
Usage: "Run the demo server with embedded assets",
EnableShellCompletion: true,
// use the common flags
Flags: []cli.Flag{
addressFlag,
portFlag,
},
// Repeat validation logic (consider sharing).
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
if a := net.ParseIP(c.String("address")); a == nil {
return ctx, fmt.Errorf("invalid IP address: %s", c.String("address"))
}
if _, err := strconv.Atoi(c.String("port")); err != nil {
return ctx, fmt.Errorf("invalid port: %s", c.String("port"))
}
return ctx, nil
},
Action: func(ctx context.Context, c *cli.Command) error {
return app.Demo(c.String("address"), c.String("port"))
},
}
rootCmd := &cli.Command{
Name: "firstgo",
Usage: ShortUsage,
Description: LongDescription,
Commands: []*cli.Command{demoCmd, initCmd, serveCmd, serveInDevelopmentCmd},
}
// custom help template.
rootCmd.CustomRootCommandHelpTemplate = rootHelpTemplate
return rootCmd
}
var rootHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} [command] [options]
DESCRIPTION:
{{.Description}}
COMMANDS:
{{range .Commands}} {{.Name}}{{ "\t"}}{{.Usage}}
{{end}}
Run '{{.Name}} [command] --help' for more information on a command.
`