Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

PersistentPreRun was only executed if the command has a Run function #2039

Closed
haoming29 opened this issue Oct 5, 2023 · 2 comments
Closed

Comments

@haoming29
Copy link
Contributor

haoming29 commented Oct 5, 2023

I'm not sure if this is intentional but if the command does not have a Run function set up, the PersistentPreRun function will not be triggered on this command.

I had this issue where I don't need to Run anything for a bunch of my subcommands but I still want to be able to show --version for the subcommand via PersistentPreRun.

If this is intentional, can we update the user guide to reflect this? If not, I can try to fix this issue. Just spent hours debugging until realized might have something to do with Run.

Steps to reproduce

  • Setup the project with go mod init example-project

  • Setup external modules by go mod tidy

  • Get the cobra package

  • Build and run

  • Run ./executable --version will print out

PersistentPreRun in rootcmd runs
inside the flag
  • Run ./executable child --version will print out
PersistentPreRun in rootcmd runs
inside the flag
  • Run ./executable grandchild --version will print out
PersistentPreRun in rootcmd runs
inside the flag

However, if you comment out any of the Run function from any of the command, the output will be the content from --help

Hugo child is a very fast static site generator

Usage:

Flags:
  -h, --help   help for child

Global Flags:
  -v, --version   Show version

Additional help topics:
  hugo child grandchild Hugo grand child is a very fast static site generator

Code to reproduce

package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var showVersion bool

var rootCmd = &cobra.Command{
	Use:   "hugo",
	Short: "Hugo is a very fast static site generator",
	Long: `A Fast and Flexible Static Site Generator built with
                love by spf13 and friends in Go.
                Complete documentation is available at https://gohugo.io/documentation/`,
	Run: func(cmd *cobra.Command, args []string) {
		// Do Stuff Here
	},
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		// Do Stuff Here
		fmt.Println("PersistentPreRun in rootcmd runs")
		if showVersion {
			fmt.Println("Inside the flag")
		}
	},
}

var childCmd = &cobra.Command{
	Use:   "child",
	Short: "Hugo child is a very fast static site generator",
	Run: func(cmd *cobra.Command, args []string) {
		// Do Stuff Here
	},
}

var grandChildCmd = &cobra.Command{
	Use:   "grandchild",
	Short: "Hugo grand child is a very fast static site generator",
	Run: func(cmd *cobra.Command, args []string) {
		// Do Stuff Here
	},
}

func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func init() {
	rootCmd.AddCommand(childCmd)
	childCmd.AddCommand(grandChildCmd)

	rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "Show version")
}

func main() {
	Execute()
}
@haoming29 haoming29 changed the title PersistentPreRun was only executed if the command have a Run function PersistentPreRun was only executed if the command has a Run function Oct 5, 2023
@marckhouzam
Copy link
Collaborator

This is the expected behaviour as per the code. If you want to always print something for every single command maybe you can just print it in the main() function

PRs welcomed to improve the documentation

@haoming29
Copy link
Contributor Author

haoming29 commented Oct 6, 2023

Thanks @marckhouzam for the clarification. Will close this issue and should I have time, I will make a PR to add the note about this to the doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants