Skip to content

fix: handle buffer scan more graciously and print to terminal #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 25, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions launchpad/pkg/flagd.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func StartFlagd(config string) error {
flagdLock.Unlock()
ready := make(chan bool)

go monitorOutput(stdout, ready)
go monitorOutput(stderr, ready)
go monitorOutput(stdout, ready, "stdout")
go monitorOutput(stderr, ready, "stderr")

select {
case success := <-ready:
Expand Down Expand Up @@ -154,19 +154,27 @@ func stopFlagDWithoutLock() error {
return fmt.Errorf("failed to stop flagd: %v", err)
}
flagdCmd = nil
fmt.Println("flagd stopped")
}
return nil
}

func monitorOutput(pipe io.ReadCloser, ready chan bool) {
func monitorOutput(pipe io.ReadCloser, ready chan bool, info string) {
scanner := bufio.NewScanner(pipe)
//adjust the capacity to your need (max characters in line)
const maxCapacity = 512 * 1024
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)
started := false

for scanner.Scan() {
line := scanner.Text()
fmt.Println("[flagd]:", line)
if ready != nil && strings.Contains(line, "listening at") {
fmt.Println("[flagd ", info, "]:", line)
if ready != nil && !started && strings.Contains(line, "listening at") {
ready <- true
close(ready)
return
fmt.Println("flagd started properly found logline with 'listening at'")
started = true
}
}
}