viper-app/cmd/root.go

46 lines
1015 B
Go
Raw Normal View History

2023-02-03 16:18:31 +08:00
package cmd
import (
"demo-server/cmd/inits"
2023-02-03 16:18:31 +08:00
"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()
2023-02-03 16:18:31 +08:00
if err != nil {
panic(err)
}
})
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./conf/application.yaml)")
}