forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
141 lines (119 loc) · 2.69 KB
/
context.go
File metadata and controls
141 lines (119 loc) · 2.69 KB
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
package context
import (
"path"
"strings"
"github.com/github/gh-cli/git"
"github.com/mitchellh/go-homedir"
)
// Context represents the interface for querying information about the current environment
type Context interface {
AuthToken() (string, error)
SetAuthToken(string)
AuthLogin() (string, error)
Branch() (string, error)
SetBranch(string)
Remotes() (Remotes, error)
BaseRepo() (GitHubRepository, error)
SetBaseRepo(string)
}
// GitHubRepository is anything that can be mapped to an OWNER/REPO pair
type GitHubRepository interface {
RepoOwner() string
RepoName() string
}
// New initializes a Context that reads from the filesystem
func New() Context {
return &fsContext{}
}
// A Context implementation that queries the filesystem
type fsContext struct {
config *configEntry
remotes Remotes
branch string
baseRepo GitHubRepository
authToken string
}
func ConfigDir() string {
dir, _ := homedir.Expand("~/.config/gh")
return dir
}
func configFile() string {
return path.Join(ConfigDir(), "config.yml")
}
func (c *fsContext) getConfig() (*configEntry, error) {
if c.config == nil {
entry, err := parseOrSetupConfigFile(configFile())
if err != nil {
return nil, err
}
c.config = entry
c.authToken = ""
}
return c.config, nil
}
func (c *fsContext) AuthToken() (string, error) {
if c.authToken != "" {
return c.authToken, nil
}
config, err := c.getConfig()
if err != nil {
return "", err
}
return config.Token, nil
}
func (c *fsContext) SetAuthToken(t string) {
c.authToken = t
}
func (c *fsContext) AuthLogin() (string, error) {
config, err := c.getConfig()
if err != nil {
return "", err
}
return config.User, nil
}
func (c *fsContext) Branch() (string, error) {
if c.branch != "" {
return c.branch, nil
}
currentBranch, err := git.CurrentBranch()
if err != nil {
return "", err
}
c.branch = currentBranch
return c.branch, nil
}
func (c *fsContext) SetBranch(b string) {
c.branch = b
}
func (c *fsContext) Remotes() (Remotes, error) {
if c.remotes == nil {
gitRemotes, err := git.Remotes()
if err != nil {
return nil, err
}
sshTranslate := git.ParseSSHConfig().Translator()
c.remotes = translateRemotes(gitRemotes, sshTranslate)
}
return c.remotes, nil
}
func (c *fsContext) BaseRepo() (GitHubRepository, error) {
if c.baseRepo != nil {
return c.baseRepo, nil
}
remotes, err := c.Remotes()
if err != nil {
return nil, err
}
rem, err := remotes.FindByName("upstream", "github", "origin", "*")
if err != nil {
return nil, err
}
c.baseRepo = rem
return c.baseRepo, nil
}
func (c *fsContext) SetBaseRepo(nwo string) {
parts := strings.SplitN(nwo, "/", 2)
if len(parts) == 2 {
c.baseRepo = &ghRepo{parts[0], parts[1]}
}
}