Skip to content

feat(output): add csv output format - #56

Open
redhatrises wants to merge 2 commits into
CrowdStrike:mainfrom
redhatrises:feat/csv-output
Open

redhatrises wants to merge 2 commits into
CrowdStrike:mainfrom
redhatrises:feat/csv-output

Conversation

@redhatrises

Copy link
Copy Markdown
Contributor

Add --output csv to every command that uses the shared output flag. Rows come from the command's TableDefinition and are written with encoding/csv (RFC 4180 quoting). CSV never truncates values, so --max-col-width and --no-truncate are ignored; --columns and --no-headers apply as they do for the table. An empty result prints only the header row. A printer without a TableDefinition returns ErrNoTableDefinition instead of printing nothing.

Cell values are written exactly as returned. The usage docs warn about spreadsheet formula injection.

@eeran-cs eeran-cs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, the CSV support looks really nice! Leaning on encoding/csv makes sense, and I tested commas, quotes, LF/CR/CRLF, leading spaces, tabs, NUL and invalid UTF-8 locally. They all round-trip cleanly. I left a few small notes inline, mostly ideas for extra test cases plus one edge case I hit. None of these are meant to block, so feel free to push back on any of them.

Comment thread pkg/output/csv.go
record[j] = cols[i]
}
}
return cw.Write(record)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hit one small edge case here while testing: when the record has exactly one field and it's empty (e.g. --columns resource_id on an item with no resource ID), csv.Writer writes a bare blank line instead of "". Many readers, including csv.Reader and pandas by default, skip blank lines, so the row can quietly go missing and the rows after it shift up.

One possible approach, if it seems reasonable:

if len(record) == 1 && record[0] == "" {
	cw.Flush()
	if err := cw.Error(); err != nil {
		return err
	}
	_, err := io.WriteString(w, "\"\"\n")
	return err
}
return cw.Write(record)

A matching test case might be nice too, e.g. Columns: []string{"note"} with Note: "", expecting "NOTE\n\"\"\n".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed and fixed in f3c4e81. I reproduced it against a live tenant: fcs assets list --output csv --columns name (every asset there has an empty name) wrote NAME\n\n\n..., and csv.Reader read back 1 record out of 6. pandas drops those rows too. A one-field record with an empty value is now written as "". I added your test case, plus a csv.Reader round-trip test with a single column, which fails without the fix.

Comment thread pkg/output/csv.go
return nil
}

cw := csv.NewWriter(w)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small thought, very optional: RFC 4180 specifies CRLF record endings, and csv.Writer defaults to LF. Most parsers handle LF fine, so we could set cw.UseCRLF = true to match the docs exactly, or just soften the docs to "RFC 4180-style quoting". Either seems fine to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with softening the docs (f3c4e81). They now say "RFC 4180-style quoting" with LF line endings, and so does the type comment. This output mostly goes to stdout and through pipes, where CRLF leaves a trailing \r on the last field for cut/awk/grep. pandas, DuckDB and csv.Reader all read LF without any trouble.

Comment thread pkg/output/csv_test.go
want: "ID,NAME,NOTE\na1,\"say \"\"hi\"\"\",\n",
},
{
name: "embedded newline is quoted",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This table is great. If you're open to it, a couple more cases near this one could help lock in line-break handling:

  • a lone carriage return: "line1\rline2" → quoted
  • CRLF inside a value: "line1\r\nline2" → quoted, written as-is

It might also be worth adding a small round-trip test that parses the output back with csv.NewReader and compares it to the input values. That would cover escaping more broadly than exact string matching. (Heads-up if you add it: csv.Reader turns \r\n inside quoted fields into \n, so that one case would need special handling in the comparison.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added both (f3c4e81). There are table cases for line1\rline2 and line1\r\nline2, and TestCSVPrinter_RoundTrip writes 17 tricky values (empty, quotes, commas, leading/trailing spaces, tab, LF/CR/CRLF, non-ASCII, =1+1) and parses them back with csv.NewReader, once with all columns and once with a single column. As you said, the comparison turns \r\n into \n first.

Comment thread pkg/output/csv_test.go
want: "ID,NAME,NOTE\n,,\n",
},
{
name: "formula-like values are written unchanged",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to defer to you on this one. Since a lot of this data (resource names, tags, descriptions) can be set by whoever controls the cloud account, I wonder if we'd want to neutralize leading =, +, -, @, tab and CR by default (OWASP suggests putting a ' in front of them), maybe with an opt-out flag for raw output. If we go that way, this case would just need its expected value updated, plus a case for the opt-out. If you'd rather keep it as-is for now, the docs caution already covers it nicely.

@redhatrises redhatrises Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm keeping raw values plus the docs caution for now. Prefixing ' by default changes the data for script users. I checked that pandas and DuckDB both read '-2 back as the literal string '-2, so negative numbers, +1, @user and so on would be altered for the main audience of a CLI. Python's CSV export also writes values raw. If spreadsheet users ask for it, an opt-in flag (e.g. --csv-safe) would be a good follow-up. I left this test as is.

Comment thread docs/usage.md
falcon fcs assets list --output csv --columns id,name,provider --no-headers
```

> **Caution:** CSV values are written exactly as returned by the API. A cell

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really appreciate the caution note here. One related idea: when Excel on Windows opens a CSV by double-click it assumes a legacy code page, so non-ASCII values (like the 東京 / café cases in the tests) can show up garbled. A short note about importing as UTF-8, or an opt-in BOM flag, might save someone a confusing moment. Totally fine as a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a note in f3c4e81: the output is UTF-8 without a BOM, and Excel on Windows users should use Data > From Text/CSV and pick UTF-8 (65001). I left out a BOM because it shows up as stray bytes on the first header for head/cut and in older tools. An opt-in --bom flag can come later if people ask.

Comment thread pkg/output/csv.go
}

activeIdx := columnIndices(p.def.Headers, p.opts.Columns)
if len(activeIdx) == 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nit, and it matches the table printer, so feel free to ignore: if --columns names only unknown columns we return with no output and no error. For CSV in a script, a short error listing the valid column names might be a little friendlier to debug.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in f3c4e81. With --output csv, a --columns list that matches nothing now returns ErrUnknownColumns and lists the valid names, e.g. --columns bogus: no known columns selected (valid columns: id, name, type, provider, region, account), and exits non-zero. I kept it to CSV for this PR. The table printer has the same problem (it prints blank lines), and I'd rather fix that in its own PR.

@eeran-cs eeran-cs left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(duplicate, please ignore)

Add `--output csv` to every command that uses the shared output flag.
Rows come from the command's TableDefinition and are written with
encoding/csv (RFC 4180 quoting). CSV never truncates values, so
--max-col-width and --no-truncate are ignored; --columns and
--no-headers apply as they do for the table. An empty result prints
only the header row. A printer without a TableDefinition returns
ErrNoTableDefinition instead of printing nothing.

Cell values are written exactly as returned. The usage docs warn about
spreadsheet formula injection.
- Write a lone empty field as `""` instead of a blank line. csv.Writer
  emits an empty line for a one-column record with an empty value, and
  readers such as csv.Reader and pandas skip blank lines, which silently
  dropped rows (e.g. `--columns name` on assets with no name).
- Return ErrUnknownColumns, listing the valid column names, when
  --columns matches no column. Previously CSV printed nothing and
  exited 0.
- Describe the format as RFC 4180-style quoting with LF line endings
  rather than claiming strict RFC 4180, which requires CRLF.
- Document how to open UTF-8 CSV in Excel on Windows.
- Add test cases for lone CR and CRLF in values, and a csv.Reader
  round-trip test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants