Skip to content

Commit

Permalink
Allow outputting raw values when outputting only a single column.
Browse files Browse the repository at this point in the history
Follows #44 re the details:
* Uses the `--raw-output` / `-r` flag.
* Only applies to the `select` subcommand when selecting only a single column.
  • Loading branch information
aotimme committed Dec 30, 2022
1 parent 254e705 commit cf64ecc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
23 changes: 17 additions & 6 deletions cmd/output_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ type OutputCsvWriter interface {
type OutputCsv struct {
writeBom bool
hasWrittenHeader bool
writer *csv.Writer
csvWriter *csv.Writer
file *os.File
writeRaw bool
}

func NewOutputCsvFromInputCsv(inputCsv *InputCsv) (oc *OutputCsv) {
Expand Down Expand Up @@ -46,16 +48,21 @@ func NewOutputCsv() (oc *OutputCsv) {

func NewOutputCsvFromFile(file *os.File) (oc *OutputCsv) {
oc = new(OutputCsv)
oc.writer = csv.NewWriter(file)
oc.file = file
oc.csvWriter = csv.NewWriter(file)
delimiter := os.Getenv("GOCSV_DELIMITER")
if delimiter != "" {
oc.writer.Comma = GetDelimiterFromString(delimiter)
oc.csvWriter.Comma = GetDelimiterFromString(delimiter)
}
return
}

func (oc *OutputCsv) SetDelimiter(delimiter rune) {
oc.writer.Comma = delimiter
oc.csvWriter.Comma = delimiter
}

func (oc *OutputCsv) SetWriteRaw(writeRaw bool) {
oc.writeRaw = writeRaw
}

func (oc *OutputCsv) Write(row []string) error {
Expand All @@ -72,14 +79,18 @@ func (oc *OutputCsv) Write(row []string) error {
}

func (oc *OutputCsv) writeRow(row []string) (err error) {
err = oc.writer.Write(row)
if oc.writeRaw && len(row) == 1 {
oc.file.WriteString(row[0] + "\n")
return
}
err = oc.csvWriter.Write(row)
if err != nil {
return
}
// It is less efficient to flush after every write, but doing so
// keeps the output flowing. Otherwise it could look "jumpy" or
// like it's not working at times when there is no visible output
// while working on a large file.
oc.writer.Flush()
oc.csvWriter.Flush()
return
}
4 changes: 4 additions & 0 deletions cmd/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type SelectSubcommand struct {
columnsString string
exclude bool
rawOutput bool
}

func (sub *SelectSubcommand) Name() string {
Expand All @@ -25,11 +26,14 @@ func (sub *SelectSubcommand) SetFlags(fs *flag.FlagSet) {
fs.StringVar(&sub.columnsString, "columns", "", "Columns to select")
fs.StringVar(&sub.columnsString, "c", "", "Columns to select (shorthand)")
fs.BoolVar(&sub.exclude, "exclude", false, "Whether to exclude the specified columns")
fs.BoolVar(&sub.rawOutput, "raw-output", false, "Whether to output as raw lines (no CSV formatting) -- only applies if select returns one column")
fs.BoolVar(&sub.rawOutput, "r", false, "Whether to output as raw lines (no CSV formatting) -- only applies if select returns one column (shorthand)")
}

func (sub *SelectSubcommand) Run(args []string) {
inputCsvs := GetInputCsvsOrPanic(args, 1)
outputCsv := NewOutputCsvFromInputCsvs(inputCsvs)
outputCsv.SetWriteRaw(sub.rawOutput)
sub.RunSelect(inputCsvs[0], outputCsv)
}

Expand Down

0 comments on commit cf64ecc

Please sign in to comment.