fix: change the working directory of the command (#2560)

* fix: change the working directory of the command
Refs #2519

* fix: change the working directory by kratos run --work=xxxxx
pull/2739/head
Xin 2 years ago committed by GitHub
parent 8af9ca33bd
commit d05729399e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      cmd/kratos/internal/run/run.go

@ -19,6 +19,11 @@ var CmdRun = &cobra.Command{
Long: "Run project. Example: kratos run", Long: "Run project. Example: kratos run",
Run: Run, Run: Run,
} }
var targetDir string
func init() {
CmdRun.Flags().StringVarP(&targetDir, "work", "w", "", "target working directory")
}
// Run run project. // Run run project.
func Run(cmd *cobra.Command, args []string) { func Run(cmd *cobra.Command, args []string) {
@ -64,10 +69,11 @@ func Run(cmd *cobra.Command, args []string) {
dir = cmdPath[dir] dir = cmdPath[dir]
} }
} }
fd := exec.Command("go", append([]string{"run", "."}, programArgs...)...) fd := exec.Command("go", append([]string{"run", dir}, programArgs...)...)
fd.Stdout = os.Stdout fd.Stdout = os.Stdout
fd.Stderr = os.Stderr fd.Stderr = os.Stderr
fd.Dir = dir fd.Dir = dir
changeWorkingDirectory(fd, targetDir)
if err := fd.Run(); err != nil { if err := fd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err.Error()) fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err.Error())
return return
@ -131,3 +137,10 @@ func findCMD(base string) (map[string]string, error) {
} }
return map[string]string{"": base}, nil return map[string]string{"": base}, nil
} }
func changeWorkingDirectory(cmd *exec.Cmd, targetDir string) {
targetDir = strings.TrimSpace(targetDir)
if targetDir != "" {
cmd.Dir = targetDir
}
}

Loading…
Cancel
Save