Repository navigation
feat(output): add csv output format #56
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright (c) 2026 CrowdStrike, Inc. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE. | ||
|
|
||
| package output | ||
|
|
||
| import ( | ||
| "encoding/csv" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "reflect" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ErrNoTableDefinition is returned when a format that renders columns (such | ||
| // as CSV) is requested from a command that has no TableDefinition. | ||
| var ErrNoTableDefinition = errors.New("this command does not support column-based output") | ||
|
|
||
| // ErrUnknownColumns is returned by the CSV printer when --columns names no | ||
| // known column, so a typo fails loudly instead of producing an empty file. | ||
| var ErrUnknownColumns = errors.New("no known columns selected") | ||
|
|
||
| // csvPrinter writes one record per item using the TableDefinition headers and | ||
| // RowFunc, with RFC 4180-style quoting and LF line endings. Values are written | ||
| // in full: the truncation and width options of the table format are ignored. | ||
| // Columns and NoHeaders are honoured. | ||
| type csvPrinter struct { | ||
| def *TableDefinition | ||
| opts *TableOptions | ||
| } | ||
|
|
||
| func (p *csvPrinter) Print(w io.Writer, data any) error { | ||
| if p.def == nil { | ||
| return fmt.Errorf("--output %s: %w", FormatCSV, ErrNoTableDefinition) | ||
| } | ||
|
|
||
| activeIdx := columnIndices(p.def.Headers, p.opts.Columns) | ||
| if len(activeIdx) == 0 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in f3c4e81. With |
||
| valid := make([]string, len(p.def.Headers)) | ||
| for i, h := range p.def.Headers { | ||
| valid[i] = strings.ToLower(h) | ||
| } | ||
| return fmt.Errorf("--columns %s: %w (valid columns: %s)", | ||
| strings.Join(p.opts.Columns, ","), ErrUnknownColumns, strings.Join(valid, ", ")) | ||
| } | ||
|
|
||
| cw := csv.NewWriter(w) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small thought, very optional: RFC 4180 specifies CRLF record endings, and
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| record := make([]string, len(activeIdx)) | ||
| write := func(cols []string) error { | ||
| for j, i := range activeIdx { | ||
| record[j] = "" | ||
| if i < len(cols) { | ||
| record[j] = cols[i] | ||
| } | ||
| } | ||
| // csv.Writer renders a lone empty field as a blank line, which | ||
| // readers (including csv.Reader) skip, dropping the row. Write an | ||
| // explicit empty quoted field instead. | ||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed and fixed in f3c4e81. I reproduced it against a live tenant: |
||
| } | ||
|
|
||
| if !p.opts.NoHeaders { | ||
| if err := write(p.def.Headers); err != nil { | ||
| return fmt.Errorf("writing CSV header: %w", err) | ||
| } | ||
| } | ||
|
|
||
| v := reflect.ValueOf(data) | ||
| if v.Kind() == reflect.Pointer { | ||
| v = v.Elem() | ||
| } | ||
| switch { | ||
| case !v.IsValid(): | ||
| // nil data or nil pointer: header only. | ||
| case v.Kind() == reflect.Slice: | ||
| for i := 0; i < v.Len(); i++ { | ||
| if err := write(p.def.RowFunc(v.Index(i).Interface())); err != nil { | ||
| return fmt.Errorf("writing CSV row: %w", err) | ||
| } | ||
| } | ||
| default: | ||
| if err := write(p.def.RowFunc(data)); err != nil { | ||
| return fmt.Errorf("writing CSV row: %w", err) | ||
| } | ||
| } | ||
|
|
||
| cw.Flush() | ||
| if err := cw.Error(); err != nil { | ||
| return fmt.Errorf("writing CSV: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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/cutand in older tools. An opt-in--bomflag can come later if people ask.