diff --git a/Makefile b/Makefile index 299d77b..5ad4410 100644 --- a/Makefile +++ b/Makefile @@ -169,6 +169,12 @@ deploy-binary: binary-package @expect scripts/deploy-binary.sh $(USER) $(PWD) $(IP) +.PHONY: patch +# patch some dependent code, such as types.proto, mysql initialization code. e.g. make patch TYPE=types-pb , make patch TYPE=mysql-redis-init +patch: + @bash scripts/patch.sh $(TYPE) + + .PHONY: clean # clean binary file, cover.out, template file clean: diff --git a/cmd/sponge/commands/gen.go b/cmd/sponge/commands/gen.go new file mode 100644 index 0000000..a172399 --- /dev/null +++ b/cmd/sponge/commands/gen.go @@ -0,0 +1,23 @@ +package commands + +import ( + "github.com/zhufuyi/sponge/cmd/sponge/commands/generate" + + "github.com/spf13/cobra" +) + +// GenCommand generate dependency code +func GenCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "gen", + Short: "Generate dependency code, e.g. mysql and redis initialization code, types.proto", + Long: `generate dependency code.`, + SilenceErrors: true, + SilenceUsage: true, + } + cmd.AddCommand( + generate.MysqlAndRedisCommand(), + generate.TypesPbCommand(), + ) + return cmd +} diff --git a/cmd/sponge/commands/generate/mysql-redis-init.go b/cmd/sponge/commands/generate/mysql-redis-init.go new file mode 100644 index 0000000..99fd048 --- /dev/null +++ b/cmd/sponge/commands/generate/mysql-redis-init.go @@ -0,0 +1,129 @@ +package generate + +import ( + "errors" + "fmt" + "strings" + + "github.com/zhufuyi/sponge/pkg/gofile" + "github.com/zhufuyi/sponge/pkg/replacer" + + "github.com/spf13/cobra" +) + +// MysqlAndRedisCommand generate mysql and redis initialization code +func MysqlAndRedisCommand() *cobra.Command { + var ( + moduleName string // go.mod module name + outPath string // output directory + targetFile = "internal/model/init.go" + ) + + cmd := &cobra.Command{ + Use: "mysql-redis-init", + Short: "Generate mysql and redis initialization code", + Long: `generate mysql and redis initialization code + +Examples: + # generate mysql and redis initialization code. + sponge gen mysql-redis-init --module-name=yourModuleName + + # generate mysql and redis initialization code, and specify the server directory, Note: code generation will be canceled when the latest generated file already exists. + sponge gen mysql-redis-init --out=./yourServerDir +`, + SilenceErrors: true, + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + mdName, _ := getNamesFromOutDir(outPath) + if mdName != "" { + moduleName = mdName + } else if moduleName == "" { + return fmt.Errorf(`required flag(s) "module-name" not set, use "sponge gen mysql-redis-init -h" for help`) + } + + var isEmpty bool + if outPath == "" { + isEmpty = true + } else { + isEmpty = false + if gofile.IsExists(targetFile) { + fmt.Printf("'%s' already exists, no need to generate it.\n", targetFile) + return nil + } + } + + var err error + outPath, err = runMysqlCliCommand(moduleName, outPath) + if err != nil { + return err + } + + if isEmpty { + fmt.Printf(` +using help: + move the folder "internal" to your project code folder. + +`) + } + if gofile.IsWindows() { + targetFile = "\\" + strings.ReplaceAll(targetFile, "/", "\\") + } else { + targetFile = "/" + targetFile + } + fmt.Printf("generate 'mysql-redis-init' codes successfully, out = %s\n", outPath+targetFile) + return nil + }, + } + + cmd.Flags().StringVarP(&moduleName, "module-name", "m", "", "module-name is the name of the module in the 'go.mod' file") + cmd.Flags().StringVarP(&outPath, "out", "o", "", "output directory, default is ./mysql-redis-init_