1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

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

This commit is contained in:
yzongyue 2019-10-09 10:12:15 +08:00 committed by Matt Holt
parent ce1205239a
commit 53dd600b4d
2 changed files with 36 additions and 33 deletions

View file

@ -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)
}

View file

@ -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.