Skip to content

Commit 8c452cd

Browse files
committed
Flush CSV rows to stdout as each result is found (#319)
With -format csv, stdout rows sat in encoding/csv's 4 KB buffer until it filled or Finish ran, so a pipe saw no live results and a force quit lost every buffered row; text, JSON and JSONL are written as found. writeCSVRows now flushes the stdout CSV writer after each result. The -o file stays buffered behind its own bufio.Writer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Fixes #319
1 parent dd0e2a5 commit 8c452cd

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6363
- The names whose lookup timed out, was refused or failed are kept only when something reads them: for the `-inconclusive` file, and until the `-retry-inconclusive` pass has run. A scan without either only counts them, so under `-no-abort` or `-auto-rate` against a failing resolver memory no longer grows with every failed lookup (about 1 GB at 10M failures). When they are kept, a wordlist entry costs its index rather than its name. Counts, the report and `pkg/subenum`'s `Stats.Inconclusive` are unchanged; the `-resume` state lists the names only with `-inconclusive` (#300).
6464

6565
### Fixed
66+
- With `-format csv`, each row reaches stdout as its result is found, as with the other formats, instead of when 4 KB had built up or the run ended; a pipe sees live rows and a force quit loses none (#319).
6667
- With both `-max-queries` and `-max-duration`, a deadline that fires after the query cap no longer loses the chunk of names admitted but not yet handed out: they count as skipped, so tested plus skipped adds up to every candidate and progress reaches 100%, and the run is marked as stopped by the time budget. Children of a name found after a budget stop whose whole branch is excluded count as excluded, not skipped, so they cannot make a complete scan look incomplete (#317).
6768
- A `-r` resolver list line with a malformed or out-of-range port (`9.9.9.9:abc`, `:99999`, `:0`) is rejected when the file is read, naming the line, instead of failing every lookup at dial time; an explicit `-attempts 0` (flag, `SUBENUM_ATTEMPTS` or config) is rejected like any value below 1 instead of running as 1, and `-attempts 0 -retries 3` reports that both cannot be used (#318, thanks @bfs2021).
6869
- Takeover hints no longer look up a CNAME target or name server that `-exclude` puts out of scope, so excluded names are never queried on this path either. Such a target can still be marked `provider:<name>`, which needs no query, but never `dangling` or `dangling-ns` (#316).

‎internal/output/writer.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ func (w *Writer) writeCSVRows(res Result) {
375375
_ = w.csvFile.Write(row)
376376
}
377377
}
378+
// Stdout gets each result as it is found, as the other formats do, so a
379+
// pipe sees live rows and a force quit loses none (#319). The -o file
380+
// stays buffered behind its own bufio.Writer.
381+
if w.csvStdout != nil {
382+
w.csvStdout.Flush()
383+
}
378384
}
379385

380386
// Finish flushes any buffered or streamed structured output. It must be called

‎internal/output/writer_test.go‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ func TestNewFileWriterSkipsStdout(t *testing.T) {
115115
}
116116
}
117117

118+
// TestWriterCSVStdoutStreams covers #319: a CSV row reaches stdout when its
119+
// result is emitted, before Finish, as text, JSON and JSONL rows do.
120+
func TestWriterCSVStdoutStreams(t *testing.T) {
121+
var out bytes.Buffer
122+
w := New(nil, false, FormatCSV)
123+
w.SetStdout(&out)
124+
w.Emit(found("www.example.com", []dns.Record{{Type: "A", Value: "192.0.2.1"}}))
125+
if got, want := out.String(), "subdomain,type,value\nwww.example.com,A,192.0.2.1\n"; got != want {
126+
t.Fatalf("stdout before Finish = %q, want %q", got, want)
127+
}
128+
w.Emit(found("api.example.com", []dns.Record{{Type: "A", Value: "192.0.2.2"}}))
129+
if !strings.HasSuffix(out.String(), "api.example.com,A,192.0.2.2\n") {
130+
t.Fatalf("second row not written before Finish: %q", out.String())
131+
}
132+
if err := w.Finish(); err != nil {
133+
t.Fatal(err)
134+
}
135+
}
136+
118137
func TestWriterCSVEmptyRecords(t *testing.T) {
119138
tmp, err := os.CreateTemp("", "output-csv-empty-*.csv")
120139
if err != nil {
@@ -125,7 +144,7 @@ func TestWriterCSVEmptyRecords(t *testing.T) {
125144
bw := bufio.NewWriter(tmp)
126145
w := New(bw, false, FormatCSV)
127146
// A resolved domain with no records still gets one row with empty fields.
128-
// Finish must run inside the capture because csv.Writer buffers until flush.
147+
// Finish runs inside the capture so the header and rows are all flushed.
129148
out := captureStdout(t, func() {
130149
w.Emit(found("www.example.com", nil))
131150
if err := w.Finish(); err != nil {

0 commit comments

Comments
 (0)