@marcel91200 / caddy Mirror · read-only / commits / 53dd600b
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

cmd: Built-in commands all use RegisterCommand (#2794)

yzongyue committed Oct 9, 2019 at 10:12 UTC 53dd600b4de0c2b169f1eb4450a0f02950912524
2 files changed +36 -33
cmd/commandfuncs.go
+8 -1
@@ -28,6 +28,7 @@ import (
28 "os/exec"
29 "reflect"
30 "runtime/debug"
31 + "sort"
32 "strings"
33
34 "github.com/caddyserver/caddy/v2"
@@ -470,7 +471,13 @@ usage:
471
472 commands:
473 `
473 - for _, cmd := range commands {
474 + keys := make([]string, 0, len(commands))
475 + for k := range commands {
476 + keys = append(keys, k)
477 + }
478 + sort.Strings(keys)
479 + for _, k := range keys {
480 + cmd := commands[k]
481 short := strings.TrimSuffix(cmd.Short, ".")
482 s += fmt.Sprintf(" %-15s %s\n", cmd.Name, short)
483 }
cmd/commands.go
+28 -32
@@ -61,8 +61,17 @@ type Command struct {
61 // any error that occurred.
62 type CommandFunc func(Flags) (int, error)
63
64 -var commands = map[string]Command{
65 - "start": {
64 +var commands = make(map[string]Command)
65 +
66 +func init() {
67 + RegisterCommand(Command{
68 + Name: "help",
69 + Func: cmdHelp,
70 + Usage: "<command>",
71 + Short: "Shows help for a Caddy subcommand",
72 + })
73 +
74 + RegisterCommand(Command{
75 Name: "start",
76 Func: cmdStart,
77 Usage: "[--config <path> [[--adapter <name>]]",
@@ -80,9 +89,9 @@ using 'caddy run' instead to keep it in the foreground.`,
89 fs.String("adapter", "", "Name of config adapter to apply")
90 return fs
91 }(),
83 - },
92 + })
93
85 - "run": {
94 + RegisterCommand(Command{
95 Name: "run",
96 Func: cmdRun,
97 Usage: "[--config <path> [--adapter <name>]] [--environ]",
@@ -116,9 +125,9 @@ not quit after printing, and can be useful for troubleshooting.`,
125 fs.String("pingback", "", "Echo confirmation bytes to this address on success")
126 return fs
127 }(),
119 - },
128 + })
129
121 - "stop": {
130 + RegisterCommand(Command{
131 Name: "stop",
132 Func: cmdStop,
133 Short: "Gracefully stops the running Caddy process",
@@ -130,9 +139,9 @@ clean up any active locks; for a graceful shutdown on Windows, use Ctrl+C
139 or the /stop API endpoint.
140
141 Note: this will stop any process named the same as the executable (os.Args[0]).`,
133 - },
142 + })
143
135 - "reload": {
144 + RegisterCommand(Command{
145 Name: "reload",
146 Func: cmdReload,
147 Usage: "--config <path> [--adapter <name>] [--address <interface>]",
@@ -152,15 +161,15 @@ config file; otherwise the default is assumed.`,
161 fs.String("address", "", "Address of the administration listener, if different from config")
162 return fs
163 }(),
155 - },
164 + })
165
157 - "version": {
166 + RegisterCommand(Command{
167 Name: "version",
168 Func: cmdVersion,
169 Short: "Prints the version",
161 - },
170 + })
171
163 - "list-modules": {
172 + RegisterCommand(Command{
173 Name: "list-modules",
174 Func: cmdListModules,
175 Usage: "[--versions]",
@@ -170,15 +179,15 @@ config file; otherwise the default is assumed.`,
179 fs.Bool("versions", false, "Print version information")
180 return fs
181 }(),
173 - },
182 + })
183
175 - "environ": {
184 + RegisterCommand(Command{
185 Name: "environ",
186 Func: cmdEnviron,
187 Short: "Prints the environment",
179 - },
188 + })
189
181 - "adapt": {
190 + RegisterCommand(Command{
191 Name: "adapt",
192 Func: cmdAdaptConfig,
193 Usage: "--config <path> [--adapter <name>] [--pretty] [--validate]",
@@ -201,9 +210,9 @@ zero exit status will be returned.`,
210 fs.Bool("validate", false, "Validate the output")
211 return fs
212 }(),
204 - },
213 + })
214
206 - "validate": {
215 + RegisterCommand(Command{
216 Name: "validate",
217 Func: cmdValidateConfig,
218 Usage: "--config <path> [--adapter <name>]",
@@ -218,21 +227,8 @@ provisioning stages.`,
227 fs.String("adapter", "", "Name of config adapter")
228 return fs
229 }(),
221 - },
222 -}
230 + })
231
224 -func init() {
225 - // the help command is special in that its func
226 - // refers to the commands map; thus, defining it
227 - // inline with the commands map's initialization
228 - // yields a compile-time error, so we have to
229 - // define this command separately
230 - commands["help"] = Command{
231 - Name: "help",
232 - Func: cmdHelp,
233 - Usage: "<command>",
234 - Short: "Shows help for a Caddy subcommand",
235 - }
232 }
233
234 // RegisterCommand registers the command cmd.