diff options
author | yzongyue <yzongyue@gmail.com> | 2019-10-09 10:12:15 +0800 |
---|---|---|
committer | Matt Holt <mholt@users.noreply.github.com> | 2019-10-08 20:12:15 -0600 |
commit | 53dd600b4de0c2b169f1eb4450a0f02950912524 (patch) | |
tree | bd742c34cefeb3a20bdb132937853dd875454f4c | |
parent | ce1205239a7c2300932d0d6819085586660fcce9 (diff) |
cmd: Built-in commands all use RegisterCommand (#2794)
-rw-r--r-- | cmd/commandfuncs.go | 9 | ||||
-rw-r--r-- | cmd/commands.go | 60 |
2 files changed, 36 insertions, 33 deletions
diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go index 5fba368..ec9d974 100644 --- a/cmd/commandfuncs.go +++ b/cmd/commandfuncs.go @@ -28,6 +28,7 @@ import ( "os/exec" "reflect" "runtime/debug" + "sort" "strings" "github.com/caddyserver/caddy/v2" @@ -470,7 +471,13 @@ usage: commands: ` - for _, cmd := range commands { + keys := make([]string, 0, len(commands)) + for k := range commands { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + cmd := commands[k] short := strings.TrimSuffix(cmd.Short, ".") s += fmt.Sprintf(" %-15s %s\n", cmd.Name, short) } diff --git a/cmd/commands.go b/cmd/commands.go index 10fb3e7..db9848d 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -61,8 +61,17 @@ type Command struct { // any error that occurred. type CommandFunc func(Flags) (int, error) -var commands = map[string]Command{ - "start": { +var commands = make(map[string]Command) + +func init() { + RegisterCommand(Command{ + Name: "help", + Func: cmdHelp, + Usage: "<command>", + Short: "Shows help for a Caddy subcommand", + }) + + RegisterCommand(Command{ Name: "start", Func: cmdStart, Usage: "[--config <path> [[--adapter <name>]]", @@ -80,9 +89,9 @@ using 'caddy run' instead to keep it in the foreground.`, fs.String("adapter", "", "Name of config adapter to apply") return fs }(), - }, + }) - "run": { + RegisterCommand(Command{ Name: "run", Func: cmdRun, Usage: "[--config <path> [--adapter <name>]] [--environ]", @@ -116,9 +125,9 @@ not quit after printing, and can be useful for troubleshooting.`, fs.String("pingback", "", "Echo confirmation bytes to this address on success") return fs }(), - }, + }) - "stop": { + RegisterCommand(Command{ Name: "stop", Func: cmdStop, 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 or the /stop API endpoint. Note: this will stop any process named the same as the executable (os.Args[0]).`, - }, + }) - "reload": { + RegisterCommand(Command{ Name: "reload", Func: cmdReload, Usage: "--config <path> [--adapter <name>] [--address <interface>]", @@ -152,15 +161,15 @@ config file; otherwise the default is assumed.`, fs.String("address", "", "Address of the administration listener, if different from config") return fs }(), - }, + }) - "version": { + RegisterCommand(Command{ Name: "version", Func: cmdVersion, Short: "Prints the version", - }, + }) - "list-modules": { + RegisterCommand(Command{ Name: "list-modules", Func: cmdListModules, Usage: "[--versions]", @@ -170,15 +179,15 @@ config file; otherwise the default is assumed.`, fs.Bool("versions", false, "Print version information") return fs }(), - }, + }) - "environ": { + RegisterCommand(Command{ Name: "environ", Func: cmdEnviron, Short: "Prints the environment", - }, + }) - "adapt": { + RegisterCommand(Command{ Name: "adapt", Func: cmdAdaptConfig, Usage: "--config <path> [--adapter <name>] [--pretty] [--validate]", @@ -201,9 +210,9 @@ zero exit status will be returned.`, fs.Bool("validate", false, "Validate the output") return fs }(), - }, + }) - "validate": { + RegisterCommand(Command{ Name: "validate", Func: cmdValidateConfig, Usage: "--config <path> [--adapter <name>]", @@ -218,21 +227,8 @@ provisioning stages.`, fs.String("adapter", "", "Name of config adapter") return fs }(), - }, -} + }) -func init() { - // the help command is special in that its func - // refers to the commands map; thus, defining it - // inline with the commands map's initialization - // yields a compile-time error, so we have to - // define this command separately - commands["help"] = Command{ - Name: "help", - Func: cmdHelp, - Usage: "<command>", - Short: "Shows help for a Caddy subcommand", - } } // RegisterCommand registers the command cmd. |