feat: support passing program args with run cmd (#2207)

pull/2217/head
Haibo 2 years ago committed by GitHub
parent 246d8d9c28
commit 59b758ceda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      cmd/kratos/internal/run/run.go

@ -23,8 +23,9 @@ var CmdRun = &cobra.Command{
// Run run project. // Run run project.
func Run(cmd *cobra.Command, args []string) { func Run(cmd *cobra.Command, args []string) {
var dir string var dir string
if len(args) > 0 { cmdArgs, programArgs := splitArgs(cmd, args)
dir = args[0] if len(cmdArgs) > 0 {
dir = cmdArgs[0]
} }
base, err := os.Getwd() base, err := os.Getwd()
if err != nil { if err != nil {
@ -63,7 +64,7 @@ func Run(cmd *cobra.Command, args []string) {
dir = cmdPath[dir] dir = cmdPath[dir]
} }
} }
fd := exec.Command("go", "run", ".") fd := exec.Command("go", append([]string{"run", "."}, programArgs...)...)
fd.Stdout = os.Stdout fd.Stdout = os.Stdout
fd.Stderr = os.Stderr fd.Stderr = os.Stderr
fd.Dir = dir fd.Dir = dir
@ -73,6 +74,14 @@ func Run(cmd *cobra.Command, args []string) {
} }
} }
func splitArgs(cmd *cobra.Command, args []string) (cmdArgs, programArgs []string) {
dashAt := cmd.ArgsLenAtDash()
if dashAt >= 0 {
return args[:dashAt], args[dashAt:]
}
return args, []string{}
}
func findCMD(base string) (map[string]string, error) { func findCMD(base string) (map[string]string, error) {
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {

Loading…
Cancel
Save