fix issue:#2358 Support for creating a project with specifying the name for its place dir (#2573)

Co-authored-by: czyt <czyt@w.cn>
pull/2575/head
虫子樱桃 2 years ago committed by GitHub
parent 2cf82fa4a7
commit 3d322fe6c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      cmd/kratos/internal/project/project.go

@ -6,6 +6,8 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
@ -64,7 +66,8 @@ func run(cmd *cobra.Command, args []string) {
} else {
name = args[0]
}
p := &Project{Name: path.Base(name), Path: name}
wd = getProjectPlaceDir(name, wd)
p := &Project{Name: filepath.Base(name), Path: name}
done := make(chan error, 1)
go func() {
if !nomod {
@ -95,3 +98,26 @@ func run(cmd *cobra.Command, args []string) {
}
}
}
func getProjectPlaceDir(projectName string, fallbackPlaceDir string) string {
projectWorkingDir := filepath.Dir(projectName)
// check for home dir
if strings.HasPrefix(projectWorkingDir, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
// cannot get user home return fallback place dir
return fallbackPlaceDir
}
projectName = filepath.Join(homeDir, projectName[2:])
}
// check path is relative
if !filepath.IsAbs(projectWorkingDir) {
wdAbs, err := filepath.Abs(projectName)
if err != nil {
return fallbackPlaceDir
}
projectWorkingDir = wdAbs
}
// create project logic will check stat,so not check path stat here
return projectWorkingDir
}

Loading…
Cancel
Save