Skip to content

Commit

Permalink
feat: add custom go module configuration support when initialize go p…
Browse files Browse the repository at this point in the history
…roject using command `gf init` (#3425)
  • Loading branch information
houseme committed Apr 2, 2024
1 parent f039393 commit 1ffb510
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions cmd/gf/internal/cmd/cmd_init.go
Expand Up @@ -25,6 +25,7 @@ import (
)

var (
// Init .
Init = cInit{}
)

Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
}
Expand All @@ -118,14 +118,19 @@ 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 {
if strings.Contains(path, ignoreFile) {
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
Expand Down

0 comments on commit 1ffb510

Please sign in to comment.