-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
50 lines (39 loc) · 1.11 KB
/
auth.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
package main
import (
"context"
"fmt"
"os"
"github.com/google/go-github/v40/github"
"github.com/mitchellh/go-homedir"
"golang.org/x/oauth2"
)
var GithubClient *github.Client
func init() {
home, err := homedir.Dir()
check(err)
_, err = os.Stat(home + "/.rtauth")
runAsGuest := false
for _, arg := range os.Args {
if arg == "--login" || arg == "--help" || arg == "--self-update" || arg == "--init" || arg == "--version" {
runAsGuest = true
}
}
if !runAsGuest && os.IsNotExist(err) {
fmt.Printf("%sYou are not connected to GitHub.%s\n", Yellow.Fg(), Stop)
fmt.Printf("%sPlease create a token at https://github.com/settings/tokens/new?scopes=repo.%s\n", Yellow.Fg(), Stop)
fmt.Printf("%sThen run `rt --login` and paste your token when asked.%s\n", Yellow.Fg(), Stop)
os.Exit(1)
}
if !runAsGuest {
bytes, err := os.ReadFile(home + "/.rtauth")
check(err)
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: string(bytes),
})
tc := oauth2.NewClient(ctx, ts)
GithubClient = github.NewClient(tc)
} else {
GithubClient = github.NewClient(nil)
}
}