Skip to content

Commit 0bb7cf3

Browse files
committed
Added Passable CLI Arguments To the Run Command
Now you can pass arguments to the command you're running, and also now the STDOUT AND STDERR is shown but currently STDIN isn't Supported.
1 parent 1c97cab commit 0bb7cf3

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

src/commands/run.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,50 @@ import (
44
"bread/src/helpers/repos"
55
"bread/src/helpers/utils"
66

7+
"fmt"
78
"os"
8-
"os/exec"
9+
goCmd "github.com/go-cmd/cmd"
910
)
1011

1112
type RunCmd struct {
1213
Target string `arg:"" name:"target" help:"Target To Run" type:"string"`
14+
Arguments []string `arg:"" passthrough:"" optional:"" name:"arguments" help:"Argument to pass to the program" type:"string"`
15+
}
16+
17+
func executeCmd(target string, arguments []string) {
18+
options := goCmd.Options{
19+
Buffered: false,
20+
Streaming: true,
21+
}
22+
runCmd := goCmd.NewCmdOptions(options, target, arguments...)
23+
24+
// Print STDOUT and STDERR lines streaming from Cmd
25+
doneChan := make(chan struct{})
26+
go func() {
27+
defer close(doneChan)
28+
for runCmd.Stdout != nil || runCmd.Stderr != nil {
29+
select {
30+
case line, open := <-runCmd.Stdout:
31+
if !open {
32+
runCmd.Stdout = nil
33+
continue
34+
}
35+
fmt.Println(line)
36+
case line, open := <-runCmd.Stderr:
37+
if !open {
38+
runCmd.Stderr = nil
39+
continue
40+
}
41+
fmt.Fprintln(os.Stderr, line)
42+
}
43+
}
44+
}()
45+
46+
// Run and wait for Cmd to return, discard Status
47+
<-runCmd.Start()
48+
49+
// Wait for goroutine to print everything
50+
<-doneChan
1351
}
1452

1553
func (cmd *RunCmd) Run() (err error) {
@@ -39,8 +77,7 @@ func (cmd *RunCmd) Run() (err error) {
3977

4078
// Check if the FilePath Exist, Show error
4179
if _, err = os.Stat(targetFilePath); err == nil {
42-
command := exec.Command(targetFilePath)
43-
command.Run()
80+
executeCmd(targetFilePath, cmd.Arguments)
4481
return nil
4582
}
4683

@@ -53,6 +90,6 @@ func (cmd *RunCmd) Run() (err error) {
5390
// Print Signature Info If Exist.
5491
utils.ShowSignature(targetFilePath)
5592

56-
command := exec.Command(targetFilePath)
57-
return command.Run()
93+
executeCmd(targetFilePath, cmd.Arguments)
94+
return nil
5895
}

0 commit comments

Comments
 (0)