Skip to content

Commit 5e470a4

Browse files
committed
feat: Initial commit
0 parents  commit 5e470a4

11 files changed

Lines changed: 373 additions & 0 deletions

File tree

.github/dependabot.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "gomod" # See documentation for possible values
4+
directory: "/" # Location of package manifests
5+
schedule:
6+
interval: "weekly"
7+
target-branch: "development"
8+
labels:
9+
- "dependabot"

.github/workflows/ci.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- '**'
6+
pull_request:
7+
branches:
8+
- '**'
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-go@v5
15+
with:
16+
go-version: 1.22
17+
- uses: golangci/golangci-lint-action@v6
18+
test:
19+
runs-on: ubuntu-latest
20+
needs: lint
21+
steps:
22+
- uses: actions/checkout@v4
23+
- uses: actions/setup-go@v5
24+
with:
25+
go-version: 1.22
26+
- run: go test -v ./...
27+
release:
28+
runs-on: ubuntu-latest
29+
needs: test
30+
permissions:
31+
contents: write
32+
steps:
33+
- uses: actions/checkout@v4
34+
- uses: actions/setup-go@v5
35+
with:
36+
go-version: 1.22
37+
- uses: go-semantic-release/action@v1
38+
with:
39+
hooks: goreleaser
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
/bin/
11+
/files/*
12+
!/files/.gitkeep
13+
14+
.idea/
15+
16+
# Test binary, built with `go test -c`
17+
*.test
18+
19+
# Output of the go coverage tool, specifically when used with LiteIDE
20+
*.out
21+
22+
# Dependency directories (remove the comment below to include it)
23+
# vendor/
24+
25+
# Go workspace file
26+
go.work
27+
dist/

.goreleaser.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
4+
# The lines below are called `modelines`. See `:help modeline`
5+
# Feel free to remove those if you don't want/need to use them.
6+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
7+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
8+
9+
version: 1
10+
11+
before:
12+
hooks:
13+
# You may remove this if you don't use go modules.
14+
- go mod tidy
15+
16+
builds:
17+
- main: ./cmd/main.go
18+
env:
19+
- CGO_ENABLED=0
20+
goos:
21+
- linux
22+
- windows
23+
- darwin
24+
25+
archives:
26+
- format: tar.gz
27+
# this name template makes the OS and Arch compatible with the results of `uname`.
28+
name_template: >-
29+
{{ .ProjectName }}_
30+
{{- title .Os }}_
31+
{{- if eq .Arch "amd64" }}x86_64
32+
{{- else if eq .Arch "386" }}i386
33+
{{- else }}{{ .Arch }}{{ end }}
34+
{{- if .Arm }}v{{ .Arm }}{{ end }}
35+
# use zip for windows archives
36+
format_overrides:
37+
- goos: windows
38+
format: zip
39+
40+
changelog:
41+
sort: asc
42+
use: github
43+
filters:
44+
exclude:
45+
- '^test:'
46+
- '^chore'
47+
- 'merge conflict'
48+
- Merge pull request
49+
- Merge remote-tracking branch
50+
- Merge branch
51+
- go mod tidy
52+
groups:
53+
- title: Dependency updates
54+
regexp: "^.*feat\\(deps\\)*:+.*$"
55+
order: 300
56+
- title: 'New Features'
57+
regexp: "^.*feat[(\\w)]*:+.*$"
58+
order: 100
59+
- title: 'Bug fixes'
60+
regexp: "^.*fix[(\\w)]*:+.*$"
61+
order: 200
62+
- title: 'Documentation updates'
63+
regexp: "^.*docs[(\\w)]*:+.*$"
64+
order: 400
65+
- title: Other work
66+
order: 9999

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright © 2023 Clive Walkden <clivewalkden@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Makefile

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.DEFAULT_GOAL := build
2+
3+
GO_BIN=${GOROOT}/bin/go
4+
EXECUTABLE=github-token-limit
5+
VERSION=1.0.0
6+
7+
GO_MAJOR_VERSION = $(shell $(GO_BIN) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1)
8+
GO_MINOR_VERSION = $(shell $(GO_BIN) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
9+
MINIMUM_SUPPORTED_GO_MAJOR_VERSION = 1
10+
MINIMUM_SUPPORTED_GO_MINOR_VERSION = 22
11+
GO_VERSION_VALIDATION_ERR_MSG = Golang version is not supported, please update to at least $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION).$(MINIMUM_SUPPORTED_GO_MINOR_VERSION)
12+
13+
validate-go-version: ## Validates the installed version of go against Mattermost's minimum requirement.
14+
@if [ $(GO_MAJOR_VERSION) -gt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \
15+
exit 0 ;\
16+
elif [ $(GO_MAJOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \
17+
echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\
18+
exit 1; \
19+
elif [ $(GO_MINOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MINOR_VERSION) ] ; then \
20+
echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\
21+
exit 1; \
22+
fi
23+
24+
$(info Compiling with $(GOROOT))
25+
26+
fmt: validate-go-version
27+
${GO_BIN} fmt ./...
28+
.PHONY:fmt
29+
30+
lint: fmt
31+
golangci-lint run ./...
32+
.PHONY:lint
33+
34+
vet: fmt
35+
${GO_BIN} vet ./...
36+
.PHONY:vet
37+
38+
build: vet
39+
echo "Compiling for every OS and Platform"
40+
GOOS=freebsd GOARCH=amd64 ${GO_BIN} build -o bin/${VERSION}/${EXECUTABLE}-freebsd-amd64 ./cmd/main.go
41+
GOOS=darwin GOARCH=amd64 ${GO_BIN} build -o bin/${VERSION}/${EXECUTABLE}-macos-amd64 ./cmd/main.go
42+
GOOS=darwin GOARCH=arm64 ${GO_BIN} build -o bin/${VERSION}/${EXECUTABLE}-macos-arm64 ./cmd/main.go
43+
GOOS=linux GOARCH=amd64 ${GO_BIN} build -o bin/${VERSION}/${EXECUTABLE}-linux-amd64 ./cmd/main.go
44+
GOOS=windows GOARCH=amd64 ${GO_BIN} build -o bin/${VERSION}/${EXECUTABLE}-windows-amd64.exe ./cmd/main.go
45+
.PHONY:build

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Check GitHub Token Limit
2+
3+
This executable returns the number of requests remaining for the GitHub API. If there are no tokens left it gives the reset time.
4+
5+
## Run
6+
7+
`go run cmd/main.go`
8+
9+
## Versioning
10+
11+
We use [SemVer](http://semver.org/) for versioning. For the versions available, see
12+
the [tags on this repository](https://github.com/clivewalkden/go-wasabi-cleanup/tags) or [CHANGELOG.md](./CHANGELOG.md).
13+
14+
## Authors
15+
16+
* **Clive Walkden** - *Initial work* - [SOZO Design Ltd](https://github.com/sozo-design)
17+
18+
See also the list of [contributors](https://github.com/clivewalkden/go-wasabi-cleanup/contributors) who participated in
19+
this project.
20+
21+
## License
22+
23+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details

cmd/main.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module go-github-token-limit
2+
3+
go 1.22
4+
5+
require (
6+
github.com/fatih/color v1.17.0 // indirect
7+
github.com/mattn/go-colorable v0.1.13 // indirect
8+
github.com/mattn/go-isatty v0.0.20 // indirect
9+
golang.org/x/sys v0.18.0 // indirect
10+
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
2+
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
3+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
5+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
6+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
7+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
8+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
11+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

0 commit comments

Comments
 (0)