Skip to content

Commit

Permalink
fix: project creation --nomod (#2611)
Browse files Browse the repository at this point in the history
Co-authored-by: czyt <czyt@w.cn>
  • Loading branch information
czyt and czyt committed Jan 10, 2023
1 parent b2689af commit 6174475
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cmd/kratos/internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ func run(cmd *cobra.Command, args []string) {
done <- p.New(ctx, wd, repoURL, branch)
return
}
if _, e := os.Stat(path.Join(wd, "go.mod")); os.IsNotExist(e) {
done <- fmt.Errorf("🚫 go.mod don't exists in %s", wd)
projectRoot := getgomodProjectRoot(wd)
if gomodIsNotExistIn(projectRoot) {
done <- fmt.Errorf("🚫 go.mod don't exists in %s", projectRoot)
return
}

mod, e := base.ModulePath(path.Join(wd, "go.mod"))
mod, e := base.ModulePath(path.Join(projectRoot, "go.mod"))
if e != nil {
panic(e)
}
Expand Down Expand Up @@ -123,3 +124,18 @@ func getProjectPlaceDir(projectName string, fallbackPlaceDir string) string {
// create project logic will check stat,so not check path stat here
return filepath.Dir(projectFullPath)
}

func getgomodProjectRoot(dir string) string {
if dir == filepath.Dir(dir) {
return dir
}
if gomodIsNotExistIn(dir) {
return getgomodProjectRoot(filepath.Dir(dir))
}
return dir
}

func gomodIsNotExistIn(dir string) bool {
_, e := os.Stat(path.Join(dir, "go.mod"))
return os.IsNotExist(e)
}

0 comments on commit 6174475

Please sign in to comment.