-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (71 loc) · 1.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/efreitasn/contribs/internal/config"
"github.com/efreitasn/contribs/internal/github"
"github.com/efreitasn/contribs/internal/logs"
)
func main() {
// Set mode
setMode := false
for _, arg := range os.Args {
if arg == "set" {
setMode = true
}
}
if setMode {
flags := flag.NewFlagSet("set", flag.ExitOnError)
apiKey := flags.String("key", "", "Set the GitHub API key.")
flags.Parse(os.Args[2:])
if *apiKey == "" {
logs.Error.Fatalln("while parsing arguments")
}
c := config.Config{
GitHubAPIKey: *apiKey,
}
err := config.Write(&c)
if err != nil {
logs.Error.Fatalln(fmt.Errorf("writing config to file: %w", err))
}
return
}
// Default mode
lastYearFlag := flag.Bool("last-year", false, "Whether to show contributions made in the past year instead of the current day.")
flag.Parse()
config, err := config.Get()
if err != nil {
logs.Error.Fatalln(fmt.Errorf("reading config file: %w", err))
}
if config == nil {
logs.Error.Fatalln("config file doesn't exist")
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client := github.NewClient(config.GitHubAPIKey)
var numContribs int
if *lastYearFlag {
numContribs, err = github.GetNumContribsLastYear(ctx, client)
} else {
now := time.Now()
from := time.Date(
now.Year(),
now.Month(),
now.Day(),
0,
0,
0,
0,
time.Local,
)
to := from.Add((time.Hour * 24) - time.Second)
numContribs, err = github.GetNumContribsByTime(ctx, client, from, to)
}
if err != nil {
logs.Error.Fatalln(fmt.Errorf("fetching contribs: %w", err))
}
fmt.Println(numContribs)
}