github.com/youminxue/odin@v0.0.0-20230216022911-c2c8b05d3a41/cmd/ddl.go (about) 1 package cmd 2 3 import ( 4 "github.com/kelseyhightower/envconfig" 5 "github.com/sirupsen/logrus" 6 "github.com/spf13/cobra" 7 "github.com/youminxue/odin/cmd/internal/ddl" 8 "github.com/youminxue/odin/cmd/internal/ddl/config" 9 "github.com/youminxue/odin/toolkit/dotenv" 10 "github.com/youminxue/odin/toolkit/pathutils" 11 "github.com/youminxue/odin/toolkit/yaml" 12 ) 13 14 var dir string 15 var reverse bool 16 var dao bool 17 var pre string 18 var df string 19 var env string 20 21 // ddlCmd generates entity and dao layer source code from database tables and update tables from entity code 22 var ddlCmd = &cobra.Command{ 23 Use: "ddl", 24 Short: "migration tool between database table structure and golang struct", 25 Long: ``, 26 Run: func(cmd *cobra.Command, args []string) { 27 yaml.Load(env) 28 dotenv.Load(env) 29 var conf config.DbConfig 30 err := envconfig.Process("db", &conf) 31 if err != nil { 32 logrus.Panicln("Error processing env", err) 33 } 34 if dir, err = pathutils.FixPath(dir, "entity"); err != nil { 35 logrus.Panicln(err) 36 } 37 d := ddl.Ddl{dir, reverse, dao, pre, df, conf} 38 d.Exec() 39 }, 40 } 41 42 func init() { 43 rootCmd.AddCommand(ddlCmd) 44 45 // Here you will define your flags and configuration settings. 46 47 // Cobra supports Persistent Flags which will work for this command 48 // and all subcommands, e.g.: 49 // ddlCmd.PersistentFlags().String("foo", "", "A help for foo") 50 51 // Cobra supports local flags which will only run when this command 52 // is called directly, e.g.: 53 ddlCmd.Flags().StringVar(&dir, "entity", "entity", "Path of entity folder.") 54 ddlCmd.Flags().StringVar(&pre, "pre", "", "Table name prefix. e.g.: prefix biz_ for biz_product.") 55 ddlCmd.Flags().StringVar(&df, "df", "dao", "Name of dao folder.") 56 ddlCmd.Flags().StringVar(&env, "env", "dev", "Environment name such as dev, uat, test, prod, default is dev") 57 ddlCmd.Flags().BoolVarP(&reverse, "reverse", "r", false, "If true, generate entity code from database. If false, update or create database tables from entity code.") 58 ddlCmd.Flags().BoolVarP(&dao, "dao", "d", false, "If true, generate dao code.") 59 }