Skip to content

Commit

Permalink
feat(command): add commander
Browse files Browse the repository at this point in the history
  • Loading branch information
anarcher committed Mar 26, 2020
1 parent b1bec5c commit 9d4f794
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/command/commander.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package command

type Commander interface {
Run(name string, args ...string) (string, error)
}
39 changes: 39 additions & 0 deletions pkg/command/osexec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package command

import (
"bytes"
"io"
"log"
"os"
"os/exec"
)

type OSExec struct {
out bytes.Buffer
err bytes.Buffer
}

func NewOSExec() *OSExec {
e := &OSExec{}
return e
}

func (e *OSExec) Run(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
cmd.Stdout = &e.out
cmd.Stderr = &e.err

err := cmd.Run()
out := e.out.String()

{
if _, err := io.Copy(os.Stdout, &e.out); err != nil {
log.Print(err)
}
if _, err := io.Copy(os.Stderr, &e.err); err != nil {
log.Print(err)
}
}

return out, err
}

0 comments on commit 9d4f794

Please sign in to comment.