diff --git a/cmd/kratos/internal/run/run.go b/cmd/kratos/internal/run/run.go index ac229bab2..005c832af 100644 --- a/cmd/kratos/internal/run/run.go +++ b/cmd/kratos/internal/run/run.go @@ -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) { @@ -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 @@ -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 + } +}