fix(cmd/run): command execution directory error (#1336)

* fix(cmd/run): command execution directory error

* fix
pull/1341/head
包子 3 years ago committed by GitHub
parent c2b4d45cdf
commit 3f68c9a9e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 43
      cmd/kratos/internal/run/run.go

@ -34,7 +34,7 @@ func Run(cmd *cobra.Command, args []string) {
} }
if dir == "" { if dir == "" {
// find the directory containing the cmd/* // find the directory containing the cmd/*
cmdDir, cmdPath, err := findCMD(base) cmdPath, err := findCMD(base)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err) fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err)
return return
@ -43,17 +43,23 @@ func Run(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", "The cmd directory cannot be found in the current directory") fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", "The cmd directory cannot be found in the current directory")
return return
} else if len(cmdPath) == 1 { } else if len(cmdPath) == 1 {
dir = path.Join(cmdDir, cmdPath[0]) for k, v := range cmdPath {
dir = path.Join(v, k)
}
} else { } else {
var cmdPaths []string
for k, _ := range cmdPath {
cmdPaths = append(cmdPaths, k)
}
prompt := &survey.Select{ prompt := &survey.Select{
Message: "Which directory do you want to run?", Message: "Which directory do you want to run?",
Options: cmdPath, Options: cmdPaths,
} }
survey.AskOne(prompt, &dir) survey.AskOne(prompt, &dir)
if dir == "" { if dir == "" {
return return
} }
dir = path.Join(cmdDir, dir) dir = path.Join(cmdPath[dir], dir)
} }
} }
fd := exec.Command("go", "run", ".") fd := exec.Command("go", "run", ".")
@ -67,15 +73,12 @@ func Run(cmd *cobra.Command, args []string) {
return return
} }
func findCMD(base string) (string, []string, error) { func findCMD(base string) (map[string]string, error) {
var root bool var root bool
next := func(dir string) (string, []string, error) { next := func(dir string) (map[string]string, error) {
var ( cmdPath := make(map[string]string)
cmdDir string
cmdPath []string
)
err := filepath.Walk(dir, func(walkPath string, info os.FileInfo, err error) error { err := filepath.Walk(dir, func(walkPath string, info os.FileInfo, err error) error {
// multi level directory is not allowed under the cmd directory, so it is judged that the path ends with cmd. // multi level directory is not allowed under the cmdPath directory, so it is judged that the path ends with cmdPath.
if strings.HasSuffix(walkPath, "cmd") { if strings.HasSuffix(walkPath, "cmd") {
paths, err := ioutil.ReadDir(walkPath) paths, err := ioutil.ReadDir(walkPath)
if err != nil { if err != nil {
@ -83,10 +86,9 @@ func findCMD(base string) (string, []string, error) {
} }
for _, fileInfo := range paths { for _, fileInfo := range paths {
if fileInfo.IsDir() { if fileInfo.IsDir() {
cmdPath = append(cmdPath, path.Join("cmd", fileInfo.Name())) cmdPath[path.Join("cmd", fileInfo.Name())] = filepath.Join(walkPath, "..")
} }
} }
cmdDir = filepath.Join(walkPath, "..")
return nil return nil
} }
if info.Name() == "go.mod" { if info.Name() == "go.mod" {
@ -94,20 +96,21 @@ func findCMD(base string) (string, []string, error) {
} }
return nil return nil
}) })
return cmdDir, cmdPath, err return cmdPath, err
} }
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
cmdDir, res, err := next(base) tmp := base
cmd, err := next(tmp)
if err != nil { if err != nil {
return "", nil, err return nil, err
} }
if len(res) > 0 { if len(cmd) > 0 {
return cmdDir, res, nil return cmd, nil
} }
if root { if root {
break break
} }
base = filepath.Join(base, "..") tmp = filepath.Join(base, "..")
} }
return "", []string{base}, nil return map[string]string{"": base}, nil
} }

Loading…
Cancel
Save