spf13/cobra

command PreRun don't run when to use [ app install phase -f ],otherwie PreRun is running when to use [ app install -f ]

Boruson opened this issue · 2 comments

command PreRun don't run when to use [ app install phase -f ],otherwie PreRun is running when to use [ app install -f ]
detail :

var (
	Url string
	Force bool
)
func NewUpgradeCmd() *cobra.Command {
	upRunner := workflow.NewRunner()
	cmd := &cobra.Command{
		Use:    "install",
		Short:  "install your app",
		Run: func(cmd *cobra.Command, args []string) {
			if err := upRunner.Run(args); err != nil {
				logger.Error(err)
			}
		},
		PreRun: PreloadConfCmdFunc,
	}
	cmd.Flags().StringVar(Url, "pkg-url", "", "https://github.com/spf13/cobra/app.tar.gz download offline package url")
	cmd.Flags().BoolVarP(Force, "force", "f", false, "install need interactive to confirm")

	upRunner.AppendPhase(PreflightPhase())
	upRunner.AppendPhase(InstallerPhase())

	// sets the data builder function, that will be used by the runner
	// both when running the entire workflow or single phases
	upRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) {
		u := install.load(, phases.NewPkgUrl)
		fmt.Println("u.Config=>:",u.Config)
		fmt.Println("u.Url=>:",u.Url)
		
		return u, nil
	})

	// binds the Runner to hke upgrade command by altering
	// command help, adding --skip-phases flag and by adding phases subcommands
	upRunner.BindToCommand(cmd)
	return cmd
}

func PreloadConfCmdFunc(cmd *cobra.Command, args []string) {
	if err := install.load(Url); err != nil {
		logger.Error("PreRun error: ", err)
		os.Exit(1)
	}
}

why? would you help me for the puzzle?

Is phase a sub-command of install? If so, you need to use PersistentPreRun

Thank you very much! PersistentPreRun can execute correctlly