Skip to content

remove global variables #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ import (
"github.com/gookit/color"
)

var email string
var since, until time.Time

func main() {
var email string
var sinceflag, untilflag string
flag.StringVar(&sinceflag, "since", "", "start date")
flag.StringVar(&untilflag, "until", "", "end date")
flag.StringVar(&email, "email", strings.TrimSpace(getGlobalEmailFromGit()), "you Git email")
flag.Parse()

err := setTimeFlags(sinceflag, untilflag)
b, err := setTimeFlags(sinceflag, untilflag)
if err != nil {
fmt.Fprint(os.Stderr, color.Red.Sprintf("gitcs: %s\n", err.Error()))
os.Exit(1)
Expand Down Expand Up @@ -53,8 +51,13 @@ func main() {
os.Exit(1)
}

commits := processRepos(repos, email)
commits := processRepos(repos, email, b.Since, b.Until)
fmt.Print("\n\n")
printTable(commits)
printTable(commits, b.Since, b.Until)
fmt.Print("\n\n")
}

type Boundary struct {
Since time.Time
Until time.Time
}
2 changes: 1 addition & 1 deletion print.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func getDay(i int) string {
return strings.Repeat(" ", 3)
}

func printTable(commits map[int]int) {
func printTable(commits map[int]int, since, until time.Time) {
for since.Weekday() != time.Sunday {
since = since.AddDate(0, 0, -1)
}
Expand Down
13 changes: 10 additions & 3 deletions print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ func TestPrintTable(t *testing.T) {
13: 0,
}

since = time.Date(2024, 2, 7, 0, 0, 0, 0, time.UTC)
until = time.Date(2024, 2, 19, 0, 0, 0, 0, time.UTC)
since := time.Date(2024, 2, 7, 0, 0, 0, 0, time.UTC)
until := time.Date(2024, 2, 19, 0, 0, 0, 0, time.UTC)

oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

printTable(commits)
printTable(commits, since, until)
w.Close()

dat, err := io.ReadAll(r)
Expand All @@ -173,6 +173,13 @@ func TestPrintTable(t *testing.T) {
var buf strings.Builder
_, _ = fmt.Fprint(&buf, string(dat))

for since.Weekday() != time.Sunday {
since = since.AddDate(0, 0, -1)
}
for until.Weekday() != time.Saturday {
until = until.AddDate(0, 0, 1)
}

s := strings.Builder{}
s1 := since

Expand Down
6 changes: 3 additions & 3 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const sixMonthsInDays int = 182

var now = time.Now()

func fillCommits(path, email string, commits map[int]int) error {
func fillCommits(path, email string, commits map[int]int, since, until time.Time) error {
repo, err := git.PlainOpen(path)
if err != nil {
return err
Expand All @@ -38,11 +38,11 @@ func fillCommits(path, email string, commits map[int]int) error {
return err
}

func processRepos(repos []string, email string) map[int]int {
func processRepos(repos []string, email string, since, until time.Time) map[int]int {
m := map[int]int{}
var err error
for _, repo := range repos {
err = fillCommits(repo, email, m)
err = fillCommits(repo, email, m, since, until)
if err != nil {
fmt.Printf("failed to fill commits in %q: %v", repo, err)
}
Expand Down
10 changes: 8 additions & 2 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ func TestFillCommits(t *testing.T) {
},
}

since := time.Now().AddDate(0, 0, -1)
until := time.Now().AddDate(0, 0, 1)

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
commits := map[int]int{}
err = fillCommits(tt.Path, tt.Email, commits)
err = fillCommits(tt.Path, tt.Email, commits, since, until)
if err != nil {
t.Fatalf("failed to fill commits in %q: %v", tt.Path, err)
}
Expand Down Expand Up @@ -94,9 +97,12 @@ func TestProcessRepos(t *testing.T) {
},
}

since := time.Now().AddDate(0, 0, -1)
until := time.Now().AddDate(0, 0, 1)

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
commits := processRepos(tt.Repos, tt.Email)
commits := processRepos(tt.Repos, tt.Email, since, until)
if len(commits) != len(tt.Expected) {
t.Errorf("processRepos11() = %v, want %v", commits, tt.Expected)
}
Expand Down
22 changes: 12 additions & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,28 @@ func getGlobalEmailFromGit() string {
return string(localEmail)
}

func setTimeFlags(sinceflag, untilflag string) error {
func setTimeFlags(sinceflag, untilflag string) (*Boundary, error) {
var err error
var boundary Boundary
if untilflag != "" {
until, err = time.Parse("2006-01-02", untilflag)
boundary.Until, err = time.Parse("2006-01-02", untilflag)
if err != nil {
return fmt.Errorf("invalid 'until' date format. please use the format: 2006-01-02")
return nil, fmt.Errorf("invalid 'until' date format. please use the format: 2006-01-02")
}
if until.After(now) {
until = now
if boundary.Until.After(now) {
boundary.Until = now
}
} else {
until = now
boundary.Until = now
}
if sinceflag != "" {
since, err = time.Parse("2006-01-02", sinceflag)
boundary.Since, err = time.Parse("2006-01-02", sinceflag)
if err != nil {
return fmt.Errorf("invalid 'since' date format. please use the format: 2006-01-02")
return nil, fmt.Errorf("invalid 'since' date format. please use the format: 2006-01-02")
}
} else {
since = time.Date(until.Year(), until.Month(), until.Day(), 0, 0, 0, 0, until.Location()).AddDate(0, 0, -sixMonthsInDays)
boundary.Since = time.Date(boundary.Until.Year(), boundary.Until.Month(), boundary.Until.Day(), 0, 0, 0, 0, boundary.Until.Location()).AddDate(0, 0, -sixMonthsInDays)
}
return nil

return &boundary, nil
}
10 changes: 5 additions & 5 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestSetTimeFlags(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := setTimeFlags(tt.sinceflag, tt.untilflag)
b, err := setTimeFlags(tt.sinceflag, tt.untilflag)

if err != nil {
if err.Error() != tt.expectedError {
Expand All @@ -261,12 +261,12 @@ func TestSetTimeFlags(t *testing.T) {
return
}

if since != tt.expectedSince {
t.Errorf("Unexpected value of 'since'. Expected: %v, Got: %v", tt.expectedSince, since)
if b.Since != tt.expectedSince {
t.Errorf("Unexpected value of 'since'. Expected: %v, Got: %v", tt.expectedSince, b.Since)
}

if until != tt.expectedUntil {
t.Errorf("Unexpected value of 'until'. Expected: %v, Got: %v", tt.expectedUntil, until)
if b.Until != tt.expectedUntil {
t.Errorf("Unexpected value of 'until'. Expected: %v, Got: %v", tt.expectedUntil, b.Until)
}
})
}
Expand Down