Skip to content

Commit

Permalink
feat: add csv support for contributors insights
Browse files Browse the repository at this point in the history
  • Loading branch information
k1nho committed Oct 16, 2023
1 parent c33e3b0 commit 60566d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 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,29 @@ 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)
err := writer.WriteAll([][]string{{"Repository URl", "New Contributors", "Recent Contributors", "Alumni Contributors", "Repeat Contributors"}, {
cis[0].RepoURL, strconv.Itoa(len(cis[0].New)), strconv.Itoa(len(cis[0].Recent)), strconv.Itoa(len(cis[0].Alumni)), strconv.Itoa(len(cis[0].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 60566d6

Please sign in to comment.