Skip to content

Commit e3b718e

Browse files
lascybkevwan
authored andcommitted
By default, it is compatible with previous versions, and the auto-package and package parameters are added
1 parent 0e753a6 commit e3b718e

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

tools/goctl/internal/flags/default_en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@
205205
"cache": "Generate code with cache [optional]",
206206
"easy": "Generate code with auto generated CollectionName for easy declare [optional]",
207207
"dir": "{{.goctl.model.dir}}",
208+
"auto-package": "Whether to automatically use the name of the directory where the build file is located as the package name [optional,default=false]\nIt is mainly used to be compatible with previous versions,If this parameter is set to false, the package name is set to model by default",
209+
"package": "Manually specify the package name, which takes precedence over VarBoolPkgAuto [optional]",
208210
"style": "{{.global.style}}",
209211
"home": "{{.global.home}}",
210212
"remote": "{{.global.remote}}",

tools/goctl/model/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func init() {
6363
mongoCmdFlags.BoolVarP(&mongo.VarBoolCache, "cache", "c")
6464
mongoCmdFlags.BoolVarP(&mongo.VarBoolEasy, "easy", "e")
6565
mongoCmdFlags.StringVarP(&mongo.VarStringDir, "dir", "d")
66+
mongoCmdFlags.BoolVarP(&mongo.VarBoolAutoPackage, "auto-package", "a")
67+
mongoCmdFlags.StringVarP(&mongo.VarStringPackage, "package", "p")
6668
mongoCmdFlags.StringVar(&mongo.VarStringStyle, "style")
6769
mongoCmdFlags.StringVar(&mongo.VarStringHome, "home")
6870
mongoCmdFlags.StringVar(&mongo.VarStringRemote, "remote")

tools/goctl/model/mongo/mongo.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ var (
1717
VarStringSliceType []string
1818
// VarStringDir describes an output directory.
1919
VarStringDir string
20+
//VarBoolAutoPackage Default false;
21+
// - Whether to automatically use the name of the directory where the build file is located as the package name
22+
// - It is mainly used to be compatible with previous versions,If this parameter is set to false, the package name is set to model by default
23+
VarBoolAutoPackage bool
24+
//VarStringPackage Manually specify the package name, which takes precedence over VarBoolPkgAuto
25+
VarStringPackage string
2026
// VarBoolCache describes whether cache is enabled.
2127
VarBoolCache bool
2228
// VarBoolEasy describes whether to generate Collection Name in the code for easy declare.
@@ -66,22 +72,25 @@ func Action(_ *cobra.Command, _ []string) error {
6672
if err != nil {
6773
return err
6874
}
69-
7075
if err = pathx.MkdirIfNotExist(a); err != nil {
7176
return err
7277
}
7378

74-
baseDir := filepath.Base(a)
75-
if baseDir == "" || baseDir == "." {
76-
baseDir = "model" // as default
79+
var pkg string
80+
if VarStringPackage != "" {
81+
pkg = VarStringPackage
82+
} else if VarBoolAutoPackage {
83+
pkg = file.SafeString(filepath.Base(a))
84+
} else {
85+
pkg = "model"
7786
}
7887

7988
return generate.Do(&generate.Context{
80-
Types: tp,
81-
Cache: c,
82-
Easy: easy,
83-
Output: a,
84-
Cfg: cfg,
85-
PackageName: baseDir,
89+
Types: tp,
90+
Cache: c,
91+
Easy: easy,
92+
Output: a,
93+
Cfg: cfg,
94+
PackageName: pkg,
8695
})
8796
}

0 commit comments

Comments
 (0)