Skip to content

Commit

Permalink
Support writing from hooks to fd 3 for warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinhlybin committed Oct 31, 2024
1 parent 4cbd5d7 commit 46f6944
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions addons/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
}

0 comments on commit 46f6944

Please sign in to comment.