Last active
April 13, 2021 20:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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