/
context.go
194 lines (157 loc) · 4.19 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package config
import (
"errors"
"fmt"
"strings"
)
// Contexts holds all available Context structs in a map
type Contexts struct {
Contexts map[string]Context `mapstructure:"contexts"`
}
// Context represents a single cluster context
type Context struct {
Domain string `mapstructure:"domain"`
Workspace string `mapstructure:"workspace"`
Token string `mapstructure:"token"`
}
// GetCurrentContext looks up current context and gets cooresponding Context struct
func GetCurrentContext() (Context, error) {
c := Context{}
domain := CFG.Context.GetHomeString()
if len(domain) == 0 {
return Context{}, errors.New("No context set, have you authenticated to a cluster?")
}
c.Domain = domain
return c.GetContext()
}
// PrintContext prints current context to stdOut
func (c Context) PrintContext() error {
c, err := c.GetContext()
if err != nil {
return err
}
cluster := c.Domain
if len(cluster) == 0 {
cluster = "N/A"
}
workspace := c.Workspace
if len(workspace) == 0 {
workspace = "N/A"
}
r := "%-36s %-36s\n"
fmt.Printf(r, "CLUSTER", "WORKSPACE")
fmt.Printf(r, cluster, workspace)
return nil
}
// PrintCurrentContext prints the current config context
func PrintCurrentContext() error {
c, err := GetCurrentContext()
if err != nil {
return err
}
err = c.PrintContext()
if err != nil {
return err
}
return nil
}
// GetContextKey allows a cluster domain to be used without interfering
// with viper's dot (.) notation for fetching configs by replacing with underscores (_)
func (c Context) GetContextKey() (string, error) {
if len(c.Domain) == 0 {
return "", errors.New("Cluster config invalid, no domain specified")
}
return strings.Replace(c.Domain, ".", "_", -1), nil
}
// ContextExists checks if a cluster struct exists in config
// based on Cluster.Domain
// Returns a boolean indicating whether or not cluster exists
func (c Context) ContextExists() bool {
key, err := c.GetContextKey()
if err != nil {
return false
}
return viperHome.IsSet("contexts" + "." + key)
}
// GetContext gets the full context from the specified Context receiver struct
// Returns based on Domain prop
func (c Context) GetContext() (Context, error) {
key, err := c.GetContextKey()
if err != nil {
return c, err
}
if !c.ContextExists() {
return c, errors.New("Cluster not set, have you authenticated to this cluster?")
}
err = viperHome.UnmarshalKey("contexts"+"."+key, &c)
if err != nil {
return c, err
}
return c, nil
}
// GetContexts gets all contexts currently configured in the global config
// Returns a Contexts struct containing a map of all Context structs
func (c Contexts) GetContexts() (Contexts, error) {
err := viperHome.Unmarshal(&c)
if err != nil {
return c, err
}
return c, nil
}
// SetContext saves Context to the config
func (c Context) SetContext() error {
key, err := c.GetContextKey()
if err != nil {
return err
}
context := map[string]string{
"token": c.Token,
"domain": c.Domain,
"workspace": c.Workspace,
}
viperHome.Set("contexts"+"."+key, context)
saveConfig(viperHome, HomeConfigFile)
return nil
}
// SetContextKey saves a single context key value pair
func (c Context) SetContextKey(key, value string) error {
cKey, err := c.GetContextKey()
if err != nil {
return err
}
cfgPath := fmt.Sprintf("contexts.%s.%s", cKey, key)
viperHome.Set(cfgPath, value)
saveConfig(viperHome, HomeConfigFile)
return nil
}
// SwitchContext sets the current config context to the one matching the provided Context struct
func (c Context) SwitchContext() error {
if c.ContextExists() {
viperHome.Set("context", c.Domain)
saveConfig(viperHome, HomeConfigFile)
}
return nil
}
// GetAPIURL returns full Houston API Url for the provided Context
func (c Context) GetAPIURL() string {
if c.Domain == "localhost" {
return CFG.LocalHouston.GetString()
}
return fmt.Sprintf(
"%s://houston.%s:%s/v1",
CFG.CloudAPIProtocol.GetString(),
c.Domain,
CFG.CloudAPIPort.GetString(),
)
}
// GetAppURL returns full Houston API Url for the provided Context
func (c Context) GetAppURL() string {
if c.Domain == "localhost" {
return CFG.LocalOrbit.GetString()
}
return fmt.Sprintf(
"%s://app.%s",
CFG.CloudAPIProtocol.GetString(),
c.Domain,
)
}