Skip to content

Commit

Permalink
Rename -since to -days and add -token-file flag
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Apr 19, 2023
1 parent d7e5fdc commit 4097247
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ sudo mv $HOME/.arkade/bin/actions-usage /usr/local/bin/
## Output

```bash
actions-usage --org openfaas --token $(cat ~/pat.txt) --days 28
actions-usage --org openfaas --token-file ~/pat.txt --days 28

Usage report generated by self-actuated/actions-usage.

Expand All @@ -59,7 +59,7 @@ Total usage: 8h24m21s (504 mins)
As a user:

```bash
actions-usage --user alexellis --token $(cat ~/pat.txt)
actions-usage --user alexellis --token-file ~/pat.txt
```

## Development
Expand All @@ -70,7 +70,7 @@ All changes must be proposed with an Issue prior to working on them or sending a
git clone https://github.com/actuated/actions-usage
cd actions-usage

go run . --org actuated-samples --token $(cat ~/pat.txt)
go run . --org actuated-samples --token-file ~/pat.txt
```

## Author
Expand Down
28 changes: 19 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ import (
func main() {

var (
orgName, userName, token string
since int
punchCard bool
orgName, userName, token, tokenFile string
days int
punchCard bool
)

flag.StringVar(&orgName, "org", "", "Organization name")
flag.StringVar(&userName, "user", "", "User name")
flag.StringVar(&token, "token", "", "GitHub token")
flag.StringVar(&tokenFile, "token-file", "", "Path to the file containing the GitHub token")

flag.BoolVar(&punchCard, "punch-card", false, "Show punch card with breakdown of builds per day")
flag.IntVar(&since, "since", 30, "Since when to fetch the data (in days)")
flag.IntVar(&days, "days", 30, "How many days of data to query from the GitHub API")

flag.Parse()

if len(tokenFile) > 0 {
tokenBytes, err := os.ReadFile(tokenFile)
if err != nil {
log.Fatal(err)
}
token = strings.TrimSpace(string(tokenBytes))
}

auth := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
Expand All @@ -44,7 +54,7 @@ func main() {
}

client := github.NewClient(auth)
created := time.Now().AddDate(0, 0, -since)
created := time.Now().AddDate(0, 0, -days)
format := "2006-01-02"
createdQuery := ">=" + created.Format(format)

Expand Down Expand Up @@ -77,7 +87,7 @@ func main() {
"skipped": 0,
}

fmt.Printf("Fetching last %d days of data (created>=%s)\n", since, created.Format("2006-01-02"))
fmt.Printf("Fetching last %d days of data (created>=%s)\n", days, created.Format("2006-01-02"))

var repos, allRepos []*github.Repository
var res *github.Response
Expand Down Expand Up @@ -243,8 +253,8 @@ func main() {
entity = userName
}

days := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
fmt.Printf("\nGenerated by: https://github.com/self-actuated/actions-usage\nReport for %s - last: %d days.\n\n", entity, since)
daysOfWEek := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
fmt.Printf("\nGenerated by: https://github.com/self-actuated/actions-usage\nReport for %s - last: %d days.\n\n", entity, days)
fmt.Printf("Total repos: %d\n", len(allRepos))
fmt.Printf("Total private repos: %d\n", totalPrivate)
fmt.Printf("Total public repos: %d\n", totalPublic)
Expand All @@ -271,7 +281,7 @@ func main() {
if punchCard {
w := tabwriter.NewWriter(os.Stdout, 15, 4, 1, ' ', tabwriter.TabIndent)
fmt.Fprintln(w, "Day\tBuilds")
for _, day := range days {
for _, day := range daysOfWEek {
fmt.Fprintf(w, "%s\t%d\n", day, buildsPerDay[day])
}
fmt.Fprintf(w, "%s\t%d\n", "Total", totalJobs)
Expand Down

0 comments on commit 4097247

Please sign in to comment.