forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
69 lines (61 loc) · 1.57 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
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/markbates/going/defaults"
"github.com/markbates/pop"
"github.com/markbates/pop/fizz"
"github.com/spf13/cobra"
)
var cfgFile string
var env string
var debugMode bool
var version bool
var RootCmd = &cobra.Command{
Use: "soda",
Short: "A tasty treat for all your database needs",
PersistentPreRun: func(c *cobra.Command, args []string) {
fmt.Printf("Soda v%s\n\n", Version)
pop.Debug = debugMode
fizz.Debug = debugMode
env = defaults.String(os.Getenv("GO_ENV"), env)
setConfigLocation()
},
Run: func(cmd *cobra.Command, args []string) {
if !version {
cmd.Help()
}
},
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
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(&debugMode, "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
}