Skip to content

Commit

Permalink
feat: --csv-path
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Jul 15, 2021
1 parent 074fcc1 commit f736548
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 Carlos Alexandro Becker
Copyright (c) 2016-2021 Carlos Alexandro Becker

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
56 changes: 52 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"encoding/csv"
"fmt"
"io"
"os"
"path/filepath"
"runtime/debug"
"sort"
"strings"
"time"

Expand All @@ -16,10 +20,14 @@ import (
)

var (
token, organization, githubURL, since string
blacklist []string
top int
includeReviews bool
token string
organization string
githubURL string
since string
csvPath string
blacklist []string
top int
includeReviews bool
)

func main() {
Expand Down Expand Up @@ -65,6 +73,20 @@ Important notes:
return err
}
fmt.Println()

if csvPath != "" {
if err := os.MkdirAll(filepath.Dir(csvPath), 0755); err != nil {
return fmt.Errorf("failed to create csv file: %w", err)
}
f, err := os.OpenFile(csvPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to create csv file: %w", err)
}
if err := writeCsv(allStats, includeReviews, f); err != nil {
return fmt.Errorf("failed to create csv file: %w", err)
}
}

printHighlights(allStats, top, includeReviews)
return nil
},
Expand Down Expand Up @@ -115,6 +137,7 @@ Important notes:
rootCmd.Flags().StringVar(&githubURL, "github-url", "", "custom github base url (if using github enterprise)")
rootCmd.Flags().StringVar(&since, "since", "0s", "time to look back to gather info (0s means everything)")
rootCmd.Flags().BoolVar(&includeReviews, "include-reviews", false, "include pull request reviews in the stats")
rootCmd.Flags().StringVar(&csvPath, "csv-path", "", "path to write a csv file with all data collected")

rootCmd.AddCommand(versionCmd, docsCmd)

Expand Down Expand Up @@ -146,6 +169,31 @@ type statUI struct {
kind string
}

func writeCsv(s orgstats.Stats, includeReviews bool, f io.Writer) error {
w := csv.NewWriter(f)
defer w.Flush()
headers := []string{"login", "commits", "lines-added", "lines-removed"}
if includeReviews {
headers = append(headers, "reviews")
}
if err := w.Write(headers); err != nil {
return fmt.Errorf("failed to write csv: %w", err)
}
logins := s.Logins()
sort.Strings(logins)
for _, login := range logins {
stat := s.For(login)
record := []string{login, fmt.Sprintf("%d", stat.Commits), fmt.Sprintf("%d", stat.Additions), fmt.Sprintf("%d", stat.Deletions)}
if includeReviews {
record = append(record, fmt.Sprintf("%d", stat.Reviews))
}
if err := w.Write(record); err != nil {
return fmt.Errorf("failed to write csv: %w", err)
}
}
return w.Error()
}

func printHighlights(s orgstats.Stats, top int, includeReviews bool) {
data := []statUI{
{
Expand Down
12 changes: 12 additions & 0 deletions orgstats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ type Stats struct {
since time.Time
}

func (s Stats) Logins() []string {
logins := make([]string, 0, len(s.data))
for login := range s.data {
logins = append(logins, login)
}
return logins
}

func (s Stats) For(login string) Stat {
return s.data[login]
}

// NewStats return a new Stats map
func NewStats(since time.Time) Stats {
return Stats{
Expand Down

0 comments on commit f736548

Please sign in to comment.