From f3268240184a7e77756cc6b346dc030cbd513923 Mon Sep 17 00:00:00 2001 From: Travis Hampton Date: Wed, 29 Nov 2023 13:52:25 -0600 Subject: [PATCH] Add version flag ``` ./octopus --version ``` Fixes https://github.com/OctopusDeploy/cli/issues/269 --- pkg/cmd/root/help.go | 17 ++++++++++++++++- pkg/cmd/root/root.go | 2 ++ pkg/constants/constants.go | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/root/help.go b/pkg/cmd/root/help.go index 913f0678..bf009059 100644 --- a/pkg/cmd/root/help.go +++ b/pkg/cmd/root/help.go @@ -6,6 +6,7 @@ import ( "regexp" "strings" + version "github.com/OctopusDeploy/cli" "github.com/OctopusDeploy/cli/pkg/constants" "github.com/OctopusDeploy/cli/pkg/constants/annotations" "github.com/OctopusDeploy/cli/pkg/output" @@ -24,6 +25,20 @@ func rootHelpFunc(cmd *cobra.Command, _ []string) { libraryCmds := []string{} infrastructureCmds := []string{} additionalCmds := []string{} + + flags := cmd.Flags() + + if isRootCmd(cmd) { + out := cmd.OutOrStdout() + if versionVal, err := flags.GetBool(constants.FlagHelp); err == nil && versionVal { + fmt.Fprintln(out, strings.TrimSpace(version.Version)) + return + } else if err != nil { + fmt.Fprintln(out, err) + return + } + } + for _, c := range cmd.Commands() { if c.Short == "" { continue @@ -119,7 +134,7 @@ func rootHelpFunc(cmd *cobra.Command, _ []string) { } func calculatePadding(cmd *cobra.Command) int { - namePadding:= 12 + namePadding := 12 for _, c := range cmd.Commands() { if len(c.Name()) > namePadding { namePadding = len(c.Name()) + 2 diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 6124cb15..6daa4c14 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -117,5 +117,7 @@ func NewCmdRoot(f factory.Factory, clientFactory apiclient.ClientFactory, askPro } } + cmd.Flags().Bool(constants.FlagVersion, false, "Show version") + return cmd } diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 4aff714b..7be0bd47 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -6,6 +6,7 @@ const ( // flags for command line switches const ( + FlagVersion = "version" FlagHelp = "help" FlagSpace = "space" FlagOutputFormat = "output-format"