This repository has been archived by the owner on Apr 19, 2021. It is now read-only.
forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
69 lines (54 loc) · 1.67 KB
/
context.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
package cli
import (
"fmt"
"os"
"github.com/99designs/keyring"
"github.com/buildkite/cli/config"
"github.com/buildkite/cli/github"
"github.com/buildkite/cli/graphql"
githubclient "github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type TerminalContext interface {
Header(h string)
Println(s ...interface{})
Printf(s string, v ...interface{})
Failure(s string)
WaitForKeyPress(prompt string)
Spinner() Spinner
Try() Tryer
ReadPassword(prompt string) (string, error)
}
type KeyringContext struct {
Keyring keyring.Keyring
}
func (kc KeyringContext) GithubClient() (*githubclient.Client, error) {
var token oauth2.Token
// Try and load from env first
if envToken := os.Getenv(`GITHUB_OAUTH_TOKEN`); envToken != "" {
return github.NewClientFromToken(&oauth2.Token{AccessToken: envToken}), nil
}
// Otherwise load from keyring
err := config.RetrieveCredential(kc.Keyring, config.GithubOAuthToken, &token)
if err != nil {
return nil, fmt.Errorf("Error retrieving github oauth credentials: %v", err)
}
return github.NewClientFromToken(&token), nil
}
func (kc KeyringContext) BuildkiteGraphQLClient() (*graphql.Client, error) {
var token string
// Try and load from env first
if envToken := os.Getenv(`BUILDKITE_TOKEN`); envToken != "" {
return graphql.NewClient(envToken)
}
// Otherwise load from keyring
err := config.RetrieveCredential(kc.Keyring, config.BuildkiteGraphQLToken, &token)
if err != nil {
return nil, NewExitError(fmt.Errorf("Error retrieving buildkite graphql credentials: %v", err), 1)
}
client, err := graphql.NewClient(token)
if err != nil {
return nil, fmt.Errorf("Failed to create a client: %v", err)
}
return client, nil
}