From a9ebe24220de0699e8dbca9c8ee575e8f85959da Mon Sep 17 00:00:00 2001 From: Takuo Kitame Date: Mon, 8 Jan 2024 00:31:16 +0900 Subject: [PATCH] feat(output): add `--stdout.iterations` option --- output/stdout.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/output/stdout.go b/output/stdout.go index e435ac5..7c61ace 100644 --- a/output/stdout.go +++ b/output/stdout.go @@ -8,6 +8,7 @@ import ( "reflect" "strings" "sync" + "sync/atomic" "time" "github.com/northeye/chissoku/types" @@ -16,6 +17,10 @@ import ( // Stdout outputter for Stdout type Stdout struct { Base + Iterations int64 `help:"Inavtive on maximum iterations INT"` + + // iteration counter + count atomic.Int64 // close close func() @@ -85,5 +90,14 @@ func (s *Stdout) write(d *types.Data) { slog.Error("json.Marshal", "error", err, "outputter", s.Name()) return } - fmt.Println(string(b)) + if s.Iterations <= 0 { + fmt.Println(string(b)) + return + } + if i := s.count.Add(1); i <= s.Iterations { + fmt.Println(string(b)) + if i == s.Iterations { + s.cancel() + } + } }