Skip to content

Commit 46f6944

Browse files
committed
Support writing from hooks to fd 3 for warnings
1 parent 4cbd5d7 commit 46f6944

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

addons/hooks.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,39 @@ func (h *Hook) executeCommand() error {
6767
return fmt.Errorf("failed to get stderr pipe: %w", err)
6868
}
6969

70+
// Create fd 3 for warnings
71+
warnRead, warnWrite, err := os.Pipe()
72+
if err != nil {
73+
return fmt.Errorf("failed to create pipe for warnings (fd 3): %w", err)
74+
}
75+
76+
cmd.ExtraFiles = []*os.File{warnWrite}
77+
78+
var wg sync.WaitGroup
79+
7080
// Start the command
7181
if err := cmd.Start(); err != nil {
82+
warnWrite.Close()
7283
return fmt.Errorf("failed to execute command: %w", err)
7384
}
7485

75-
var wg sync.WaitGroup
86+
warnWrite.Close()
7687

77-
// Print stdout and stderr
78-
wg.Add(1)
88+
// Start reading stdout, stderr and fd 3
89+
wg.Add(3)
7990
go h.printStdout(stdout, &wg)
80-
wg.Add(1)
8191
go h.printStderr(stderr, &wg)
92+
go h.printWarn(warnRead, &wg)
93+
94+
// Wait for all goroutines to finish
95+
wg.Wait()
8296

8397
// Wait for the command to finish
84-
if err := cmd.Wait(); err != nil {
98+
err = cmd.Wait()
99+
if err != nil {
85100
return fmt.Errorf("%s hook execution failed: %w", h.Name, err)
86101
}
87102

88-
// Wait for all goroutines to finish
89-
wg.Wait()
90-
91103
return nil
92104
}
93105

@@ -114,3 +126,15 @@ func (h *Hook) printStderr(pipe io.Reader, wg *sync.WaitGroup) {
114126
logger.Error("Error reading stderr: %v", err)
115127
}
116128
}
129+
130+
func (h *Hook) printWarn(pipe io.Reader, wg *sync.WaitGroup) {
131+
defer wg.Done()
132+
scanner := bufio.NewScanner(pipe)
133+
134+
for scanner.Scan() {
135+
logger.Warning(scanner.Text())
136+
}
137+
if err := scanner.Err(); err != nil {
138+
logger.Error("Error reading warnings: %v", err)
139+
}
140+
}

0 commit comments

Comments
 (0)