From cfaca91d06961e30b0132d4138fde182e368b6f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 24 May 2022 16:21:13 +0200 Subject: [PATCH] Fix --version flag printing to stdout --- pkg/cmd/root/help.go | 11 +++++++++++ pkg/cmd/root/root.go | 7 ++----- pkg/cmd/version/version.go | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/root/help.go b/pkg/cmd/root/help.go index c7940faf0..d6be5abea 100644 --- a/pkg/cmd/root/help.go +++ b/pkg/cmd/root/help.go @@ -82,6 +82,17 @@ func isRootCmd(command *cobra.Command) bool { } func rootHelpFunc(f *cmdutil.Factory, command *cobra.Command, args []string) { + if isRootCmd(command) { + if versionVal, err := command.Flags().GetBool("version"); err == nil && versionVal { + fmt.Fprint(f.IOStreams.Out, command.Annotations["versionInfo"]) + return + } else if err != nil { + fmt.Fprintln(f.IOStreams.ErrOut, err) + hasFailed = true + return + } + } + cs := f.IOStreams.ColorScheme() if isRootCmd(command.Parent()) && len(args) >= 2 && args[1] != "--help" && args[1] != "-h" { diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 4aa65c798..3c7aaf814 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -53,12 +53,14 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { "help:feedback": heredoc.Doc(` Open an issue using 'gh issue create -R github.com/cli/cli' `), + "versionInfo": versionCmd.Format(version, buildDate), }, } cmd.SetOut(f.IOStreams.ErrOut) // command usage summary and deprecation warnings cmd.SetErr(f.IOStreams.ErrOut) // error messages + cmd.Flags().Bool("version", false, "Show gh version") cmd.PersistentFlags().Bool("help", false, "Show help for command") cmd.SetHelpFunc(func(c *cobra.Command, args []string) { rootHelpFunc(f, c, args) @@ -68,11 +70,6 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command { }) cmd.SetFlagErrorFunc(rootFlagErrorFunc) - formattedVersion := versionCmd.Format(version, buildDate) - cmd.SetVersionTemplate(formattedVersion) - cmd.Version = formattedVersion - cmd.Flags().Bool("version", false, "Show gh version") - // Child commands cmd.AddCommand(versionCmd.NewCmdVersion(f, version, buildDate)) cmd.AddCommand(actionsCmd.NewCmdActions(f)) diff --git a/pkg/cmd/version/version.go b/pkg/cmd/version/version.go index ec1c1e597..11d4a0271 100644 --- a/pkg/cmd/version/version.go +++ b/pkg/cmd/version/version.go @@ -14,7 +14,7 @@ func NewCmdVersion(f *cmdutil.Factory, version, buildDate string) *cobra.Command Use: "version", Hidden: true, Run: func(cmd *cobra.Command, args []string) { - fmt.Fprint(f.IOStreams.Out, Format(version, buildDate)) + fmt.Fprint(f.IOStreams.Out, cmd.Root().Annotations["versionInfo"]) }, }