diff --git a/cmd/gf/internal/cmd/cmd_init.go b/cmd/gf/internal/cmd/cmd_init.go index 1f3ff694b..c2494ad73 100644 --- a/cmd/gf/internal/cmd/cmd_init.go +++ b/cmd/gf/internal/cmd/cmd_init.go @@ -25,6 +25,7 @@ import ( ) var ( + // Init . Init = cInit{} ) @@ -64,14 +65,13 @@ type cInitInput struct { Name string `name:"NAME" arg:"true" v:"required" brief:"{cInitNameBrief}"` Mono bool `name:"mono" short:"m" brief:"initialize a mono-repo instead a single-repo" orphan:"true"` Update bool `name:"update" short:"u" brief:"update to the latest goframe version" orphan:"true"` + Module string `name:"module" short:"g" brief:"custom go module"` } type cInitOutput struct{} func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err error) { - var ( - overwrote = false - ) + var overwrote = false if !gfile.IsEmpty(in.Name) && !allyes.Check() { s := gcmd.Scanf(`the folder "%s" is not empty, files might be overwrote, continue? [y/n]: `, in.Name) if strings.EqualFold(s, "n") { @@ -105,7 +105,7 @@ func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err err = gfile.ReadLines(gitignoreFile, func(line string) error { // Add only hidden files or directories // If other directories are added, it may cause the entire directory to be ignored - // such as 'main' in the .gitignore file, but the path is 'D:\main\my-project' + // such as 'main' in the .gitignore file, but the path is ' D:\main\my-project ' if line != "" && strings.HasPrefix(line, ".") { ignoreFiles = append(ignoreFiles, line) } @@ -118,6 +118,11 @@ func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err } } + // Replace module name. + if in.Module == "" { + in.Module = gfile.Basename(gfile.RealPath(in.Name)) + } + // Replace template name to project name. err = gfile.ReplaceDirFunc(func(path, content string) string { for _, ignoreFile := range ignoreFiles { @@ -125,7 +130,7 @@ func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err return content } } - return gstr.Replace(gfile.GetContents(path), cInitRepoPrefix+templateRepoName, gfile.Basename(gfile.RealPath(in.Name))) + return gstr.Replace(gfile.GetContents(path), cInitRepoPrefix+templateRepoName, in.Module) }, in.Name, "*", true) if err != nil { return