Skip to content

Commit

Permalink
TOOLS-896 Add --noHeaderLine option for export csv
Browse files Browse the repository at this point in the history
Add --noHeaderLine option for mongoexport. Give user an option to choose
output the list of field names at the first line.
  • Loading branch information
coding-hello authored and Gabriel Russell committed Jan 20, 2016
1 parent 314837d commit 05d0f1f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
13 changes: 10 additions & 3 deletions mongoexport/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,30 @@ type CSVExportOutput struct {
// NumExported maintains a running total of the number of documents written.
NumExported int64

// NoHeaderLine, if set, will export CSV data without a list of field names at the first line
NoHeaderLine bool

csvWriter *csv.Writer
}

// NewCSVExportOutput returns a CSVExportOutput configured to write output to the
// given io.Writer, extracting the specified fields only.
func NewCSVExportOutput(fields []string, out io.Writer) *CSVExportOutput {
func NewCSVExportOutput(fields []string, noHeaderLine bool, out io.Writer) *CSVExportOutput {
return &CSVExportOutput{
fields,
0,
noHeaderLine,
csv.NewWriter(out),
}
}

// WriteHeader writes a comma-delimited list of fields as the output header row.
func (csvExporter *CSVExportOutput) WriteHeader() error {
csvExporter.csvWriter.Write(csvExporter.Fields)
return csvExporter.csvWriter.Error()
if !csvExporter.NoHeaderLine {
csvExporter.csvWriter.Write(csvExporter.Fields)
return csvExporter.csvWriter.Error()
}
return nil
}

// WriteFooter is a no-op for CSV export formats.
Expand Down
23 changes: 20 additions & 3 deletions mongoexport/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,31 @@ func TestWriteCSV(t *testing.T) {
out := &bytes.Buffer{}

Convey("Headers should be written correctly", func() {
csvExporter := NewCSVExportOutput(fields, out)
csvExporter := NewCSVExportOutput(fields, false, out)
err := csvExporter.WriteHeader()
So(err, ShouldBeNil)
csvExporter.ExportDocument(bson.D{{"_id", "12345"}})
csvExporter.WriteFooter()
csvExporter.Flush()
rec, err := csv.NewReader(strings.NewReader(out.String())).Read()
So(err, ShouldBeNil)
So(rec, ShouldResemble, []string{"_id", "x", " y", "z.1.a"})
})

Convey("Headers should not be written", func() {
csvExporter := NewCSVExportOutput(fields, true, out)
err := csvExporter.WriteHeader()
So(err, ShouldBeNil)
csvExporter.ExportDocument(bson.D{{"_id", "12345"}})
csvExporter.WriteFooter()
csvExporter.Flush()
rec, err := csv.NewReader(strings.NewReader(out.String())).Read()
So(err, ShouldBeNil)
So(rec, ShouldResemble, []string{"12345", "", "", ""})
})

Convey("Exported document with missing fields should print as blank", func() {
csvExporter := NewCSVExportOutput(fields, out)
csvExporter := NewCSVExportOutput(fields, true, out)
csvExporter.ExportDocument(bson.D{{"_id", "12345"}})
csvExporter.WriteFooter()
csvExporter.Flush()
Expand All @@ -36,7 +53,7 @@ func TestWriteCSV(t *testing.T) {
})

Convey("Exported document with index into nested objects should print correctly", func() {
csvExporter := NewCSVExportOutput(fields, out)
csvExporter := NewCSVExportOutput(fields, true, out)
csvExporter.ExportDocument(bson.D{{"z", []interface{}{"x", bson.D{{"a", "T"}, {"B", 1}}}}})
csvExporter.WriteFooter()
csvExporter.Flush()
Expand Down
3 changes: 2 additions & 1 deletion mongoexport/mongoexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ func (exp *MongoExport) getExportOutput(out io.Writer) (ExportOutput, error) {
exportFields = append(exportFields, field)
}
}
return NewCSVExportOutput(exportFields, out), nil

return NewCSVExportOutput(exportFields, exp.OutputOpts.NoHeaderLine, out), nil
}
return NewJSONExportOutput(exp.OutputOpts.JSONArray, exp.OutputOpts.Pretty, out), nil
}
Expand Down
3 changes: 3 additions & 0 deletions mongoexport/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type OutputFormatOptions struct {

// Pretty displays JSON data in a human-readable form.
Pretty bool `long:"pretty" description:"output JSON formatted to be human-readable"`

// NoHeaderLine, if set, will export CSV data without a list of field names at the first line.
NoHeaderLine bool `long:"noHeaderLine" description:"export CSV data without a list of field names at the first line"`
}

// Name returns a human-readable group name for output format options.
Expand Down

0 comments on commit 05d0f1f

Please sign in to comment.