root.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package cmd
  2. import (
  3. "gadmin/config"
  4. "github.com/spf13/cobra"
  5. )
  6. var cfgFile string
  7. // rootCmd represents the base command when called without any subcommands
  8. var rootCmd = &cobra.Command{
  9. Use: "serve",
  10. Short: "A brief description of your application",
  11. Long: `A longer description that spans multiple lines and likely contains
  12. examples and usage of using your application. For example:
  13. Cobra is a CLI library for Go that empowers applications.
  14. This application is a tool to generate the needed files
  15. to quickly create a Cobra application.`,
  16. // Uncomment the following line if your bare application
  17. // has an action associated with it:
  18. // Run: func(cmd *cobra.Command, args []string) {},
  19. PersistentPreRun: func(cmd *cobra.Command, args []string) {
  20. if config.SysType == "windows" {
  21. // envFilename = "local.env"
  22. // go run main.go -c local67.env
  23. if cfgFile == "" {
  24. cfgFile = "local.env"
  25. }
  26. } else {
  27. cfgFile = ".env"
  28. }
  29. config.Init(cfgFile)
  30. },
  31. }
  32. // Execute adds all child commands to the root command and sets flags appropriately.
  33. // This is called by main.main(). It only needs to happen once to the rootCmd.
  34. func Execute() {
  35. cobra.CheckErr(rootCmd.Execute())
  36. }
  37. func init() {
  38. // cobra.OnInitialize(initConfig)
  39. // Cobra also supports local flags, which will only run
  40. // when this action is called directly.
  41. rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
  42. rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.cobra.yaml)")
  43. }
  44. // initConfig reads in config file and ENV variables if set.
  45. func initConfig() {
  46. }