From 22cdfde7e910d18aa127ff71864df2ec85121aa5 Mon Sep 17 00:00:00 2001 From: Mariano Cano Date: Wed, 17 Apr 2019 18:45:14 -0700 Subject: [PATCH] Add method to get the output of a command. --- exec/exec.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/exec/exec.go b/exec/exec.go index 48ef2964..abced481 100644 --- a/exec/exec.go +++ b/exec/exec.go @@ -106,6 +106,19 @@ func Step(args ...string) ([]byte, error) { return out, nil } +// Command executes the given command with it's arguments and returns the +// standard output. +func Command(name string, args ...string) ([]byte, error) { + var stderr bytes.Buffer + cmd := exec.Command(name, args...) + cmd.Stderr = &stderr + out, err := cmd.Output() + if err != nil { + return nil, errors.Wrapf(err, "error running %s %s:\n%s", name, strings.Join(args, " "), stderr.String()) + } + return out, nil +} + func run(name string, arg ...string) (*exec.Cmd, chan int, error) { cmd := exec.Command(name, arg...) cmd.Stderr = os.Stderr