Skip to content

Commit

Permalink
fix: change the working directory of the command (#2560)
Browse files Browse the repository at this point in the history
* fix: change the working directory of the command
Refs #2519

* fix: change the working directory by kratos run --work=xxxxx
  • Loading branch information
starmilkxin authored Mar 16, 2023
1 parent 8af9ca3 commit d057293
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cmd/kratos/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ var CmdRun = &cobra.Command{
Long: "Run project. Example: kratos run",
Run: Run,
}
var targetDir string

func init() {
CmdRun.Flags().StringVarP(&targetDir, "work", "w", "", "target working directory")
}

// Run run project.
func Run(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -64,10 +69,11 @@ func Run(cmd *cobra.Command, args []string) {
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.Stderr = os.Stderr
fd.Dir = dir
changeWorkingDirectory(fd, targetDir)
if err := fd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err.Error())
return
Expand Down Expand Up @@ -131,3 +137,10 @@ func findCMD(base string) (map[string]string, error) {
}
return map[string]string{"": base}, nil
}

func changeWorkingDirectory(cmd *exec.Cmd, targetDir string) {
targetDir = strings.TrimSpace(targetDir)
if targetDir != "" {
cmd.Dir = targetDir
}
}

0 comments on commit d057293

Please sign in to comment.