-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
63 lines (54 loc) · 1.54 KB
/
client.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
package github
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"syscall"
"github.com/google/go-github/v21/github"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/oauth2"
)
// NewBasicAuthClient 產生一個 BasicAuth 的 GitHub Client, 並且會檢查完 OTP
func NewBasicAuthClient(ctx context.Context, log *logrus.Logger, username, password string) (*github.Client, error) {
r := bufio.NewReader(os.Stdin)
if username == "" {
fmt.Fprint(log.Out, "GitHub username: ")
username, _ = r.ReadString('\n')
}
if password == "" {
fmt.Fprint(log.Out, "GitHub password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
password = string(bytePassword)
fmt.Fprintln(log.Out, "")
}
tp := github.BasicAuthTransport{
Username: strings.TrimSpace(username),
Password: strings.TrimSpace(password),
}
client := github.NewClient(tp.Client())
// call api for checking OTP
_, _, err := client.APIMeta(ctx)
if err == nil {
return client, nil
}
if _, ok := err.(*github.TwoFactorAuthError); ok {
fmt.Fprint(log.Out, "GitHub two-factor authentication code: ")
otp, _ := r.ReadString('\n')
tp.OTP = strings.TrimSpace(otp)
if _, _, err = client.APIMeta(ctx); err == nil {
return client, nil
}
}
return nil, err
}
// NewTokenClient 產生一個 Token 的 GitHub Client
func NewTokenClient(ctx context.Context, token string) (*github.Client, error) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc), nil
}