Skip to content

Commit

Permalink
Merge pull request #15 from boyter/CSV
Browse files Browse the repository at this point in the history
Add in CSV output support
  • Loading branch information
boyter authored Jun 13, 2018
2 parents fd167cd + 3bd5e7d commit dccfcc2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "format, f",
Usage: "Set output format [possible values: tabular, wide, json]",
Usage: "Set output format [possible values: tabular, wide, json, csv]",
Destination: &processor.Format,
Value: "tabular",
},
Expand Down
39 changes: 39 additions & 0 deletions processor/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sort"
"strings"
"time"
"encoding/csv"
"bytes"
)

var tabularShortBreak = "-------------------------------------------------------------------------------\n"
Expand Down Expand Up @@ -120,12 +122,49 @@ func toJson(input *chan *FileJob) string {
return string(jsonString)
}

func toCSV(input *chan *FileJob) string {
records := [][]string{{
"Language",
"Location",
"Filename",
"Lines",
"Code",
"Comments",
"Blanks",
"Complexity"},
}

for result := range *input {
records = append(records, []string{
result.Language,
result.Location,
result.Filename,
fmt.Sprint(result.Lines),
fmt.Sprint(result.Code),
fmt.Sprint(result.Comment),
fmt.Sprint(result.Blank),
fmt.Sprint(result.Complexity)})
}


b := &bytes.Buffer{}
w := csv.NewWriter(b)
w.WriteAll(records)
w.Flush()

return b.String()
}



func fileSummerize(input *chan *FileJob) string {
switch {
case More || strings.ToLower(Format) == "wide":
return fileSummerizeLong(input)
case strings.ToLower(Format) == "json":
return toJson(input)
case strings.ToLower(Format) == "csv":
return toCSV(input)
}

return fileSummerizeShort(input)
Expand Down

0 comments on commit dccfcc2

Please sign in to comment.