Skip to content

Commit

Permalink
feat(output): add --stdout.iterations option
Browse files Browse the repository at this point in the history
  • Loading branch information
takuo committed Jan 6, 2024
1 parent f58295c commit a5739cb
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions output/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// Stdout outputter for Stdout
type Stdout struct {
Base
Iterations int `help:"Inavtive on maximum iterations INT"`

close func() bool
}
Expand All @@ -31,9 +32,14 @@ func (s *Stdout) Initialize(ctx context.Context) error {
return true
})

go func() {
go func(iter int) {
var cur *types.Data
s.write(<-s.r) // output first data immediately
iter--
if iter == 0 {
s.Close()
return
}
tick := time.NewTicker(time.Second * time.Duration(s.Interval))
for {
select {
Expand All @@ -43,6 +49,13 @@ func (s *Stdout) Initialize(ctx context.Context) error {
}
s.write(cur)
cur = nil // dismiss
if iter > 0 {
iter--
if iter == 0 {
tick.Stop()
s.Close()
}
}
case d, more := <-s.r:
if !more {
slog.Debug("Output channel has been closed", "outputter", s.Name())
Expand All @@ -51,7 +64,7 @@ func (s *Stdout) Initialize(ctx context.Context) error {
cur = d
}
}
}()
}(s.Iterations)
return nil
}

Expand Down

0 comments on commit a5739cb

Please sign in to comment.