main.main   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 28
nop 0
dl 0
loc 39
rs 9.208
c 0
b 0
f 0
1
package main
2
3
import (
4
	"fmt"
5
	"go-github-token-limit/internal/utils"
6
	"net/http"
7
	"os"
8
	"time"
9
10
	"go-github-token-limit/internal/githubapi"
11
)
12
13
var version = "none provided"
14
15
func main() {
16
	fmt.Print("\033[H\033[2J") //clear screen
17
	println("")
18
	utils.InfoNotice(`GitHub Token Limit Checker`)
19
	utils.InfoNotice(fmt.Sprintf(`v%s`, version))
20
	println("")
21
22
	client := &http.Client{}
23
	token := githubapi.GetGithubTokenFromEnv()
24
25
	rateLimitResponse, err := githubapi.FetchRateLimit(client, token)
26
	if err != nil {
27
		utils.ErrorNotice(fmt.Sprintf("Error fetching rate limit: %v\n", err))
28
		println("")
29
		os.Exit(3)
30
	}
31
32
	// Default limit is 60 if no token is provided
33
	if rateLimitResponse.Resources.Core.Limit == 60 {
34
		utils.CautionNotice("Please provide a GitHub token to in the environment variable " + githubapi.DefaultTokenEnv + ".")
35
		println("")
36
		os.Exit(3)
37
	}
38
39
	core := rateLimitResponse.Resources.Core
40
	resetTime := core.Reset.Time
41
	//fmt.Printf("%+v\n", core)
42
43
	utils.SuccessNotice(fmt.Sprintf("Using token: %s", utils.ObscureToken(token)))
44
45
	if core.Remaining > 0 {
46
		utils.SuccessNotice(fmt.Sprintf("You have %d/%d requests left this hour", core.Remaining, core.Limit))
47
	} else {
48
		now := time.Now()
49
		durationUntilReset := resetTime.Sub(now).Minutes()
50
		utils.CautionNotice("You have no requests left.")
51
		utils.CautionNotice(fmt.Sprintf("The limit will reset in %.0f minutes at %s", durationUntilReset, resetTime))
52
	}
53
	println("")
54
}
55