From 5ee76f5ece4bd8641c6903b7b43fc1f3051589a1 Mon Sep 17 00:00:00 2001 From: HikariKnight Date: Tue, 11 Apr 2023 14:40:10 +0200 Subject: [PATCH] move Run with stderr output to its own command --- pkg/command/command.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/command/command.go b/pkg/command/command.go index ef785f2..0d033d4 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -23,17 +23,40 @@ func Run(binary string, args ...string) ([]string, error) { // Read the output output, _ := io.ReadAll(&stdout) - outerr, _ := io.ReadAll(&stderr) // Get the output outputs := []string{} outputs = append(outputs, string(output)) - outputs = append(outputs, string(outerr)) // Return our list of items return outputs, err } +// This function is just like command.Run() but also returns STDERR +func RunErr(binary string, args ...string) ([]string, []string, error) { + var stdout, stderr bytes.Buffer + + // Configure the ls-iommu c--ommand + cmd := exec.Command(binary, args...) + cmd.Stderr = &stderr + cmd.Stdout = &stdout + + // Execute the command + err := cmd.Run() + + // Read the output + output, _ := io.ReadAll(&stdout) + outerr, _ := io.ReadAll(&stderr) + + // Get the output + var outputs, outerrs []string + outputs = append(outputs, string(output)) + outerrs = append(outerrs, string(outerr)) + + // Return our list of items + return outputs, outerrs, err +} + // This functions runs the command "sudo -Sk -- echo", this forces sudo // to re-authenticate and lets us enter the password to STDIN // giving us the ability to run sudo commands