diff options
author | Mohammed Al Sahaf <msaa1990@gmail.com> | 2022-08-31 01:38:38 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-30 22:38:38 +0000 |
commit | 258bc82b69ccb0f514fc62ec8ecd7273458ab2e4 (patch) | |
tree | 201b76cd74ce0809f6b3061af4c145759a6e483f /cmd/commands.go | |
parent | 8cb3cf540c2d083721a1717b9ddf2657b7eb5102 (diff) |
cmd: Migrate to `spf13/cobra`, remove single-dash arg support (#4565)
* cmd: migrate to spf13/cobra
* add `manpage` command
* limit Caddy tagline to root `help` only
* hard-code the manpage section to 8
Diffstat (limited to 'cmd/commands.go')
-rw-r--r-- | cmd/commands.go | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/cmd/commands.go b/cmd/commands.go index e454f7b..2085dfc 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -16,7 +16,13 @@ package caddycmd import ( "flag" + "fmt" + "os" "regexp" + "strings" + + "github.com/caddyserver/caddy/v2" + "github.com/spf13/cobra/doc" ) // Command represents a subcommand. Name, Func, @@ -71,13 +77,6 @@ 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>]] [--envfile <path>] [--watch] [--pidfile <file>]", @@ -346,16 +345,50 @@ EXPERIMENTAL: May be changed or removed. }(), }) + RegisterCommand(Command{ + Name: "manpage", + Func: func(fl Flags) (int, error) { + dir := strings.TrimSpace(fl.String("directory")) + if dir == "" { + return caddy.ExitCodeFailedQuit, fmt.Errorf("designated output directory and specified section are required") + } + if err := os.MkdirAll(dir, 0755); err != nil { + return caddy.ExitCodeFailedQuit, err + } + if err := doc.GenManTree(rootCmd, &doc.GenManHeader{ + Title: "Caddy", + Section: "8", + }, dir); err != nil { + return caddy.ExitCodeFailedQuit, err + } + return caddy.ExitCodeSuccess, nil + }, + Usage: "--directory <directory path>", + Short: "Generates the manual pages of Caddy commands", + Long: ` +Generates the manual pages of Caddy commands into the designated directory tagged into the specified section. + +The manual page files are generated into the directory specified by the argument of --directory. If the directory does not exist, it will be created. + +The manual pages are sorted into the section specified by the argument of --section. +`, + Flags: func() *flag.FlagSet { + fs := flag.NewFlagSet("manpage", flag.ExitOnError) + fs.String("directory", "", "The output directory where the manpages are generated") + fs.String("section", "", "The section number of the generated manual pages") + return fs + }(), + }) } // RegisterCommand registers the command cmd. // cmd.Name must be unique and conform to the // following format: // -// - lowercase -// - alphanumeric and hyphen characters only -// - cannot start or end with a hyphen -// - hyphen cannot be adjacent to another hyphen +// - lowercase +// - alphanumeric and hyphen characters only +// - cannot start or end with a hyphen +// - hyphen cannot be adjacent to another hyphen // // This function panics if the name is already registered, // if the name does not meet the described format, or if @@ -378,7 +411,7 @@ func RegisterCommand(cmd Command) { if !commandNameRegex.MatchString(cmd.Name) { panic("invalid command name") } - commands[cmd.Name] = cmd + rootCmd.AddCommand(caddyCmdToCoral(cmd)) } var commandNameRegex = regexp.MustCompile(`^[a-z0-9]$|^([a-z0-9]+-?[a-z0-9]*)+[a-z0-9]$`) |