Skip to content

Instantly share code, notes, and snippets.

@viggy28
Last active April 13, 2021 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viggy28/82ba862ff7117be85c55fef64c9d3d58 to your computer and use it in GitHub Desktop.
Save viggy28/82ba862ff7117be85c55fef64c9d3d58 to your computer and use it in GitHub Desktop.
checkPipeExecError checks for error in the pipe irrespective of what the command is the function returns the actual error message
package main
import (
"fmt"
"github.com/bitfield/script"
"strings"
)
func main() {
fmt.Println("Hello World")
commands := []string{"dateee", "man bogus"}
for _, command := range commands {
p := script.Exec(command)
if errString := checkError(p); errString != "" {
fmt.Println(strings.TrimSpace(errString))
}
}
}
func checkError(p *script.Pipe) string {
if p.Error() != nil {
var outputError string
outputError = p.Error().Error()
if strings.Contains(outputError, "exit status") {
p.SetError(nil)
outputError, _ = p.String()
}
return outputError
}
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment