Skip to content

Commit

Permalink
Merge pull request #118 from MH4GF/refactor-list-2
Browse files Browse the repository at this point in the history
refactor(lib): Remove environment variable dependencies from lib.List()
  • Loading branch information
masutaka authored Nov 2, 2023
2 parents 48a1394 + 914b967 commit 8ab32ef
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
11 changes: 10 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ var RootCmd = &cobra.Command{
Use: "github-nippou",
Short: "Print today's your GitHub activity for issues and pull requests",
Run: func(cmd *cobra.Command, args []string) {
if err := lib.List(sinceDate, untilDate, debug); err != nil {
list, err := lib.NewListFromCLI(sinceDate, untilDate, debug)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

lines, err := list.Collect()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println(lines)
},
}

Expand Down
74 changes: 52 additions & 22 deletions lib/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,84 @@ package lib

import (
"context"
"fmt"
"log"
"sync"
"time"

"github.com/google/go-github/github"
)

// List outputs formated GitHub events to stdout
func List(sinceDate, untilDate string, debug bool) error {
// List is a struct for collecting GitHub activities.
type List struct {
sinceDate string
untilDate string
user string
accessToken string
settingsGistID string
debug bool
}

// NewList returns a new List.
func NewList(sinceDate, untilDate, user, accessToken, settingsGistID string, debug bool) *List {
return &List{
sinceDate: sinceDate,
untilDate: untilDate,
user: user,
accessToken: accessToken,
settingsGistID: settingsGistID,
debug: debug,
}
}

// NewListFromCLI returns a new List from environment variables or git config.
func NewListFromCLI(sinceDate, untilDate string, debug bool) (*List, error) {
user, err := getUser()
if err != nil {
return err
return nil, err
}

accessToken, err := getAccessToken()
if err != nil {
return err
return nil, err
}
settingsGistID := getGistID()

return &List{
sinceDate: sinceDate,
untilDate: untilDate,
user: user,
accessToken: accessToken,
settingsGistID: settingsGistID,
debug: debug,
}, nil
}

sinceTime, err := getSinceTime(sinceDate)
// Collect collects GitHub activities.
func (l *List) Collect() (string, error) {
sinceTime, err := getSinceTime(l.sinceDate)
if err != nil {
log.Fatal(err)
return "", err
}

untilTime, err := getUntilTime(untilDate)
untilTime, err := getUntilTime(l.untilDate)
if err != nil {
log.Fatal(err)
return "", err
}

ctx := context.Background()
client := getClient(ctx, accessToken)
client := getClient(ctx, l.accessToken)

events, err := NewEvents(ctx, client, user, sinceTime, untilTime, debug).Collect()
events, err := NewEvents(ctx, client, l.user, sinceTime, untilTime, l.debug).Collect()
if err != nil {
return err
return "", err
}
var settings Settings
if err = settings.Init(getGistID(), accessToken); err != nil {
return err
if err = settings.Init(l.settingsGistID, l.accessToken); err != nil {
return "", err
}
format := NewFormat(ctx, client, settings, debug)
format := NewFormat(ctx, client, settings, l.debug)

parallelNum, err := getParallelNum()
if err != nil {
return err
return "", err
}

sem := make(chan int, parallelNum)
Expand All @@ -72,12 +104,10 @@ func List(sinceDate, untilDate string, debug bool) error {

allLines, err := format.All(lines)
if err != nil {
return err
return "", err
}

fmt.Print(allLines)

return nil
return allLines, nil
}

func getSinceTime(sinceDate string) (time.Time, error) {
Expand Down

0 comments on commit 8ab32ef

Please sign in to comment.