...
1
2 package ghx
3
4 import (
5 "context"
6 "fmt"
7 "net/http"
8
9 "github.com/google/go-github/v47/github"
10 "github.com/shurcooL/githubv4"
11 "golang.org/x/oauth2"
12
13 "edge-infra.dev/pkg/lib/cli/rags"
14 "edge-infra.dev/pkg/lib/cli/sink"
15 )
16
17
18
19
20 type GHX struct {
21 V4 *githubv4.Client
22 V3 *github.Client
23 httpClient *http.Client
24
25
26
27 Optional bool
28
29 token string
30 }
31
32 func (g *GHX) RegisterFlags(rs *rags.RagSet) {
33 rs.Add(&rags.Rag{
34 Name: "github-token",
35 Value: &rags.String{Var: &g.token},
36 Usage: "A GitHub personal access token.",
37 Category: "remote file",
38 Required: !g.Optional,
39 })
40 }
41
42 func (g *GHX) BeforeRun(ctx context.Context, r sink.Run) (context.Context, sink.Run, error) {
43 switch {
44 case g.token == "" && g.Optional:
45 return ctx, r, nil
46 case g.token == "":
47
48 return ctx, r, fmt.Errorf("--github-token is required")
49 }
50
51 g.httpClient = oauth2.NewClient(ctx, oauth2.StaticTokenSource(
52 &oauth2.Token{AccessToken: g.token},
53 ))
54 g.V4 = githubv4.NewClient(g.httpClient)
55 g.V3 = github.NewClient(g.httpClient)
56
57 return ctx, r, nil
58 }
59
View as plain text