From 9d7f546610416336f14b8b6176e40023f2cb869a Mon Sep 17 00:00:00 2001 From: MH4GF Date: Mon, 30 Oct 2023 09:00:28 +0900 Subject: [PATCH 1/3] refactor(lib): Remove environment variable dependencies from lib.List() --- cmd/root.go | 8 +++++++- lib/list.go | 56 +++++++++++++++++++++++++++++------------------------ 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b037559..b443c0d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,10 +18,16 @@ 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 { + + // TODO + list := lib.NewList(sinceDate, untilDate, "user", "token", "gistID") + lines, err := list.Collect(debug) + if err != nil { fmt.Println(err) os.Exit(1) } + + fmt.Println(lines) }, } diff --git a/lib/list.go b/lib/list.go index 0bd3e24..2833409 100644 --- a/lib/list.go +++ b/lib/list.go @@ -2,52 +2,60 @@ 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 { - user, err := getUser() - if err != nil { - return err - } +// List is a struct for collecting GitHub activities. +type List struct { + sinceDate string + untilDate string + user string + accessToken string + settingsGistID string +} - accessToken, err := getAccessToken() - if err != nil { - return err +// NewList returns a new List. +func NewList(sinceDate, untilDate, user, accessToken, settingsGistID string) *List { + return &List{ + sinceDate: sinceDate, + untilDate: untilDate, + user: user, + accessToken: accessToken, + settingsGistID: settingsGistID, } +} - sinceTime, err := getSinceTime(sinceDate) +// Collect collects GitHub activities. +func (l *List) Collect(debug bool) (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, 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) parallelNum, err := getParallelNum() if err != nil { - return err + return "", err } sem := make(chan int, parallelNum) @@ -72,12 +80,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) { From 55eb6525454015b0e25d033220772e36dec50d79 Mon Sep 17 00:00:00 2001 From: MH4GF Date: Mon, 30 Oct 2023 09:11:25 +0900 Subject: [PATCH 2/3] refactor(lib): add NewListFromCLI() for CLI usage --- cmd/root.go | 7 +++++-- lib/list.go | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b443c0d..784288a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,9 +18,12 @@ 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) { + list, err := lib.NewListFromCLI(sinceDate, untilDate) + if err != nil { + fmt.Println(err) + os.Exit(1) + } - // TODO - list := lib.NewList(sinceDate, untilDate, "user", "token", "gistID") lines, err := list.Collect(debug) if err != nil { fmt.Println(err) diff --git a/lib/list.go b/lib/list.go index 2833409..36aa6b3 100644 --- a/lib/list.go +++ b/lib/list.go @@ -28,6 +28,27 @@ func NewList(sinceDate, untilDate, user, accessToken, settingsGistID string) *Li } } +// NewListFromCLI returns a new List from environment variables or git config. +func NewListFromCLI(sinceDate, untilDate string) (*List, error) { + user, err := getUser() + if err != nil { + return nil, err + } + accessToken, err := getAccessToken() + if err != nil { + return nil, err + } + settingsGistID := getGistID() + + return &List{ + sinceDate: sinceDate, + untilDate: untilDate, + user: user, + accessToken: accessToken, + settingsGistID: settingsGistID, + }, nil +} + // Collect collects GitHub activities. func (l *List) Collect(debug bool) (string, error) { sinceTime, err := getSinceTime(l.sinceDate) From 914b967a6d0790cfc6b2dbb17aad0bfd750ef04c Mon Sep 17 00:00:00 2001 From: MH4GF Date: Thu, 2 Nov 2023 22:39:50 +0900 Subject: [PATCH 3/3] refactor: Refactor code to include debug flag in List struct --- cmd/root.go | 4 ++-- lib/list.go | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 784288a..244ba1f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,13 +18,13 @@ 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) { - list, err := lib.NewListFromCLI(sinceDate, untilDate) + list, err := lib.NewListFromCLI(sinceDate, untilDate, debug) if err != nil { fmt.Println(err) os.Exit(1) } - lines, err := list.Collect(debug) + lines, err := list.Collect() if err != nil { fmt.Println(err) os.Exit(1) diff --git a/lib/list.go b/lib/list.go index 36aa6b3..87e38a4 100644 --- a/lib/list.go +++ b/lib/list.go @@ -15,21 +15,23 @@ type List struct { user string accessToken string settingsGistID string + debug bool } // NewList returns a new List. -func NewList(sinceDate, untilDate, user, accessToken, settingsGistID string) *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) (*List, error) { +func NewListFromCLI(sinceDate, untilDate string, debug bool) (*List, error) { user, err := getUser() if err != nil { return nil, err @@ -46,11 +48,12 @@ func NewListFromCLI(sinceDate, untilDate string) (*List, error) { user: user, accessToken: accessToken, settingsGistID: settingsGistID, + debug: debug, }, nil } // Collect collects GitHub activities. -func (l *List) Collect(debug bool) (string, error) { +func (l *List) Collect() (string, error) { sinceTime, err := getSinceTime(l.sinceDate) if err != nil { return "", err @@ -64,7 +67,7 @@ func (l *List) Collect(debug bool) (string, error) { ctx := context.Background() client := getClient(ctx, l.accessToken) - events, err := NewEvents(ctx, client, l.user, sinceTime, untilTime, debug).Collect() + events, err := NewEvents(ctx, client, l.user, sinceTime, untilTime, l.debug).Collect() if err != nil { return "", err } @@ -72,7 +75,7 @@ func (l *List) Collect(debug bool) (string, error) { 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 {