Skip to content

Commit

Permalink
cmd: Expand cobra support, add short flags (#5379)
Browse files Browse the repository at this point in the history
* cmd: Expand cobra support

* Convert commands to cobra, add short flags

* Fix version command typo

Co-authored-by: Emily Lange <[email protected]>

* Apply suggestions from code review

Co-authored-by: Matt Holt <[email protected]>

---------

Co-authored-by: Emily Lange <[email protected]>
Co-authored-by: Matt Holt <[email protected]>
  • Loading branch information
3 people authored Feb 24, 2023
1 parent 167981d commit 9e69195
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 209 deletions.
21 changes: 15 additions & 6 deletions cmd/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,21 @@ func caddyCmdToCobra(caddyCmd Command) *cobra.Command {
Use: caddyCmd.Name,
Short: caddyCmd.Short,
Long: caddyCmd.Long,
RunE: func(cmd *cobra.Command, _ []string) error {
fls := cmd.Flags()
_, err := caddyCmd.Func(Flags{fls})
return err
},
}
cmd.Flags().AddGoFlagSet(caddyCmd.Flags)
if caddyCmd.CobraFunc != nil {
caddyCmd.CobraFunc(cmd)
} else {
cmd.RunE = WrapCommandFuncForCobra(caddyCmd.Func)
cmd.Flags().AddGoFlagSet(caddyCmd.Flags)
}
return cmd
}

// WrapCommandFuncForCobra wraps a Caddy CommandFunc for use
// in a cobra command's RunE field.
func WrapCommandFuncForCobra(f CommandFunc) func(cmd *cobra.Command, _ []string) error {
return func(cmd *cobra.Command, _ []string) error {
_, err := f(Flags{cmd.Flags()})
return err
}
}
Loading

0 comments on commit 9e69195

Please sign in to comment.