1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-28 03:57:02 +02:00

fix: project creation --nomod (#2611)

Co-authored-by: czyt <czyt@w.cn>
This commit is contained in:
虫子樱桃 2023-01-10 18:22:19 +08:00 committed by GitHub
parent b2689af39c
commit 61744753eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}
@ -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)
}