diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/.vscode/ipch/f3d1b0046d6a9751/mmap_address.bin b/.vscode/ipch/f3d1b0046d6a9751/mmap_address.bin new file mode 100644 index 0000000..71307ab Binary files /dev/null and b/.vscode/ipch/f3d1b0046d6a9751/mmap_address.bin differ diff --git a/data/cmd/cmd.go b/data/cmd/cmd.go index c567be9..199dc8d 100644 --- a/data/cmd/cmd.go +++ b/data/cmd/cmd.go @@ -1,11 +1,12 @@ package cmd import ( - "data/config" "fmt" "os" "public/tools" + "github.com/xie1xiao1jun/gorm-tools/data/config" + "github.com/spf13/cobra" "gopkg.in/go-playground/validator.v9" ) @@ -17,7 +18,7 @@ var rootCmd = &cobra.Command{ Short: "gorm mysql reflect tools", Long: `base on gorm tools for mysql database to golang struct`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println(tools.GetJsonStr(config.GetMysqlDbInfo())) + //开始做事情 }, } @@ -33,7 +34,7 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Host, "host", "H", "", "数据库地址.") + rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Host, "host", "H", "", "数据库地址.(注意-H为大写)") rootCmd.MarkFlagRequired("host") rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Username, "user", "u", "", "用户名.") rootCmd.MarkFlagRequired("user") @@ -49,18 +50,37 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { + MergeMysqlDbInfo() validate := validator.New() - err := validate.Struct(mysqlInfo) + err := validate.Struct(config.GetMysqlDbInfo()) if err != nil { - err1 := validate.Struct(config.GetMysqlDbInfo()) - if err1 != nil { - fmt.Println("Can't read cmd: using (-h, --help) to get more imfo") - fmt.Println("error info: ", err, err1) - os.Exit(1) - } else { - fmt.Println("using default config info.") - } + fmt.Println("Can't read cmd: using (-h, --help) to get more imfo") + fmt.Println("error info: ", err, err) + os.Exit(1) } else { - config.SetMysqlDbInfo(&mysqlInfo) + fmt.Println("using config info.") + fmt.Println(tools.GetJsonStr(config.GetMysqlDbInfo())) } } + +//合并 +func MergeMysqlDbInfo() { + var tmp = config.GetMysqlDbInfo() + if len(mysqlInfo.Database) > 0 { + tmp.Database = mysqlInfo.Database + } + if len(mysqlInfo.Host) > 0 { + tmp.Host = mysqlInfo.Host + } + if len(mysqlInfo.Password) > 0 { + tmp.Password = mysqlInfo.Password + } + if mysqlInfo.Port != 3306 { + tmp.Port = mysqlInfo.Port + } + if len(mysqlInfo.Username) > 0 { + tmp.Username = mysqlInfo.Username + } + + config.SetMysqlDbInfo(&tmp) +} diff --git a/data/view/gtools/gtools.go b/data/view/gtools/gtools.go new file mode 100644 index 0000000..e69de29 diff --git a/main b/main new file mode 100755 index 0000000..b32dffc Binary files /dev/null and b/main differ diff --git a/main.go b/main.go index d916842..f0fb043 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "data/cmd" + "github.com/xie1xiao1jun/gorm-tools/data/cmd" ) func main() {