Skip to content

Commit

Permalink
fix: error usage of stderr/out
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Nov 15, 2024
1 parent a4d65b5 commit 6956d27
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ func ExecuteCommand(logger *Logger, command string, description string) (*Comman
return nil, fmt.Errorf("failed to start command: %v", err)
}

// Create buffers to store complete output
var stdoutBuilder, stderrBuilder strings.Builder

// Read stdout in real-time
Expand All @@ -259,7 +258,6 @@ func ExecuteCommand(logger *Logger, command string, description string) (*Comman
line := scanner.Text()
fmt.Println(line)
stdoutBuilder.WriteString(line + "\n")
logger.Info(line)
}
}()

Expand All @@ -268,13 +266,20 @@ func ExecuteCommand(logger *Logger, command string, description string) (*Comman
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
line := scanner.Text()
fmt.Println("ERROR:", line)
stderrBuilder.WriteString(line + "\n")
logger.Error(line, nil)
if strings.Contains(line, "error") || strings.Contains(line, "Error") {
fmt.Println("ERROR:", line)
stderrBuilder.WriteString(line + "\n")
} else {
fmt.Println(line)
stdoutBuilder.WriteString(line + "\n")
}
}
}()

if err := cmd.Wait(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() != 0 {
return nil, fmt.Errorf("command failed with exit code %d: %v", exitErr.ExitCode(), err)
}
return nil, fmt.Errorf("command failed: %v", err)
}

Expand Down

0 comments on commit 6956d27

Please sign in to comment.