-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
174 lines (147 loc) · 3.81 KB
/
config.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
165
166
167
168
169
170
171
172
173
174
// Package config parses config and plugin configuration files. It provides APIs to read
// and write config files and manage multiple profiles.
package config
import (
"fmt"
"net/url"
)
// The Config structure holds the config data from the selected profile.
type Config struct {
Uri *url.URL
Organization string
Tenant string
Parameter map[string]string
Header map[string]string
Auth AuthConfig
Insecure bool
Debug bool
Output string
Version string
}
// AuthConfig with metadata used for authenticating the caller.
type AuthConfig struct {
Type string
Config map[string]interface{}
}
const clientIdKey = "clientId"
const clientSecretKey = "clientSecret"
const redirectUriKey = "redirectUri"
const scopesKey = "scopes"
const patKey = "pat"
func (c Config) ClientId() string {
clientId := c.Auth.Config[clientIdKey]
if clientId == nil {
return ""
}
return fmt.Sprintf("%v", clientId)
}
func (c Config) ClientSecret() string {
clientSecret := c.Auth.Config[clientSecretKey]
if clientSecret == nil {
return ""
}
return fmt.Sprintf("%v", clientSecret)
}
func (c Config) RedirectUri() string {
redirectUri := c.Auth.Config[redirectUriKey]
if redirectUri == nil {
return ""
}
return fmt.Sprintf("%v", redirectUri)
}
func (c Config) Scopes() string {
scopes := c.Auth.Config[scopesKey]
if scopes == nil {
return ""
}
return fmt.Sprintf("%v", scopes)
}
func (c Config) Pat() string {
pat := c.Auth.Config[patKey]
if pat == nil {
return ""
}
return fmt.Sprintf("%v", pat)
}
func (c *Config) ConfigureOrgTenant(organization string, tenant string) bool {
if organization != "" {
c.Organization = organization
}
if tenant != "" {
c.Tenant = tenant
}
return organization != "" || tenant != ""
}
func (c Config) ConfigurePatAuth(pat string) bool {
delete(c.Auth.Config, clientIdKey)
delete(c.Auth.Config, clientSecretKey)
delete(c.Auth.Config, redirectUriKey)
delete(c.Auth.Config, scopesKey)
if pat != "" {
c.Auth.Config[patKey] = pat
}
return pat != ""
}
func (c Config) ConfigureLoginAuth(clientId string, redirectUri string, scopes string) bool {
delete(c.Auth.Config, clientSecretKey)
delete(c.Auth.Config, patKey)
if clientId != "" {
c.Auth.Config[clientIdKey] = clientId
}
if redirectUri != "" {
c.Auth.Config[redirectUriKey] = redirectUri
}
if scopes != "" {
c.Auth.Config[scopesKey] = scopes
}
return clientId != "" || redirectUri != "" || scopes != ""
}
func (c Config) ConfigureCredentialsAuth(clientId string, clientSecret string) bool {
delete(c.Auth.Config, redirectUriKey)
delete(c.Auth.Config, scopesKey)
delete(c.Auth.Config, patKey)
if clientId != "" {
c.Auth.Config[clientIdKey] = clientId
}
if clientSecret != "" {
c.Auth.Config[clientSecretKey] = clientSecret
}
return clientId != "" || clientSecret != ""
}
func (c *Config) SetUri(uri string) error {
parsedUri, err := url.Parse(uri)
if err != nil {
return fmt.Errorf("Invalid value for 'uri': %w", err)
}
c.Uri = parsedUri
return nil
}
func (c *Config) SetInsecure(insecure bool) {
c.Insecure = insecure
}
func (c *Config) SetDebug(debug bool) {
c.Debug = debug
}
func (c Config) SetHeader(key string, value string) {
c.Header[key] = value
}
func (c Config) SetParameter(key string, value string) {
c.Parameter[key] = value
}
func (c Config) SetAuthGrantType(grantType string) {
c.Auth.Config["grantType"] = grantType
}
func (c Config) SetAuthScopes(scopes string) {
c.Auth.Config["scopes"] = scopes
}
func (c Config) SetAuthProperty(key string, value string) {
properties, ok := c.Auth.Config["properties"].(map[interface{}]interface{})
if properties == nil || !ok {
properties = map[interface{}]interface{}{}
}
properties[key] = value
c.Auth.Config["properties"] = properties
}
func (c *Config) SetVersion(version string) {
c.Version = version
}