package cmd import ( "demo-server/cmd/inits" "os" "github.com/spf13/cobra" ) var cfgFile string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "${cmd}", Short: "${cmd} example", Long: `${cmd} example use --config use config file to init app. `, // Uncomment the following line if your bare application // has an action associated with it: Run: func(cmd *cobra.Command, args []string) { println("app say: no default opt, see help. ") _ = cmd.Help() }, } // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { err := rootCmd.Execute() if err != nil { os.Exit(1) } } func init() { cobra.OnInitialize(func() { err := inits.InitFundamental() if err != nil { panic(err) } }) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./conf/application.yaml)") }