Merge pull request #5703 from cli/version-flag-fix

Fix --version flag printing to stdout
This commit is contained in:
Mislav Marohnić 2022-05-24 17:06:32 +02:00 committed by GitHub
commit ec12d27db5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

View file

@ -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" {

View file

@ -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))

View file

@ -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"])
},
}