tweak logging

This commit is contained in:
Roberto Hidalgo 2022-12-31 17:53:49 -06:00
parent 087c1cdfac
commit 7bff52a47f
2 changed files with 17 additions and 22 deletions

View File

@ -20,6 +20,8 @@ import (
// 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.
const ContextKeyRuntimeIndex = "x-chinampa-runtime-index" const ContextKeyRuntimeIndex = "x-chinampa-runtime-index"
var log = logrus.WithField("chinampa", "registry")
var registry = &CommandRegistry{ var registry = &CommandRegistry{
kv: map[string]*command.Command{}, kv: map[string]*command.Command{},
} }
@ -36,7 +38,7 @@ type CommandRegistry struct {
} }
func Register(cmd *command.Command) { func Register(cmd *command.Command) {
logrus.Debugf("Registering %s", cmd.FullName()) log.Debugf("adding to registry: %s", cmd.FullName())
registry.kv[cmd.FullName()] = cmd registry.kv[cmd.FullName()] = cmd
} }
@ -58,24 +60,22 @@ func CommandList() []*command.Command {
} }
func Execute(version string) error { func Execute(version string) error {
log.Debug("starting execution")
cmdRoot := command.Root cmdRoot := command.Root
ccRoot := newCobraRoot(command.Root) ccRoot := newCobraRoot(command.Root)
ccRoot.Annotations["version"] = version ccRoot.Annotations["version"] = version
ccRoot.CompletionOptions.HiddenDefaultCmd = true ccRoot.CompletionOptions.HiddenDefaultCmd = true
ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet()) ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet())
ccRoot.SetHelpCommand(commands.Help) ccRoot.AddCommand(commands.Help)
ccRoot.AddCommand(commands.Version) ccRoot.AddCommand(commands.Version)
ccRoot.AddCommand(commands.GenerateCompletions) ccRoot.AddCommand(commands.GenerateCompletions)
for name, opt := range cmdRoot.Options { for name, opt := range cmdRoot.Options {
if err := ccRoot.RegisterFlagCompletionFunc(name, opt.CompletionFunction); err != nil { if err := ccRoot.RegisterFlagCompletionFunc(name, opt.CompletionFunction); err != nil {
logrus.Errorf("Failed setting up autocompletion for option <%s> of command <%s>", name, cmdRoot.FullName()) log.Errorf("Failed setting up autocompletion for option <%s> of command <%s>", name, cmdRoot.FullName())
} }
} }
// ccRoot.SetHelpFunc(func(cc *cobra.Command, args []string) {
// cmdRoot.HelpRenderer(cmdRoot.Options)(cc, args)
// os.Exit(statuscode.RenderHelp)
// })
ccRoot.SetHelpFunc(cmdRoot.HelpRenderer(cmdRoot.Options)) ccRoot.SetHelpFunc(cmdRoot.HelpRenderer(cmdRoot.Options))
for _, cmd := range CommandList() { for _, cmd := range CommandList() {
@ -84,21 +84,17 @@ func Execute(version string) error {
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, cmdRoot.Options)
logrus.Debugf("adding command %s to %s", leaf.Name(), container.Name())
container.AddCommand(leaf) container.AddCommand(leaf)
log.Debugf("cobra: %s => %s", leaf.Name(), container.CommandPath())
break break
} }
query := []string{cp} query := []string{cp}
found := false found := false
if cp == "help" && container == ccRoot { for _, sub := range container.Commands() {
container = commands.Help if sub.Name() == cp {
} else { container = sub
for _, sub := range container.Commands() { found = true
if sub.Name() == cp {
container = sub
found = true
}
} }
} }
@ -152,6 +148,7 @@ func Execute(version string) error {
} }
Register(groupParent) Register(groupParent)
cc.SetHelpFunc(groupParent.HelpRenderer(command.Options{})) cc.SetHelpFunc(groupParent.HelpRenderer(command.Options{}))
cc.SetHelpCommand(commands.Help)
container.AddCommand(cc) container.AddCommand(cc)
container = cc container = cc
} }
@ -165,17 +162,16 @@ func Execute(version string) error {
if err != nil { if err != nil {
current = ccRoot current = ccRoot
} }
logrus.Debugf("Chinampa found command %s, remaining %s", current.Name(), remaining) log.Debugf("exec: found command %s with args: %s", current.CommandPath(), remaining)
// if current.HasSubCommands() && current.
if sub, _, err := current.Find(remaining); err == nil && sub != current { if sub, _, err := current.Find(remaining); err == nil && sub != current {
logrus.Debugf("Chinampa found sub-command %s, of %s", sub.Name(), current.Name()) log.Debugf("exec: found sub-command %s", sub.CommandPath())
current = sub current = sub
} }
logrus.Debugf("Chinampa is going to call command %s", current.Name()) log.Debugf("exec: calling %s", current.CommandPath())
err = current.Execute() err = current.Execute()
if err != nil { if err != nil {
logrus.Debugf("Chinampa found error calling command %s", current.Name()) log.Debugf("exec: error calling %s, %s", current.CommandPath(), err)
errors.HandleCobraExit(current, err) errors.HandleCobraExit(current, err)
} }

View File

@ -87,7 +87,6 @@ func (vs *ValueSource) Resolve(currentValue string) (values []string, flag cobra
flag = cobra.ShellCompDirectiveDefault flag = cobra.ShellCompDirectiveDefault
timeout := time.Duration(vs.Timeout) timeout := time.Duration(vs.Timeout)
logrus.Errorf("resolving: %s", vs)
switch { switch {
case vs.Static != nil: case vs.Static != nil:
values = *vs.Static values = *vs.Static