From 966dc7ac298e7c202b02a167262492146dedfc8e Mon Sep 17 00:00:00 2001 From: rwxrob Date: Sat, 9 Apr 2022 17:26:03 -0400 Subject: [PATCH] Add Z.Out, capture shell command output --- z/exec.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/z/exec.go b/z/exec.go index 6b1f357..f5305c8 100644 --- a/z/exec.go +++ b/z/exec.go @@ -5,6 +5,7 @@ package Z import ( "fmt" + "log" "os" "os/exec" "syscall" @@ -50,3 +51,22 @@ func Exec(args ...string) error { cmd.Stderr = os.Stderr return cmd.Run() } + +// Out returns the standard output of the executed command as +// a string. Errors are logged but not returned. +func Out(args ...string) string { + if len(args) == 0 { + log.Println("missing name of executable") + return "" + } + path, err := exec.LookPath(args[0]) + if err != nil { + log.Println(err) + return "" + } + out, err := exec.Command(path, args[1:]...).Output() + if err != nil { + log.Println(err) + } + return string(out) +}