Skip to content

Commit

Permalink
feat: add jsonl output
Browse files Browse the repository at this point in the history
  • Loading branch information
justmiles committed Nov 8, 2022
1 parent c33f794 commit 077b86c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 53 deletions.
98 changes: 54 additions & 44 deletions lib/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Query struct {

// Format is an enumeration of available query output formats
// ENUM(
// json, csv, table, tsv, xlsx
// json, jsonl, csv, table, tsv, xlsx
// )
type Format int

Expand Down Expand Up @@ -139,8 +139,22 @@ func (q *Query) Execute() (*os.File, error) {
func (q *Query) RenderQueryResults(file *os.File) error {
var err error

if q.Format == FormatJson.String() {
var outFile *os.File

if q.OutputFile == "" {
outFile = os.Stdout
} else {
outFile, err = os.Create(q.OutputFile)
if err != nil {
return err
}
defer outFile.Close()
}

writer := bufio.NewWriter(outFile)
defer writer.Flush()

if q.Format == FormatJson.String() {
reader := csvmap.NewReader(file)
reader.Columns, err = reader.ReadHeader()
if err != nil {
Expand All @@ -152,15 +166,40 @@ func (q *Query) RenderQueryResults(file *os.File) error {
return fmt.Errorf("Unable to read query results from %q, %v", file.Name(), err)
}

output, _ := json.MarshalIndent(records, "", " ")

if q.OutputFile == "" {
fmt.Println(string(output))
} else {
return ioutil.WriteFile(q.OutputFile, output, 0644)
output, err := json.MarshalIndent(records, "", " ")
if err != nil {
return fmt.Errorf("Unable to marshal json %v", err)
}

writer.Write(output)
return nil

}

if q.Format == FormatJsonl.String() {

reader := csvmap.NewReader(file)
reader.Columns, err = reader.ReadHeader()
if err != nil {
return fmt.Errorf("Unable to read header from %q, %v", file.Name(), err)
}

for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("Unable to read query results from %q, %v", file.Name(), err)
}

output, err := json.Marshal(record)
if err != nil {
return fmt.Errorf("Unable to convert output to jsonl, %v", err)
}
writer.WriteString(string(output) + "\n")
}

}

if q.Format == FormatTable.String() {
Expand All @@ -175,18 +214,7 @@ func (q *Query) RenderQueryResults(file *os.File) error {
return fmt.Errorf("Unable to read query results from %q, %v", file.Name(), err)
}

var f *os.File

if q.OutputFile == "" {
f = os.Stdout
} else {
f, err = os.Create(q.OutputFile)
if err != nil {
return err
}
}

table := tablewriter.NewWriter(f)
table := tablewriter.NewWriter(outFile)
table.SetHeader(reader.Columns)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
Expand All @@ -200,33 +228,15 @@ func (q *Query) RenderQueryResults(file *os.File) error {
}

if q.Format == FormatCsv.String() {

sb, err := ioutil.ReadFile(file.Name())
records, err := ioutil.ReadFile(file.Name())
if err != nil {
return fmt.Errorf("Unable to read query results from %q, %v", file.Name(), err)
}

if q.OutputFile == "" {
fmt.Println(string(sb))
} else {
return ioutil.WriteFile(q.OutputFile, sb, 0644)
}

writer.Write(records)
}

if q.Format == FormatTsv.String() {
var w io.Writer

if q.OutputFile == "" {
w = os.Stdout
} else {
f, err := os.Create(q.OutputFile)
if err != nil {
return fmt.Errorf("Unable to create output file %q, %v", q.OutputFile, err)
}
defer f.Close()
w = bufio.NewWriter(f)
}

csvFile, err := os.Open(file.Name())
if err != nil {
Expand All @@ -235,10 +245,10 @@ func (q *Query) RenderQueryResults(file *os.File) error {
defer csvFile.Close()

reader := csv.NewReader(csvFile)
writer := csv.NewWriter(w)
defer writer.Flush()
w := csv.NewWriter(writer)
defer w.Flush()

writer.Comma = '\t'
w.Comma = '\t'

for {
record, err := reader.Read()
Expand All @@ -249,7 +259,7 @@ func (q *Query) RenderQueryResults(file *os.File) error {
return fmt.Errorf("Unable to read query results from %q, %v", file.Name(), err)
}

if err := writer.Write(record); err != nil {
if err := w.Write(record); err != nil {
return fmt.Errorf("error writing record to output %v", err)
}
}
Expand Down
22 changes: 13 additions & 9 deletions lib/query_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 077b86c

Please sign in to comment.