Browse Source

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

master
yzongyue 5 years ago
committed by Matt Holt
parent
commit
53dd600b4d
  1. 9
      cmd/commandfuncs.go
  2. 60
      cmd/commands.go

9
cmd/commandfuncs.go

@ -28,6 +28,7 @@ import (
"os/exec" "os/exec"
"reflect" "reflect"
"runtime/debug" "runtime/debug"
"sort"
"strings" "strings"
"github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2"
@ -470,7 +471,13 @@ usage:
commands: 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, ".") short := strings.TrimSuffix(cmd.Short, ".")
s += fmt.Sprintf(" %-15s %s\n", cmd.Name, short) s += fmt.Sprintf(" %-15s %s\n", cmd.Name, short)
} }

60
cmd/commands.go

@ -61,8 +61,17 @@ type Command struct {
// any error that occurred. // any error that occurred.
type CommandFunc func(Flags) (int, error) type CommandFunc func(Flags) (int, error)
var commands = map[string]Command{ var commands = make(map[string]Command)
"start": {
func init() {
RegisterCommand(Command{
Name: "help",
Func: cmdHelp,
Usage: "<command>",
Short: "Shows help for a Caddy subcommand",
})
RegisterCommand(Command{
Name: "start", Name: "start",
Func: cmdStart, Func: cmdStart,
Usage: "[--config <path> [[--adapter <name>]]", 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") fs.String("adapter", "", "Name of config adapter to apply")
return fs return fs
}(), }(),
}, })
"run": { RegisterCommand(Command{
Name: "run", Name: "run",
Func: cmdRun, Func: cmdRun,
Usage: "[--config <path> [--adapter <name>]] [--environ]", 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") fs.String("pingback", "", "Echo confirmation bytes to this address on success")
return fs return fs
}(), }(),
}, })
"stop": { RegisterCommand(Command{
Name: "stop", Name: "stop",
Func: cmdStop, Func: cmdStop,
Short: "Gracefully stops the running Caddy process", 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. or the /stop API endpoint.
Note: this will stop any process named the same as the executable (os.Args[0]).`, Note: this will stop any process named the same as the executable (os.Args[0]).`,
}, })
"reload": { RegisterCommand(Command{
Name: "reload", Name: "reload",
Func: cmdReload, Func: cmdReload,
Usage: "--config <path> [--adapter <name>] [--address <interface>]", 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") fs.String("address", "", "Address of the administration listener, if different from config")
return fs return fs
}(), }(),
}, })
"version": { RegisterCommand(Command{
Name: "version", Name: "version",
Func: cmdVersion, Func: cmdVersion,
Short: "Prints the version", Short: "Prints the version",
}, })
"list-modules": { RegisterCommand(Command{
Name: "list-modules", Name: "list-modules",
Func: cmdListModules, Func: cmdListModules,
Usage: "[--versions]", Usage: "[--versions]",
@ -170,15 +179,15 @@ config file; otherwise the default is assumed.`,
fs.Bool("versions", false, "Print version information") fs.Bool("versions", false, "Print version information")
return fs return fs
}(), }(),
}, })
"environ": { RegisterCommand(Command{
Name: "environ", Name: "environ",
Func: cmdEnviron, Func: cmdEnviron,
Short: "Prints the environment", Short: "Prints the environment",
}, })
"adapt": { RegisterCommand(Command{
Name: "adapt", Name: "adapt",
Func: cmdAdaptConfig, Func: cmdAdaptConfig,
Usage: "--config <path> [--adapter <name>] [--pretty] [--validate]", Usage: "--config <path> [--adapter <name>] [--pretty] [--validate]",
@ -201,9 +210,9 @@ zero exit status will be returned.`,
fs.Bool("validate", false, "Validate the output") fs.Bool("validate", false, "Validate the output")
return fs return fs
}(), }(),
}, })
"validate": { RegisterCommand(Command{
Name: "validate", Name: "validate",
Func: cmdValidateConfig, Func: cmdValidateConfig,
Usage: "--config <path> [--adapter <name>]", Usage: "--config <path> [--adapter <name>]",
@ -218,21 +227,8 @@ provisioning stages.`,
fs.String("adapter", "", "Name of config adapter") fs.String("adapter", "", "Name of config adapter")
return fs 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. // RegisterCommand registers the command cmd.

Loading…
Cancel
Save