forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_configure.go
164 lines (124 loc) Β· 3.93 KB
/
cmd_configure.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cli
import (
"context"
"fmt"
"github.com/buildkite/cli/v2/config"
"github.com/buildkite/cli/v2/github"
"github.com/buildkite/cli/v2/graphql"
"github.com/fatih/color"
)
type ConfigureCommandContext struct {
TerminalContext
ConfigContext
Debug bool
DebugGraphQL bool
}
func ConfigureDefaultCommand(ctx ConfigureCommandContext) error {
ctx.Header("Ok! Let's get started with configuring bk π\n")
if err := ConfigureBuildkiteGraphQLCommand(ctx); err != nil {
return err
}
ctx.Println()
ctx.Printf("For now, we will assume you are using GitHub. " +
"Support for others is coming soon! π\n\n")
if err := ConfigureGithubCommand(ctx); err != nil {
return err
}
ctx.Printf("\nOk, you are good to go!\n")
return nil
}
func ConfigureGithubCommand(ctx ConfigureCommandContext) error {
cfg, err := config.Open()
if err != nil {
return NewExitError(fmt.Errorf("Failed to open config file: %v", err), 1)
}
ctx.Header("Let's configure your github.com credentials π»")
ctx.Printf(`We need to authorize this app to access your repositories.
This authorization is stored locally in ` + cfg.Path + `, buildkite.com never gets access to it.
`)
ctx.Printf(color.WhiteString(`In a moment, we'll print a unique code and open a github.com URL in your default browser.
To authenticate bk, enter the unique code into the browser.
`))
ctx.WaitForKeyPress(color.WhiteString("Press enter to continue...\n\n"))
token, err := github.Authenticate()
if err != nil {
ctx.Printf("β\n\n")
return NewExitError(fmt.Errorf("GitHub OAuth error: %v", err), 1)
}
client := github.NewClientFromToken(token)
user, _, err := client.Users.Get(context.Background(), "")
if err != nil {
return NewExitError(fmt.Errorf("GitHub Users.Get() failed: %v", err), 1)
}
ctx.Println()
ctx.Printf(color.GreenString("Authenticated as %s β
\n"), *user.Login)
cfg.GitHubOAuthToken = token
if err := cfg.Write(); err != nil {
return NewExitError(err, 1)
}
ctx.Printf(color.GreenString("Stored GitHub token! πͺ\n"))
return nil
}
func ConfigureBuildkiteGraphQLCommand(ctx ConfigureCommandContext) error {
ctx.Header("Configuring Buildkite GraphQL credentials")
cfg, err := config.Open()
if err != nil {
return NewExitError(fmt.Errorf("Failed to open config file: %v", err), 1)
}
ctx.Printf(`Create a GraphQL token at https://buildkite.com/user/api-access-tokens/new
Make sure to tick the GraphQL scope at the bottom.
This will be validated and stored in ` + cfg.Path + "\n\n")
token, err := ctx.ReadPassword(color.WhiteString("GraphQL Token"))
if err != nil {
return NewExitError(fmt.Errorf("Failed to read token from terminal: %v", err), 1)
}
s := ctx.Spinner()
s.Start()
client, err := graphql.NewClient(token)
if err != nil {
return NewExitError(fmt.Errorf("Failed to create a client: %v", err), 1)
}
resp, err := client.Do(`
query {
viewer {
user {
email
uuid
}
}
}
`, nil)
s.Stop()
if err != nil {
fmt.Printf("β\n\n")
return NewExitError(err, 1)
}
var userQueryResponse struct {
Data struct {
Viewer struct {
User struct {
Email string `json:"email"`
UUID string `json:"uuid"`
} `json:"user"`
} `json:"viewer"`
} `json:"data"`
}
if err = resp.DecodeInto(&userQueryResponse); err != nil {
return NewExitError(fmt.Errorf("Failed to parse GraphQL response: %v", err), 1)
}
ctx.Printf(color.GreenString("%s β
\n\n"),
userQueryResponse.Data.Viewer.User.Email)
cfg.GraphQLToken = token
cfg.BuildkiteEmail = userQueryResponse.Data.Viewer.User.Email
cfg.BuildkiteUUID = userQueryResponse.Data.Viewer.User.UUID
ctx.Printf(color.GreenString("Stored Buildkite configuration! πͺ\n"))
if err = cfg.Write(); err != nil {
return NewExitError(fmt.Errorf("Failed to write config: %v", err), 1)
}
configPath, err := config.Path()
if err != nil {
return NewExitError(err, 1)
}
ctx.Printf(color.GreenString("Wrote configuration to %s π\n"), configPath)
return nil
}