-
Notifications
You must be signed in to change notification settings - Fork 40
/
env.go
249 lines (218 loc) · 6.37 KB
/
env.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
Copyright 2019 Splunk Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
"github.com/splunk/qbec/internal/cmd"
)
func newEnvCommand(cp ctxProvider) *cobra.Command {
cmd := &cobra.Command{
Use: "env <subcommand>",
Short: "environment lists and details",
}
cmd.AddCommand(newEnvListCommand(cp), newEnvVarsCommand(cp), newEnvPropsCommand(cp))
return cmd
}
type envListCommandConfig struct {
cmd.AppContext
format string
}
func newEnvListCommand(cp ctxProvider) *cobra.Command {
c := &cobra.Command{
Use: "list [-o <format>]",
Short: "list all environments in short, json or yaml format",
Example: envListExamples(),
}
config := envListCommandConfig{}
c.Flags().StringVarP(&config.format, "format", "o", "", "use json|yaml to display machine readable output")
c.RunE = func(c *cobra.Command, args []string) error {
config.AppContext = cp()
return cmd.WrapError(doEnvList(args, config))
}
return c
}
type displayEnv struct {
Name string `json:"name"`
Server string `json:"server"`
DefaultNamespace string `json:"defaultNamespace"`
}
type displayEnvList struct {
Environments []displayEnv `json:"environments"`
}
func listEnvironments(config envListCommandConfig) error {
app := config.App()
var list []displayEnv
for name, obj := range app.Environments() {
defNs := obj.DefaultNamespace
if defNs == "" {
defNs = "default"
}
list = append(list, displayEnv{
Name: name,
Server: obj.Server,
DefaultNamespace: defNs,
})
}
sort.Slice(list, func(i, j int) bool {
return list[i].Name < list[j].Name
})
wrapper := displayEnvList{list}
w := config.Stdout()
switch config.format {
case "json":
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
_ = enc.Encode(wrapper)
case "yaml":
b, _ := yaml.Marshal(wrapper)
_, _ = w.Write(b)
case "":
for _, e := range list {
fmt.Fprintln(w, e.Name)
}
default:
return cmd.NewUsageError(fmt.Sprintf("listEnvironments: unsupported format %q", config.format))
}
return nil
}
func doEnvList(args []string, config envListCommandConfig) error {
if len(args) != 0 {
return cmd.NewUsageError("extra arguments specified")
}
return listEnvironments(config)
}
func newEnvVarsCommand(cp ctxProvider) *cobra.Command {
c := &cobra.Command{
Use: "vars [-o <format>] <env>",
Short: "print variables for kubeconfig, context and cluster for an environment",
Example: envVarsExamples(),
}
config := envVarsCommandConfig{}
c.Flags().StringVarP(&config.format, "format", "o", "", "use json|yaml to display machine readable output")
c.RunE = func(c *cobra.Command, args []string) error {
config.AppContext = cp()
return cmd.WrapError(doEnvVars(args, config))
}
return c
}
type envVarsCommandConfig struct {
cmd.AppContext
format string
}
func doEnvVars(args []string, config envVarsCommandConfig) error {
if len(args) != 1 {
return cmd.NewUsageError(fmt.Sprintf("exactly one environment required, but provided: %q", args))
}
if _, ok := config.App().Environments()[args[0]]; !ok {
return fmt.Errorf("invalid environment: %q", args[0])
}
return environmentVars(args[0], config)
}
func environmentVars(name string, config envVarsCommandConfig) error {
envCtx, err := config.EnvContext(name)
if err != nil {
return err
}
attrs, err := envCtx.KubeAttributes()
if err != nil {
return err
}
w := config.Stdout()
switch config.format {
case "json":
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
_ = enc.Encode(attrs)
case "yaml":
b, _ := yaml.Marshal(attrs)
_, _ = w.Write(b)
case "":
var kcArgs []string
addArg := func(name, value string) {
if value != "" {
kcArgs = append(kcArgs, fmt.Sprintf(`--%s=%s`, name, value))
}
}
addArg("context", attrs.Context)
addArg("cluster", attrs.Cluster)
addArg("namespace", attrs.Namespace)
var lines []string
var vars []string
addLine := func(name, value string) {
lines = append(lines, fmt.Sprintf(`%s='%s'`, name, value))
vars = append(vars, name)
}
addLine("KUBECONFIG", attrs.ConfigFile)
addLine("KUBE_CLUSTER", attrs.Cluster)
addLine("KUBE_CONTEXT", attrs.Context)
addLine("KUBE_NAMESPACE", attrs.Namespace)
addLine("KUBECTL_ARGS", strings.Join(kcArgs, " "))
for _, l := range lines {
fmt.Fprintf(w, "%s;\n", l)
}
fmt.Fprintf(w, "export %s\n", strings.Join(vars, " "))
default:
return cmd.NewUsageError(fmt.Sprintf("environmentVars: unsupported format %q", config.format))
}
return nil
}
func newEnvPropsCommand(cp ctxProvider) *cobra.Command {
c := &cobra.Command{
Use: "props [-o <format>] <env>",
Short: "print properties for an environment",
}
config := envPropsCommandConfig{}
c.Flags().StringVarP(&config.format, "format", "o", "", "use json|yaml to display machine readable output")
c.RunE = func(c *cobra.Command, args []string) error {
config.AppContext = cp()
return cmd.WrapError(doEnvProps(args, config))
}
return c
}
type envPropsCommandConfig struct {
cmd.AppContext
format string
}
func doEnvProps(args []string, config envPropsCommandConfig) error {
if len(args) != 1 {
return cmd.NewUsageError(fmt.Sprintf("exactly one environment required, but provided: %q", args))
}
if _, ok := config.App().Environments()[args[0]]; !ok {
return fmt.Errorf("invalid environment: %q", args[0])
}
return environmentProps(args[0], config)
}
func environmentProps(name string, config envPropsCommandConfig) error {
props, err := config.App().Properties(name)
if err != nil {
return err
}
w := config.Stdout()
switch config.format {
case "json":
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
_ = enc.Encode(props)
case "", "yaml":
b, _ := yaml.Marshal(props)
_, _ = w.Write(b)
default:
return cmd.NewUsageError(fmt.Sprintf("environmentVars: unsupported format %q", config.format))
}
return nil
}