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