/
root.go
84 lines (75 loc) · 2.29 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
73
74
75
76
77
78
79
80
81
82
83
84
package cmd
import (
"fmt"
"os"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
// Verbose flag to indicate the verbose mode
Verbose bool
// DbUser
DbUser string
DbPass string
DbName string
DbHost string
DbPort int16
cfgFile string
cfgName string = ".charlatan"
)
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("config file (default is $HOME/%s)", cfgName))
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().StringVarP(&DbUser, "user", "u", "", "Database username (required)")
rootCmd.MarkPersistentFlagRequired("user")
rootCmd.PersistentFlags().StringVarP(&DbPass, "password", "p", "", "Database user password (required)")
rootCmd.MarkPersistentFlagRequired("pass")
rootCmd.PersistentFlags().StringVarP(&DbName, "dbname", "d", "", "Database name (required)")
rootCmd.MarkPersistentFlagRequired("dbname")
rootCmd.PersistentFlags().StringVarP(&DbHost, "host", "", "127.0.0.1", "Host Database (default is 127.0.0.1)")
rootCmd.PersistentFlags().Int16VarP(&DbPort, "port", "", 3306, "Database port (default is 3306)")
viper.BindPFlag("user", rootCmd.PersistentFlags().Lookup("user"))
viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password"))
viper.BindPFlag("dbname", rootCmd.PersistentFlags().Lookup("dbname"))
}
func initConfig() {
hasCfgfile := false
if cfgFile != "" {
hasCfgfile = true
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if _, err := os.Stat(fmt.Sprintf("%s/%s", home, cfgName)); !os.IsNotExist(err) {
hasCfgfile = true
viper.AddConfigPath(home)
viper.SetConfigName(cfgName)
}
}
if hasCfgfile {
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Can't read config:", err)
os.Exit(1)
}
}
}
var rootCmd = &cobra.Command{
Use: "charlatan",
Short: "charlatan is a very fast fixtures loaders",
Long: `A Fast and Flexible fixtures loader built with love by guiyomh.`,
Run: func(cmd *cobra.Command, args []string) {
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}