From 53dd600b4de0c2b169f1eb4450a0f02950912524 Mon Sep 17 00:00:00 2001 From: yzongyue Date: Wed, 9 Oct 2019 10:12:15 +0800 Subject: cmd: Built-in commands all use RegisterCommand (#2794) --- cmd/commands.go | 60 +++++++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 32 deletions(-) (limited to 'cmd/commands.go') 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: "", + Short: "Shows help for a Caddy subcommand", + }) + + RegisterCommand(Command{ Name: "start", Func: cmdStart, Usage: "[--config [[--adapter ]]", @@ -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 [--adapter ]] [--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 [--adapter ] [--address ]", @@ -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 [--adapter ] [--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 [--adapter ]", @@ -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: "", - Short: "Shows help for a Caddy subcommand", - } } // RegisterCommand registers the command cmd. -- cgit v1.2.3