make version command name adjustable
This commit is contained in:
parent
f0a8223e8d
commit
20814bccd9
@ -10,8 +10,9 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var VersionCommandName = "version"
|
||||||
var Version = &cobra.Command{
|
var Version = &cobra.Command{
|
||||||
Use: "version",
|
Use: VersionCommandName,
|
||||||
Short: "Display program version",
|
Short: "Display program version",
|
||||||
Hidden: false,
|
Hidden: false,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
|
@ -35,7 +35,7 @@ description: {{ .Command.Short }}
|
|||||||
{{ range .Command.Commands -}}
|
{{ range .Command.Commands -}}
|
||||||
{{- if (or .IsAvailableCommand (eq .Name "help")) -}}
|
{{- if (or .IsAvailableCommand (eq .Name "help")) -}}
|
||||||
- {{ if $hh -}}
|
- {{ if $hh -}}
|
||||||
[`{{ .Name }}`]({{.Name}})
|
[`{{ .Name }}`]({{.Name}}/)
|
||||||
{{- else -}}
|
{{- else -}}
|
||||||
`{{ .Name }}`
|
`{{ .Name }}`
|
||||||
{{- end }} - {{.Short}}
|
{{- end }} - {{.Short}}
|
||||||
|
@ -65,10 +65,15 @@ func Execute(version string) error {
|
|||||||
log.Debug("starting execution")
|
log.Debug("starting execution")
|
||||||
cmdRoot := command.Root
|
cmdRoot := command.Root
|
||||||
ccRoot := newCobraRoot(command.Root)
|
ccRoot := newCobraRoot(command.Root)
|
||||||
ccRoot.Annotations["version"] = version
|
|
||||||
ccRoot.CompletionOptions.HiddenDefaultCmd = true
|
ccRoot.CompletionOptions.HiddenDefaultCmd = true
|
||||||
ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet())
|
ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet())
|
||||||
|
if version != "" {
|
||||||
|
name := commands.VersionCommandName
|
||||||
|
ccRoot.Annotations["version"] = version
|
||||||
|
commands.Version.Hidden = strings.HasPrefix(name, "_")
|
||||||
|
commands.Version.Use = name
|
||||||
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 {
|
||||||
|
5
main.go
5
main.go
@ -3,6 +3,7 @@
|
|||||||
package chinampa
|
package chinampa
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.rob.mx/nidito/chinampa/internal/commands"
|
||||||
"git.rob.mx/nidito/chinampa/internal/registry"
|
"git.rob.mx/nidito/chinampa/internal/registry"
|
||||||
"git.rob.mx/nidito/chinampa/pkg/command"
|
"git.rob.mx/nidito/chinampa/pkg/command"
|
||||||
"git.rob.mx/nidito/chinampa/pkg/runtime"
|
"git.rob.mx/nidito/chinampa/pkg/runtime"
|
||||||
@ -22,6 +23,10 @@ type Config struct {
|
|||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetVersionCommandName(name string) {
|
||||||
|
commands.VersionCommandName = name
|
||||||
|
}
|
||||||
|
|
||||||
func SetErrorHandler(handlerFunc func(cmd *cobra.Command, err error) error) {
|
func SetErrorHandler(handlerFunc func(cmd *cobra.Command, err error) error) {
|
||||||
registry.ErrorHandler = handlerFunc
|
registry.ErrorHandler = handlerFunc
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,7 @@ var TemplateFuncs = template.FuncMap{
|
|||||||
"trim": strings.TrimSpace,
|
"trim": strings.TrimSpace,
|
||||||
"trimSuffix": strings.TrimSuffix,
|
"trimSuffix": strings.TrimSuffix,
|
||||||
"trimPrefix": strings.TrimPrefix,
|
"trimPrefix": strings.TrimPrefix,
|
||||||
|
"list": func(args ...any) []any { return args },
|
||||||
}
|
}
|
||||||
|
|
||||||
// TemplateCommandHelp holds a template for rendering command help.
|
// TemplateCommandHelp holds a template for rendering command help.
|
||||||
|
Loading…
Reference in New Issue
Block a user