Skip to content

Commit

Permalink
Merge pull request #53 from k1nho/feature/add-csv-support
Browse files Browse the repository at this point in the history
feat: add csv support for contributors insights
  • Loading branch information
jpmcb authored Oct 18, 2023
2 parents cb28f47 + 51c1897 commit ec80310
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/insights/contributors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package insights

import (
"bytes"
"context"
"encoding/csv"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -134,11 +136,39 @@ func (cis contributorsInsightsSlice) BuildOutput(format string) (string, error)
return utils.OutputJSON(cis)
case constants.OutputYAML:
return utils.OutputYAML(cis)
case constants.OuputCSV:
return cis.OutputCSV()
default:
return "", fmt.Errorf("unknown output format %s", format)
}
}

func (cis contributorsInsightsSlice) OutputCSV() (string, error) {
if len(cis) == 0 {
return "", fmt.Errorf("repository is either non-existent or has not been indexed yet")
}
b := new(bytes.Buffer)
writer := csv.NewWriter(b)

// write headers
err := writer.WriteAll([][]string{{"Repository URL", "New Contributors", "Recent Contributors", "Alumni Contributors", "Repeat Contributors"}})
if err != nil {
return "", err
}

// write records
for _, ci := range cis {
err := writer.WriteAll([][]string{{ci.RepoURL, strconv.Itoa(len(ci.New)), strconv.Itoa(len(ci.Recent)),
strconv.Itoa(len(ci.Alumni)), strconv.Itoa(len(ci.Repeat))}})

if err != nil {
return "", err
}
}

return b.String(), nil
}

func (cis contributorsInsightsSlice) OutputTable() (string, error) {
tables := make([]string, 0, len(cis))
for i := range cis {
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ const (
OutputJSON = "json"
OutputTable = "table"
OutputYAML = "yaml"
OuputCSV = "csv"
)

0 comments on commit ec80310

Please sign in to comment.