2022-12-31 05:53:24 +00:00
|
|
|
// Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx>
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
_c "git.rob.mx/nidito/chinampa/internal/constants"
|
|
|
|
"git.rob.mx/nidito/chinampa/pkg/env"
|
|
|
|
"git.rob.mx/nidito/chinampa/pkg/runtime"
|
|
|
|
"git.rob.mx/nidito/chinampa/pkg/statuscode"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-03-20 01:18:10 +00:00
|
|
|
func cobraCommandFullName(c *cobra.Command) []string {
|
|
|
|
name := []string{c.Name()}
|
|
|
|
if c.HasParent() {
|
|
|
|
name = append(cobraCommandFullName(c.Parent()), name...)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2022-12-31 05:53:24 +00:00
|
|
|
var Help = &cobra.Command{
|
|
|
|
Use: _c.HelpCommandName + " [command]",
|
|
|
|
Short: "Display usage information for any command",
|
2023-03-24 01:51:36 +00:00
|
|
|
Long: `Help provides the valid arguments and options for any command known to ﹅@chinampa@﹅.
|
|
|
|
|
|
|
|
## 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﹅.`,
|
2022-12-31 05:53:24 +00:00
|
|
|
ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
var completions []string
|
|
|
|
cmd, _, e := c.Root().Find(args)
|
|
|
|
if e != nil {
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
if cmd == nil {
|
|
|
|
// Root help command.
|
|
|
|
cmd = c.Root()
|
|
|
|
}
|
|
|
|
for _, subCmd := range cmd.Commands() {
|
|
|
|
if subCmd.IsAvailableCommand() || subCmd.Name() == _c.HelpCommandName {
|
|
|
|
if strings.HasPrefix(subCmd.Name(), toComplete) {
|
|
|
|
completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return completions, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
},
|
|
|
|
Run: func(c *cobra.Command, args []string) {
|
2023-03-24 01:51:36 +00:00
|
|
|
c.Long = strings.ReplaceAll(c.Long, "@chinampa@", runtime.Executable)
|
2023-03-20 01:18:10 +00:00
|
|
|
if len(args) > 0 && c != nil && c.Name() != args[len(args)-1] {
|
|
|
|
c, topicArgs, err := c.Root().Find(args)
|
|
|
|
if err == nil && c != nil && len(topicArgs) == 0 {
|
|
|
|
// exact command help
|
|
|
|
cobra.CheckErr(c.Help())
|
|
|
|
os.Exit(statuscode.RenderHelp)
|
|
|
|
return
|
|
|
|
}
|
2022-12-31 05:53:24 +00:00
|
|
|
|
2023-03-20 01:18:10 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
os.Exit(statuscode.ProgrammerError)
|
|
|
|
}
|
2022-12-31 05:53:24 +00:00
|
|
|
|
2023-03-20 01:18:10 +00:00
|
|
|
fullName := strings.Join(cobraCommandFullName(c), " ")
|
|
|
|
cobra.CheckErr(c.Help())
|
|
|
|
if len(topicArgs) > 0 {
|
|
|
|
logrus.Errorf("Unknown help topic \"%s\" for %s", topicArgs[0], fullName)
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("Unknown help topic \"%s\"", args[0])
|
2022-12-31 05:53:24 +00:00
|
|
|
}
|
2023-03-20 01:18:10 +00:00
|
|
|
os.Exit(statuscode.NotFound)
|
2022-12-31 05:53:24 +00:00
|
|
|
} else {
|
2023-03-20 01:18:10 +00:00
|
|
|
c.InitDefaultHelpFlag() // make possible 'help' flag to be shown
|
|
|
|
cobra.CheckErr(c.Help())
|
2022-12-31 05:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(statuscode.RenderHelp)
|
|
|
|
},
|
|
|
|
}
|