-
Notifications
You must be signed in to change notification settings - Fork 6
/
config_type.go
54 lines (45 loc) · 1.06 KB
/
config_type.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
package auth
import (
"github.com/jenkins-x/jx-logging/pkg/log"
)
const (
APIGithubCom = "api.github.com"
)
// Config interface describes interacting with some persistent configuration for gh.
type Config interface {
GetToken(hostname string) string
GetUser(hostname string) string
HasHosts() bool
GetHosts() []string
}
type fileConfig struct {
Hosts map[string]*HostConfig `json:"hosts"`
}
// HostConfig a struct containing the host level information.
type HostConfig struct {
User string `yaml:"user"`
Token string `yaml:"oauth_token"`
}
func (c *fileConfig) GetToken(hostname string) string {
log.Logger().Debugf("Getting token for host %s", hostname)
if c.Hosts[hostname] != nil {
return c.Hosts[hostname].Token
}
return ""
}
func (c *fileConfig) GetUser(hostname string) string {
if c.Hosts[hostname] != nil {
return c.Hosts[hostname].User
}
return ""
}
func (c *fileConfig) HasHosts() bool {
return len(c.Hosts) > 0
}
func (c *fileConfig) GetHosts() []string {
var hosts []string
for k := range c.Hosts {
hosts = append(hosts, k)
}
return hosts
}