|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "go-github-token-limit/internal/utils" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + apiUrl = "https://api.github.com/rate_limit" |
| 14 | + authHeader = "Authorization" |
| 15 | + tokenEnvName = "GITHUB_TOKEN" |
| 16 | +) |
| 17 | + |
| 18 | +type Rate struct { |
| 19 | + Limit int `json:"limit"` |
| 20 | + Remaining int `json:"remaining"` |
| 21 | + Reset Timestamp `json:"reset"` |
| 22 | +} |
| 23 | + |
| 24 | +type RateLimit struct { |
| 25 | + Core Rate `json:"core"` |
| 26 | +} |
| 27 | + |
| 28 | +type RateLimitResponse struct { |
| 29 | + Resources RateLimit `json:"resources"` |
| 30 | +} |
| 31 | + |
| 32 | +type Timestamp time.Time |
| 33 | + |
| 34 | +func (t *Timestamp) UnmarshalJSON(data []byte) error { |
| 35 | + // The data comes as a JSON number (UNIX timestamp) |
| 36 | + var unixTime int64 |
| 37 | + if err := json.Unmarshal(data, &unixTime); err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + *t = Timestamp(time.Unix(unixTime, 0)) |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +func main() { |
| 45 | + utils.InfoNotice(` GitHub Token Limit Checker `) |
| 46 | + utils.InfoNotice(` v1.0.0 `) |
| 47 | + println("") // This is a placeholder for the main function |
| 48 | + |
| 49 | + client := &http.Client{} |
| 50 | + req, err := http.NewRequest("GET", apiUrl, nil) |
| 51 | + if err != nil { |
| 52 | + utils.ErrorNotice(` ERROR `) |
| 53 | + utils.ErrorNotice(err.Error()) |
| 54 | + os.Exit(3) |
| 55 | + } |
| 56 | + |
| 57 | + token := os.Getenv(tokenEnvName) |
| 58 | + if token != "" { |
| 59 | + req.Header.Set(authHeader, fmt.Sprintf("token %s", token)) |
| 60 | + } |
| 61 | + |
| 62 | + resp, err := client.Do(req) |
| 63 | + if err != nil { |
| 64 | + utils.ErrorNotice(` ERROR `) |
| 65 | + utils.ErrorNotice(err.Error()) |
| 66 | + os.Exit(3) |
| 67 | + } |
| 68 | + defer resp.Body.Close() |
| 69 | + |
| 70 | + if resp.StatusCode != http.StatusOK { |
| 71 | + utils.ErrorNotice(` ERROR `) |
| 72 | + utils.ErrorNotice(`Failed to get rate limit`) |
| 73 | + os.Exit(3) |
| 74 | + } |
| 75 | + |
| 76 | + var rateLimit RateLimitResponse |
| 77 | + if err := json.NewDecoder(resp.Body).Decode(&rateLimit); err != nil { |
| 78 | + utils.ErrorNotice(` ERROR `) |
| 79 | + utils.ErrorNotice(err.Error()) |
| 80 | + os.Exit(3) |
| 81 | + } |
| 82 | + |
| 83 | + core := rateLimit.Resources.Core |
| 84 | + resetTime := time.Time(core.Reset) |
| 85 | + //fmt.Printf("%+v\n", core) |
| 86 | + |
| 87 | + if core.Remaining > 0 { |
| 88 | + utils.InfoNotice(fmt.Sprintf(" You have %d/%d requests left this hour", core.Remaining, core.Limit)) |
| 89 | + } else { |
| 90 | + now := time.Now() |
| 91 | + durationUntilReset := resetTime.Sub(now).Minutes() |
| 92 | + utils.InfoNotice(fmt.Sprintf(" You have no requests left. The limit will reset in %.0f at %s", durationUntilReset, resetTime)) |
| 93 | + } |
| 94 | + println("") |
| 95 | +} |
0 commit comments