Skip to content

Commit

Permalink
Add shell struct to make shell command run
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Apr 19, 2018
1 parent 40a4a88 commit eb377ae
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ func (c CLI) run() int {
)
args = append(args, url)

return c.exit(runCommand(env.Binary, args))
s := newShell(env.Binary, args)
return c.exit(s.run())
}

func (c CLI) debug(a ...interface{}) {
Expand All @@ -147,11 +148,32 @@ func (c CLI) getURL() string {
return c.urls[0].String()
}

func runCommand(command string, args []string) error {
type shell struct {
stdin io.Reader
stdout io.Writer
stderr io.Writer
env map[string]string
command string
args []string
}

func newShell(command string, args []string) shell {
return shell{
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
env: map[string]string{},
command: command,
args: args,
}
}

func (s shell) run() error {
command := s.command
if _, err := exec.LookPath(command); err != nil {
return err
}
for _, arg := range args {
for _, arg := range s.args {
command += " " + arg
}
var cmd *exec.Cmd
Expand All @@ -160,8 +182,11 @@ func runCommand(command string, args []string) error {
} else {
cmd = exec.Command("sh", "-c", command)
}
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = s.stderr
cmd.Stdout = s.stdout
cmd.Stdin = s.stdin
for k, v := range s.env {
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", k, v))
}
return cmd.Run()
}

0 comments on commit eb377ae

Please sign in to comment.