add flag check
命令行标记优化
This commit is contained in:
@@ -10,17 +10,10 @@ import (
|
|||||||
"github.com/xxjwxc/gormt/data/config"
|
"github.com/xxjwxc/gormt/data/config"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/xxjwxc/public/mycobra"
|
||||||
"gopkg.in/go-playground/validator.v9"
|
"gopkg.in/go-playground/validator.v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mysqlInfo config.DBInfo
|
|
||||||
var outDir string
|
|
||||||
var singularTable bool
|
|
||||||
var foreignKey bool
|
|
||||||
var funcKey bool
|
|
||||||
var ui bool
|
|
||||||
var urlTag string
|
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "main",
|
Use: "main",
|
||||||
Short: "gorm mysql reflect tools",
|
Short: "gorm mysql reflect tools",
|
||||||
@@ -42,36 +35,36 @@ func Execute() {
|
|||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Host, "host", "H", "", "数据库地址.(注意-H为大写)")
|
rootCmd.PersistentFlags().StringP("host", "H", "", "数据库地址.(注意-H为大写)")
|
||||||
rootCmd.MarkFlagRequired("host")
|
rootCmd.MarkFlagRequired("host")
|
||||||
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Username, "user", "u", "", "用户名.")
|
rootCmd.PersistentFlags().StringP("user", "u", "", "用户名.")
|
||||||
rootCmd.MarkFlagRequired("user")
|
rootCmd.MarkFlagRequired("user")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Password, "password", "p", "", "密码.")
|
rootCmd.PersistentFlags().StringP("password", "p", "", "密码.")
|
||||||
rootCmd.MarkFlagRequired("password")
|
rootCmd.MarkFlagRequired("password")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&mysqlInfo.Database, "database", "d", "", "数据库名")
|
rootCmd.PersistentFlags().StringP("database", "d", "", "数据库名")
|
||||||
rootCmd.MarkFlagRequired("database")
|
rootCmd.MarkFlagRequired("database")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&outDir, "outdir", "o", "", "输出目录")
|
rootCmd.PersistentFlags().StringP("outdir", "o", "", "输出目录")
|
||||||
rootCmd.MarkFlagRequired("outdir")
|
rootCmd.MarkFlagRequired("outdir")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&singularTable, "singular", "s", true, "是否禁用表名复数")
|
rootCmd.PersistentFlags().BoolP("singular", "s", true, "是否禁用表名复数")
|
||||||
rootCmd.MarkFlagRequired("singular")
|
rootCmd.MarkFlagRequired("singular")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&foreignKey, "foreign", "f", false, "是否导出外键关联")
|
rootCmd.PersistentFlags().BoolP("foreign", "f", false, "是否导出外键关联")
|
||||||
rootCmd.MarkFlagRequired("foreign key")
|
rootCmd.MarkFlagRequired("foreign key")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&funcKey, "fun", "F", false, "是否导出函数")
|
rootCmd.PersistentFlags().BoolP("fun", "F", false, "是否导出函数")
|
||||||
rootCmd.MarkFlagRequired("func export")
|
rootCmd.MarkFlagRequired("func export")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&ui, "gui", "g", false, "是否ui显示模式")
|
rootCmd.PersistentFlags().BoolP("gui", "g", false, "是否ui显示模式")
|
||||||
rootCmd.MarkFlagRequired("show on gui")
|
rootCmd.MarkFlagRequired("show on gui")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&urlTag, "url", "l", "", "url标签(json,url)")
|
rootCmd.PersistentFlags().StringP("url", "l", "", "url标签(json,url)")
|
||||||
rootCmd.MarkFlagRequired("url tag")
|
rootCmd.MarkFlagRequired("url tag")
|
||||||
|
|
||||||
rootCmd.Flags().IntVar(&mysqlInfo.Port, "port", 3306, "端口号")
|
rootCmd.Flags().Int("port", 3306, "端口号")
|
||||||
}
|
}
|
||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
@@ -92,45 +85,34 @@ func initConfig() {
|
|||||||
// MergeMysqlDbInfo merge parm
|
// MergeMysqlDbInfo merge parm
|
||||||
func MergeMysqlDbInfo() {
|
func MergeMysqlDbInfo() {
|
||||||
var tmp = config.GetDbInfo()
|
var tmp = config.GetDbInfo()
|
||||||
if len(mysqlInfo.Database) > 0 {
|
mycobra.IfReplace(rootCmd, "database", &tmp.Database) // 如果设置了,更新
|
||||||
tmp.Database = mysqlInfo.Database
|
mycobra.IfReplace(rootCmd, "host", &tmp.Host) // 如果设置了,更新
|
||||||
}
|
mycobra.IfReplace(rootCmd, "password", &tmp.Password) // 如果设置了,更新
|
||||||
if len(mysqlInfo.Host) > 0 {
|
mycobra.IfReplace(rootCmd, "port", &tmp.Port) // 如果设置了,更新
|
||||||
tmp.Host = mysqlInfo.Host
|
mycobra.IfReplace(rootCmd, "user", &tmp.Username) // 如果设置了,更新
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
if len(urlTag) > 0 {
|
|
||||||
config.SetURLTag(urlTag)
|
|
||||||
}
|
|
||||||
|
|
||||||
config.SetMysqlDbInfo(&tmp)
|
config.SetMysqlDbInfo(&tmp)
|
||||||
|
|
||||||
if len(outDir) > 0 {
|
url := config.GetURLTag()
|
||||||
config.SetOutDir(outDir)
|
mycobra.IfReplace(rootCmd, "url", &url) // 如果设置了,更新
|
||||||
}
|
config.SetURLTag(url)
|
||||||
|
|
||||||
if singularTable {
|
dir := config.GetOutDir()
|
||||||
config.SetSingularTable(singularTable)
|
mycobra.IfReplace(rootCmd, "outdir", &dir) // 如果设置了,更新
|
||||||
}
|
config.SetOutDir(dir)
|
||||||
|
|
||||||
if foreignKey {
|
st := config.GetSingularTable()
|
||||||
config.SetForeignKey(foreignKey)
|
mycobra.IfReplace(rootCmd, "singular", &st) // 如果设置了,更新
|
||||||
}
|
config.SetSingularTable(st)
|
||||||
|
|
||||||
if funcKey {
|
fk := config.GetIsForeignKey()
|
||||||
config.SetIsOutFunc(funcKey)
|
mycobra.IfReplace(rootCmd, "foreign", &fk) // 如果设置了,更新
|
||||||
}
|
config.SetForeignKey(st)
|
||||||
|
|
||||||
if ui {
|
funcKey := config.GetIsOutFunc()
|
||||||
config.SetIsGUI(ui)
|
mycobra.IfReplace(rootCmd, "fun", &funcKey) // 如果设置了,更新
|
||||||
}
|
config.SetIsOutFunc(funcKey)
|
||||||
|
|
||||||
|
ig := config.GetIsGUI()
|
||||||
|
mycobra.IfReplace(rootCmd, "gui", &ig) // 如果设置了,更新
|
||||||
|
config.SetIsGUI(ig)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user