From 7bff52a47f797e5d181ef7f9cb9ea7bcaf307980 Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Sat, 31 Dec 2022 17:53:49 -0600 Subject: [PATCH] tweak logging --- internal/registry/registry.go | 38 ++++++++++++++++------------------- pkg/command/value.go | 1 - 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/internal/registry/registry.go b/internal/registry/registry.go index f657abd..65348f4 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -20,6 +20,8 @@ import ( // ContextKeyRuntimeIndex is the string key used to store context in a cobra Command. const ContextKeyRuntimeIndex = "x-chinampa-runtime-index" +var log = logrus.WithField("chinampa", "registry") + var registry = &CommandRegistry{ kv: map[string]*command.Command{}, } @@ -36,7 +38,7 @@ type CommandRegistry struct { } func Register(cmd *command.Command) { - logrus.Debugf("Registering %s", cmd.FullName()) + log.Debugf("adding to registry: %s", cmd.FullName()) registry.kv[cmd.FullName()] = cmd } @@ -58,24 +60,22 @@ func CommandList() []*command.Command { } func Execute(version string) error { + log.Debug("starting execution") cmdRoot := command.Root ccRoot := newCobraRoot(command.Root) ccRoot.Annotations["version"] = version ccRoot.CompletionOptions.HiddenDefaultCmd = true ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet()) - ccRoot.SetHelpCommand(commands.Help) + ccRoot.AddCommand(commands.Help) ccRoot.AddCommand(commands.Version) ccRoot.AddCommand(commands.GenerateCompletions) for name, opt := range cmdRoot.Options { 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)) for _, cmd := range CommandList() { @@ -84,21 +84,17 @@ func Execute(version string) error { for idx, cp := range cmd.Path { if idx == len(cmd.Path)-1 { leaf := ToCobra(cmd, cmdRoot.Options) - logrus.Debugf("adding command %s to %s", leaf.Name(), container.Name()) container.AddCommand(leaf) + log.Debugf("cobra: %s => %s", leaf.Name(), container.CommandPath()) break } query := []string{cp} found := false - if cp == "help" && container == ccRoot { - container = commands.Help - } else { - for _, sub := range container.Commands() { - if sub.Name() == cp { - container = sub - found = true - } + for _, sub := range container.Commands() { + if sub.Name() == cp { + container = sub + found = true } } @@ -152,6 +148,7 @@ func Execute(version string) error { } Register(groupParent) cc.SetHelpFunc(groupParent.HelpRenderer(command.Options{})) + cc.SetHelpCommand(commands.Help) container.AddCommand(cc) container = cc } @@ -165,17 +162,16 @@ func Execute(version string) error { if err != nil { 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 { - logrus.Debugf("Chinampa found sub-command %s, of %s", sub.Name(), current.Name()) + log.Debugf("exec: found sub-command %s", sub.CommandPath()) current = sub } - logrus.Debugf("Chinampa is going to call command %s", current.Name()) + log.Debugf("exec: calling %s", current.CommandPath()) err = current.Execute() 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) } diff --git a/pkg/command/value.go b/pkg/command/value.go index 2cc4751..8bcd226 100644 --- a/pkg/command/value.go +++ b/pkg/command/value.go @@ -87,7 +87,6 @@ func (vs *ValueSource) Resolve(currentValue string) (values []string, flag cobra flag = cobra.ShellCompDirectiveDefault timeout := time.Duration(vs.Timeout) - logrus.Errorf("resolving: %s", vs) switch { case vs.Static != nil: values = *vs.Static