github.com/gobuffalo/pop@v4.13.1+incompatible/soda/cmd/root.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/gobuffalo/pop" 9 "github.com/gobuffalo/pop/internal/defaults" 10 "github.com/spf13/cobra" 11 ) 12 13 var cfgFile string 14 var env string 15 var version bool 16 17 // RootCmd is the entry point of soda CLI. 18 var RootCmd = &cobra.Command{ 19 SilenceUsage: true, 20 Short: "A tasty treat for all your database needs", 21 PersistentPreRun: func(c *cobra.Command, args []string) { 22 fmt.Printf("%s\n\n", Version) 23 // CLI flag has priority 24 if !c.PersistentFlags().Changed("env") { 25 env = defaults.String(os.Getenv("GO_ENV"), env) 26 } 27 // TODO! Only do this when the command needs it. 28 setConfigLocation() 29 pop.LoadConfigFile() 30 }, 31 RunE: func(cmd *cobra.Command, args []string) error { 32 if !version { 33 return cmd.Help() 34 } 35 return nil 36 }, 37 } 38 39 // Execute runs RunCmd. 40 func Execute() { 41 if err := RootCmd.Execute(); err != nil { 42 os.Exit(-1) 43 } 44 } 45 46 func init() { 47 RootCmd.Flags().BoolVarP(&version, "version", "v", false, "Show version information") 48 RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "The configuration file you would like to use.") 49 RootCmd.PersistentFlags().StringVarP(&env, "env", "e", "development", "The environment you want to run migrations against. Will use $GO_ENV if set.") 50 RootCmd.PersistentFlags().BoolVarP(&pop.Debug, "debug", "d", false, "Use debug/verbose mode") 51 } 52 53 func setConfigLocation() { 54 if cfgFile != "" { 55 abs, err := filepath.Abs(cfgFile) 56 if err != nil { 57 return 58 } 59 dir, file := filepath.Split(abs) 60 pop.AddLookupPaths(dir) 61 pop.ConfigName = file 62 } 63 } 64 65 func getConn() *pop.Connection { 66 conn := pop.Connections[env] 67 if conn == nil { 68 fmt.Printf("There is no connection named %s defined!\n", env) 69 os.Exit(1) 70 } 71 return conn 72 }