mirror of
https://github.com/caddyserver/caddy.git
synced 2026-01-17 01:30:34 +00:00
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
This commit is contained in:
committed by
GitHub
parent
8cb3cf540c
commit
258bc82b69
49
cmd/main.go
49
cmd/main.go
@@ -33,6 +33,7 @@ import (
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/certmagic"
|
||||
"github.com/spf13/pflag"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -58,35 +59,9 @@ func Main() {
|
||||
os.Args = append(os.Args, "help")
|
||||
}
|
||||
|
||||
subcommandName := os.Args[1]
|
||||
subcommand, ok := commands[subcommandName]
|
||||
if !ok {
|
||||
if strings.HasPrefix(os.Args[1], "-") {
|
||||
// user probably forgot to type the subcommand
|
||||
fmt.Println("[ERROR] first argument must be a subcommand; see 'caddy help'")
|
||||
} else {
|
||||
fmt.Printf("[ERROR] '%s' is not a recognized subcommand; see 'caddy help'\n", os.Args[1])
|
||||
}
|
||||
os.Exit(caddy.ExitCodeFailedStartup)
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fs := subcommand.Flags
|
||||
if fs == nil {
|
||||
fs = flag.NewFlagSet(subcommand.Name, flag.ExitOnError)
|
||||
}
|
||||
|
||||
err := fs.Parse(os.Args[2:])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(caddy.ExitCodeFailedStartup)
|
||||
}
|
||||
|
||||
exitCode, err := subcommand.Func(Flags{fs})
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: %v\n", subcommand.Name, err)
|
||||
}
|
||||
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
// handlePingbackConn reads from conn and ensures it matches
|
||||
@@ -280,7 +255,7 @@ func watchConfigFile(filename, adapterName string) {
|
||||
// Flags wraps a FlagSet so that typed values
|
||||
// from flags can be easily retrieved.
|
||||
type Flags struct {
|
||||
*flag.FlagSet
|
||||
*pflag.FlagSet
|
||||
}
|
||||
|
||||
// String returns the string representation of the
|
||||
@@ -326,22 +301,6 @@ func (f Flags) Duration(name string) time.Duration {
|
||||
return val
|
||||
}
|
||||
|
||||
// flagHelp returns the help text for fs.
|
||||
func flagHelp(fs *flag.FlagSet) string {
|
||||
if fs == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// temporarily redirect output
|
||||
out := fs.Output()
|
||||
defer fs.SetOutput(out)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
fs.SetOutput(buf)
|
||||
fs.PrintDefaults()
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func loadEnvFromFile(envFile string) error {
|
||||
file, err := os.Open(envFile)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user