remove version from global options

This commit is contained in:
Roberto Hidalgo 2023-03-24 00:44:47 -06:00
parent 6ec1c56f0f
commit fef0a55288
4 changed files with 37 additions and 30 deletions

View File

@ -9,7 +9,6 @@ import (
_c "git.rob.mx/nidito/chinampa/internal/constants" _c "git.rob.mx/nidito/chinampa/internal/constants"
"git.rob.mx/nidito/chinampa/pkg/env" "git.rob.mx/nidito/chinampa/pkg/env"
"git.rob.mx/nidito/chinampa/pkg/runtime"
"git.rob.mx/nidito/chinampa/pkg/statuscode" "git.rob.mx/nidito/chinampa/pkg/statuscode"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -24,13 +23,13 @@ func cobraCommandFullName(c *cobra.Command) []string {
} }
var Help = &cobra.Command{ var Help = &cobra.Command{
Use: _c.HelpCommandName + " [command]", Use: _c.HelpCommandName,
Short: "Display usage information for any command", Short: "Display usage information for any command",
Long: `Help provides the valid arguments and options for any command known to @chinampa@. Long: `Provides documentation for any command known to @chinampa@, including its known arguments and options.
## Colorized output ### Colorized output
By default, and unless ` + env.NoColor + ` is set, @chinampa@ help will query the environment variable COLORFGBG to decide which style to use when rendering help, unless if ` + env.HelpStyle + ` is set to any of the following values: **light**, **dark**, **markdown**, and **auto**. 24-bit color is available when COLORTERM is set to truecolor.`, By default, and unless ` + env.NoColor + ` is set, @chinampa@ help will query the environment variable COLORFGBG to decide which style to use when rendering help, unless if ` + env.HelpStyle + ` is set to any of the following values: **light**, **dark**, **markdown**, or **auto**. 24-bit color is available when COLORTERM is set to truecolor.`,
ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var completions []string var completions []string
cmd, _, e := c.Root().Find(args) cmd, _, e := c.Root().Find(args)
@ -51,7 +50,6 @@ By default, and unless ﹅` + env.NoColor + `﹅ is set, ﹅@chinampa@ help﹅ w
return completions, cobra.ShellCompDirectiveNoFileComp return completions, cobra.ShellCompDirectiveNoFileComp
}, },
Run: func(c *cobra.Command, args []string) { Run: func(c *cobra.Command, args []string) {
c.Long = strings.ReplaceAll(c.Long, "@chinampa@", runtime.Executable)
if len(args) > 0 && c != nil && c.Name() != args[len(args)-1] { if len(args) > 0 && c != nil && c.Name() != args[len(args)-1] {
c, topicArgs, err := c.Root().Find(args) c, topicArgs, err := c.Root().Find(args)
if err == nil && c != nil && len(topicArgs) == 0 { if err == nil && c != nil && len(topicArgs) == 0 {

View File

@ -12,8 +12,10 @@ import (
"git.rob.mx/nidito/chinampa/pkg/command" "git.rob.mx/nidito/chinampa/pkg/command"
"git.rob.mx/nidito/chinampa/pkg/errors" "git.rob.mx/nidito/chinampa/pkg/errors"
"git.rob.mx/nidito/chinampa/pkg/logger" "git.rob.mx/nidito/chinampa/pkg/logger"
"git.rob.mx/nidito/chinampa/pkg/runtime"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
) )
// ContextKeyRuntimeIndex is the string key used to store context in a cobra Command. // ContextKeyRuntimeIndex is the string key used to store context in a cobra Command.
@ -65,7 +67,21 @@ func Execute(version string) error {
cmdRoot := command.Root cmdRoot := command.Root
ccRoot := newCobraRoot(command.Root) ccRoot := newCobraRoot(command.Root)
ccRoot.CompletionOptions.HiddenDefaultCmd = true ccRoot.CompletionOptions.HiddenDefaultCmd = true
ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet()) globalOptions := command.Options{}
cmdRoot.FlagSet().VisitAll(func(f *pflag.Flag) {
opt := command.Root.Options[f.Name]
if f.Name == "version" {
ccRoot.Flags().AddFlag(f)
} else {
ccRoot.PersistentFlags().AddFlag(f)
globalOptions[f.Name] = opt
}
if err := ccRoot.RegisterFlagCompletionFunc(f.Name, opt.CompletionFunction); err != nil {
log.Errorf("Failed setting up autocompletion for option <%s> of command <%s>", f.Name, cmdRoot.FullName())
}
})
if version != "" { if version != "" {
name := commands.VersionCommandName name := commands.VersionCommandName
ccRoot.Annotations["version"] = version ccRoot.Annotations["version"] = version
@ -75,20 +91,13 @@ func Execute(version string) error {
} }
ccRoot.AddCommand(commands.GenerateCompletions) ccRoot.AddCommand(commands.GenerateCompletions)
for name, opt := range cmdRoot.Options { ccRoot.SetHelpFunc(cmdRoot.HelpRenderer(globalOptions))
if err := ccRoot.RegisterFlagCompletionFunc(name, opt.CompletionFunction); err != nil {
log.Errorf("Failed setting up autocompletion for option <%s> of command <%s>", name, cmdRoot.FullName())
}
}
ccRoot.SetHelpFunc(cmdRoot.HelpRenderer(cmdRoot.Options))
for _, cmd := range CommandList() { for _, cmd := range CommandList() {
cmd := cmd cmd := cmd
container := ccRoot container := ccRoot
for idx, cp := range cmd.Path { for idx, cp := range cmd.Path {
if idx == len(cmd.Path)-1 { if idx == len(cmd.Path)-1 {
leaf := ToCobra(cmd, cmdRoot.Options) leaf := ToCobra(cmd, globalOptions)
container.AddCommand(leaf) container.AddCommand(leaf)
log.Tracef("cobra: %s => %s", leaf.Name(), container.CommandPath()) log.Tracef("cobra: %s => %s", leaf.Name(), container.CommandPath())
break break
@ -160,7 +169,7 @@ func Execute(version string) error {
Options: command.Options{}, Options: command.Options{},
} }
Register(groupParent) Register(groupParent)
cc.SetHelpFunc(groupParent.HelpRenderer(command.Options{})) cc.SetHelpFunc(groupParent.HelpRenderer(globalOptions))
cc.SetHelpCommand(commands.Help) cc.SetHelpCommand(commands.Help)
container.AddCommand(cc) container.AddCommand(cc)
container = cc container = cc
@ -170,6 +179,7 @@ func Execute(version string) error {
cmd.Path = append(cmdRoot.Path, cmd.Path...) cmd.Path = append(cmdRoot.Path, cmd.Path...)
} }
cmdRoot.SetCobra(ccRoot) cmdRoot.SetCobra(ccRoot)
commands.Help.Long = strings.ReplaceAll(commands.Help.Long, "@chinampa@", runtime.Executable)
ccRoot.SetHelpCommand(commands.Help) ccRoot.SetHelpCommand(commands.Help)
current, remaining, err := ccRoot.Find(os.Args[1:]) current, remaining, err := ccRoot.Find(os.Args[1:])

View File

@ -112,7 +112,6 @@ func (cmd *Command) FlagSet() *pflag.FlagSet {
continue continue
} }
} }
cmd.runtimeFlags = fs cmd.runtimeFlags = fs
} }
return cmd.runtimeFlags return cmd.runtimeFlags

View File

@ -17,26 +17,21 @@ var Root = &Command{
Type: "bool", Type: "bool",
Description: "Display help for any command", Description: "Display help for any command",
}, },
"verbose": &Option{ "color": &Option{
ShortName: "v",
Type: "bool", Type: "bool",
Default: runtime.VerboseEnabled(), Description: "Always print colors to stderr",
Description: "Log verbose output to stderr", Default: runtime.ColorEnabled(),
},
"version": &Option{
Type: "bool",
Default: false,
Description: "Display program version and exit",
}, },
"no-color": &Option{ "no-color": &Option{
Type: "bool", Type: "bool",
Description: "Disable printing of colors to stderr", Description: "Disable printing of colors to stderr",
Default: !runtime.ColorEnabled(), Default: !runtime.ColorEnabled(),
}, },
"color": &Option{ "verbose": &Option{
ShortName: "v",
Type: "bool", Type: "bool",
Description: "Always print colors to stderr", Default: runtime.VerboseEnabled(),
Default: runtime.ColorEnabled(), Description: "Log verbose output to stderr",
}, },
"silent": &Option{ "silent": &Option{
Type: "bool", Type: "bool",
@ -46,5 +41,10 @@ var Root = &Command{
Type: "bool", Type: "bool",
Description: "Do not validate any arguments or options", Description: "Do not validate any arguments or options",
}, },
"version": &Option{
Type: "bool",
Default: false,
Description: "Display program version and exit",
},
}, },
} }