Skip to content

Add version flag #298

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pkg/cmd/root/help.go
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
@@ -117,5 +117,7 @@ func NewCmdRoot(f factory.Factory, clientFactory apiclient.ClientFactory, askPro
}
}

cmd.Flags().Bool(constants.FlagVersion, false, "Show version")

return cmd
}
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ const (

// flags for command line switches
const (
FlagVersion = "version"
FlagHelp = "help"
FlagSpace = "space"
FlagOutputFormat = "output-format"