From 20814bccd9610ceeb9d60eeca0e42678cf487322 Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Sun, 22 Jan 2023 22:29:18 -0600 Subject: [PATCH] make version command name adjustable --- internal/commands/version.go | 3 ++- internal/constants/help.md | 2 +- internal/registry/registry.go | 9 +++++++-- main.go | 5 +++++ pkg/render/render.go | 1 + 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/commands/version.go b/internal/commands/version.go index 1261d34..0a09aa7 100644 --- a/internal/commands/version.go +++ b/internal/commands/version.go @@ -10,8 +10,9 @@ import ( "github.com/spf13/cobra" ) +var VersionCommandName = "version" var Version = &cobra.Command{ - Use: "version", + Use: VersionCommandName, Short: "Display program version", Hidden: false, DisableAutoGenTag: true, diff --git a/internal/constants/help.md b/internal/constants/help.md index 1aaed5b..69e36bf 100644 --- a/internal/constants/help.md +++ b/internal/constants/help.md @@ -35,7 +35,7 @@ description: {{ .Command.Short }} {{ range .Command.Commands -}} {{- if (or .IsAvailableCommand (eq .Name "help")) -}} - {{ if $hh -}} -[`{{ .Name }}`]({{.Name}}) +[`{{ .Name }}`]({{.Name}}/) {{- else -}} `{{ .Name }}` {{- end }} - {{.Short}} diff --git a/internal/registry/registry.go b/internal/registry/registry.go index 3605a52..ebb76a6 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -65,10 +65,15 @@ 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.AddCommand(commands.Version) + 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.GenerateCompletions) for name, opt := range cmdRoot.Options { diff --git a/main.go b/main.go index 23aa310..355e4ce 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package chinampa import ( + "git.rob.mx/nidito/chinampa/internal/commands" "git.rob.mx/nidito/chinampa/internal/registry" "git.rob.mx/nidito/chinampa/pkg/command" "git.rob.mx/nidito/chinampa/pkg/runtime" @@ -22,6 +23,10 @@ type Config struct { Description string } +func SetVersionCommandName(name string) { + commands.VersionCommandName = name +} + func SetErrorHandler(handlerFunc func(cmd *cobra.Command, err error) error) { registry.ErrorHandler = handlerFunc } diff --git a/pkg/render/render.go b/pkg/render/render.go index 5bcead2..29d5924 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -73,6 +73,7 @@ var TemplateFuncs = template.FuncMap{ "trim": strings.TrimSpace, "trimSuffix": strings.TrimSuffix, "trimPrefix": strings.TrimPrefix, + "list": func(args ...any) []any { return args }, } // TemplateCommandHelp holds a template for rendering command help.