From 46f69448df21e6742769d0fde43763822b070fad Mon Sep 17 00:00:00 2001 From: Marcin Hlybin Date: Thu, 31 Oct 2024 16:57:45 +0100 Subject: [PATCH] Support writing from hooks to fd 3 for warnings --- addons/hooks.go | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/addons/hooks.go b/addons/hooks.go index 388217c..64059ff 100644 --- a/addons/hooks.go +++ b/addons/hooks.go @@ -67,27 +67,39 @@ func (h *Hook) executeCommand() error { return fmt.Errorf("failed to get stderr pipe: %w", err) } + // Create fd 3 for warnings + warnRead, warnWrite, err := os.Pipe() + if err != nil { + return fmt.Errorf("failed to create pipe for warnings (fd 3): %w", err) + } + + cmd.ExtraFiles = []*os.File{warnWrite} + + var wg sync.WaitGroup + // Start the command if err := cmd.Start(); err != nil { + warnWrite.Close() return fmt.Errorf("failed to execute command: %w", err) } - var wg sync.WaitGroup + warnWrite.Close() - // Print stdout and stderr - wg.Add(1) + // Start reading stdout, stderr and fd 3 + wg.Add(3) go h.printStdout(stdout, &wg) - wg.Add(1) go h.printStderr(stderr, &wg) + go h.printWarn(warnRead, &wg) + + // Wait for all goroutines to finish + wg.Wait() // Wait for the command to finish - if err := cmd.Wait(); err != nil { + err = cmd.Wait() + if err != nil { return fmt.Errorf("%s hook execution failed: %w", h.Name, err) } - // Wait for all goroutines to finish - wg.Wait() - return nil } @@ -114,3 +126,15 @@ func (h *Hook) printStderr(pipe io.Reader, wg *sync.WaitGroup) { logger.Error("Error reading stderr: %v", err) } } + +func (h *Hook) printWarn(pipe io.Reader, wg *sync.WaitGroup) { + defer wg.Done() + scanner := bufio.NewScanner(pipe) + + for scanner.Scan() { + logger.Warning(scanner.Text()) + } + if err := scanner.Err(); err != nil { + logger.Error("Error reading warnings: %v", err) + } +}