github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/cmd/bigslice/bigslicecmd/run.go (about) 1 package bigslicecmd 2 3 import ( 4 "context" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "strings" 9 10 "github.com/grailbio/base/must" 11 ) 12 13 // RunUsage is the usage message for the Run command. 14 const RunUsage = `usage: bigslice run [input] [flags] 15 16 Command run builds and then runs the provided package or files. See 17 "bigslice build -help" for more details. 18 ` 19 20 // Run executes the supplied arguments as a subprocess. If no arguments are 21 // supplied, Build(ctx, ".") is used to build the current package and run that. 22 func Run(ctx context.Context, args []string) { 23 var buildIndex int 24 for _, arg := range args { 25 if strings.HasPrefix(arg, "-") { 26 break 27 } 28 buildIndex++ 29 } 30 var binary string 31 if buildIndex == 0 { 32 binary = Build(ctx, []string{"."}, "") 33 } else { 34 binary = Build(ctx, args[:buildIndex], "") 35 } 36 if !filepath.IsAbs(binary) { 37 // Build may return a relative path that may not include any path 38 // separators. If the name passed to CommandContext has no path 39 // separators, $PATH is searched instead of using the relative path. 40 // Ensure that the name has a path separator. 41 binary = "." + string(os.PathSeparator) + binary 42 } 43 cmd := exec.CommandContext(ctx, binary, args[buildIndex:]...) 44 cmd.Stderr = os.Stderr 45 cmd.Stdout = os.Stdout 46 cmd.Stdin = os.Stdin 47 must.Nil(cmd.Run()) 48 must.Nil(os.Remove(binary)) 49 }