chinampa/internal/registry/cobra.go

125 lines
3.6 KiB
Go
Raw Permalink Normal View History

2022-12-19 03:04:34 +00:00
// Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx>
2022-12-31 05:53:24 +00:00
// SPDX-License-Identifier: Apache-2.0
2022-12-19 03:04:34 +00:00
package registry
import (
"fmt"
"strings"
"git.rob.mx/nidito/chinampa/pkg/command"
2022-12-31 05:53:24 +00:00
"git.rob.mx/nidito/chinampa/pkg/errors"
2022-12-19 03:04:34 +00:00
"git.rob.mx/nidito/chinampa/pkg/runtime"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
2022-12-29 19:05:58 +00:00
func newCobraRoot(root *command.Command) *cobra.Command {
return &cobra.Command{
Use: root.Name() + " [--silent|-v|--verbose] [--[no-]color] [-h|--help] [--version]",
Annotations: map[string]string{
2022-12-31 05:53:24 +00:00
ContextKeyRuntimeIndex: root.Name(),
2022-12-29 19:05:58 +00:00
},
Short: root.Summary,
Long: root.Description,
DisableAutoGenTag: true,
SilenceUsage: true,
SilenceErrors: true,
// This tricks cobra into erroring without a subcommand
ValidArgs: []string{""},
2022-12-29 19:05:58 +00:00
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.OnlyValidArgs(cmd, args); err != nil {
2022-12-29 19:05:58 +00:00
suggestions := []string{}
bold := color.New(color.Bold)
for _, l := range cmd.SuggestionsFor(args[len(args)-1]) {
suggestions = append(suggestions, bold.Sprint(l))
}
errMessage := fmt.Sprintf("Unknown subcommand %s", bold.Sprint(strings.Join(args, " ")))
if len(suggestions) > 0 {
errMessage += ". Perhaps you meant " + strings.Join(suggestions, ", ") + "?"
}
return errors.NotFound{Msg: errMessage, Group: []string{}}
2022-12-19 03:04:34 +00:00
}
2022-12-29 19:05:58 +00:00
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if ok, err := cmd.Flags().GetBool("version"); err == nil && ok {
_, err := cmd.OutOrStdout().Write([]byte(cmd.Root().Annotations["version"]))
return err
}
return errors.NotFound{Msg: "No subcommand provided", Group: []string{}}
2022-12-19 03:04:34 +00:00
}
2022-12-29 19:05:58 +00:00
return nil
},
}
2022-12-19 03:04:34 +00:00
}
2023-04-04 00:13:42 +00:00
func ToCobra(cmd *command.Command, globalOptions command.Options, parent *cobra.Command) *cobra.Command {
2022-12-19 03:04:34 +00:00
localName := cmd.Name()
useSpec := []string{localName, "[options]"}
for _, arg := range cmd.Arguments {
useSpec = append(useSpec, arg.ToDesc())
}
cc := &cobra.Command{
Use: strings.Join(useSpec, " "),
Short: cmd.Summary,
DisableAutoGenTag: true,
SilenceUsage: true,
SilenceErrors: true,
2022-12-31 05:53:24 +00:00
Hidden: cmd.Hidden,
2022-12-19 03:04:34 +00:00
Annotations: map[string]string{
2022-12-31 05:53:24 +00:00
ContextKeyRuntimeIndex: cmd.FullName(),
2022-12-19 03:04:34 +00:00
},
Args: func(cc *cobra.Command, supplied []string) error {
skipValidation, _ := cc.Flags().GetBool("skip-validation")
if !skipValidation && runtime.ValidationEnabled() {
if err := cmd.Arguments.Parse(supplied); err != nil {
return err
}
2022-12-19 03:04:34 +00:00
return cmd.Arguments.AreValid()
}
return nil
},
RunE: cmd.Run,
}
cc.SetFlagErrorFunc(func(c *cobra.Command, e error) error {
return errors.BadArguments{Msg: e.Error()}
})
cc.ValidArgsFunction = cmd.Arguments.CompletionFunction
cc.Flags().AddFlagSet(cmd.FlagSet())
for name, opt := range cmd.Options {
if err := cc.RegisterFlagCompletionFunc(name, opt.CompletionFunction); err != nil {
2023-03-20 06:15:53 +00:00
log.Errorf("Failed setting up autocompletion for option <%s> of command <%s>", name, cmd.FullName())
2022-12-19 03:04:34 +00:00
}
}
2023-04-04 00:13:42 +00:00
parent.AddCommand(cc)
2022-12-19 03:04:34 +00:00
2023-04-04 00:13:42 +00:00
cmdGlobalOptions := globalOptions
if parent != cc.Root() {
cmdGlobalOptions = subOptions(globalOptions)
log.Tracef("Adding subflags from %s to child %s", parent.Name(), cmd.FullName())
if p := FromCobra(parent); p != nil {
for key, opt := range p.Options {
cmdGlobalOptions[key] = opt
}
}
}
cc.SetHelpFunc(cmd.HelpRenderer(cmdGlobalOptions))
2022-12-19 03:04:34 +00:00
cmd.SetCobra(cc)
return cc
}
2022-12-31 05:53:24 +00:00
func FromCobra(cc *cobra.Command) *command.Command {
rtidx, hasAnnotation := cc.Annotations[ContextKeyRuntimeIndex]
2022-12-19 03:04:34 +00:00
if hasAnnotation {
return Get(rtidx)
}
return nil
}