new
new
This commit is contained in:
BIN
.vscode/ipch/f3d1b0046d6a9751/mmap_address.bin
vendored
Normal file
BIN
.vscode/ipch/f3d1b0046d6a9751/mmap_address.bin
vendored
Normal file
Binary file not shown.
@@ -1,11 +1,12 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"data/config"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"public/tools"
|
"public/tools"
|
||||||
|
|
||||||
|
"github.com/xie1xiao1jun/gorm-tools/data/config"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/go-playground/validator.v9"
|
"gopkg.in/go-playground/validator.v9"
|
||||||
)
|
)
|
||||||
@@ -17,7 +18,7 @@ var rootCmd = &cobra.Command{
|
|||||||
Short: "gorm mysql reflect tools",
|
Short: "gorm mysql reflect tools",
|
||||||
Long: `base on gorm tools for mysql database to golang struct`,
|
Long: `base on gorm tools for mysql database to golang struct`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println(tools.GetJsonStr(config.GetMysqlDbInfo()))
|
|
||||||
//开始做事情
|
//开始做事情
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -33,7 +34,7 @@ func Execute() {
|
|||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Host, "host", "H", "", "数据库地址.")
|
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Host, "host", "H", "", "数据库地址.(注意-H为大写)")
|
||||||
rootCmd.MarkFlagRequired("host")
|
rootCmd.MarkFlagRequired("host")
|
||||||
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Username, "user", "u", "", "用户名.")
|
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Username, "user", "u", "", "用户名.")
|
||||||
rootCmd.MarkFlagRequired("user")
|
rootCmd.MarkFlagRequired("user")
|
||||||
@@ -49,18 +50,37 @@ func init() {
|
|||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
MergeMysqlDbInfo()
|
||||||
validate := validator.New()
|
validate := validator.New()
|
||||||
err := validate.Struct(mysqlInfo)
|
err := validate.Struct(config.GetMysqlDbInfo())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err1 := validate.Struct(config.GetMysqlDbInfo())
|
fmt.Println("Can't read cmd: using (-h, --help) to get more imfo")
|
||||||
if err1 != nil {
|
fmt.Println("error info: ", err, err)
|
||||||
fmt.Println("Can't read cmd: using (-h, --help) to get more imfo")
|
os.Exit(1)
|
||||||
fmt.Println("error info: ", err, err1)
|
|
||||||
os.Exit(1)
|
|
||||||
} else {
|
|
||||||
fmt.Println("using default config info.")
|
|
||||||
}
|
|
||||||
} else {
|
} 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)
|
||||||
|
}
|
||||||
|
|||||||
0
data/view/gtools/gtools.go
Normal file
0
data/view/gtools/gtools.go
Normal file
Reference in New Issue
Block a user