-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathcommands.config.go
More file actions
340 lines (317 loc) · 10.5 KB
/
Copy pathcommands.config.go
File metadata and controls
340 lines (317 loc) · 10.5 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package temporalcli
import (
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"github.com/BurntSushi/toml"
"github.com/temporalio/cli/internal/printer"
"go.temporal.io/sdk/contrib/envconfig"
)
func (c *TemporalConfigDeleteCommand) run(cctx *CommandContext, _ []string) error {
// Load config
profileName := envConfigProfileName(cctx)
conf, confProfile, err := loadEnvConfigProfile(cctx, profileName, true)
if err != nil {
return err
}
if strings.HasPrefix(c.Prop, "grpc_meta.") {
key := strings.TrimPrefix(c.Prop, "grpc_meta.")
if _, ok := confProfile.GRPCMeta[key]; !ok {
return fmt.Errorf("gRPC meta key %q not found", key)
}
delete(confProfile.GRPCMeta, key)
} else {
reflectVal, err := reflectEnvConfigProp(confProfile, c.Prop, true)
if err != nil {
return err
}
reflectVal.SetZero()
}
// Save
return writeEnvConfigFile(cctx, conf)
}
func (c *TemporalConfigDeleteProfileCommand) run(cctx *CommandContext, _ []string) error {
// Load config
profileName := envConfigProfileName(cctx)
conf, _, err := loadEnvConfigProfile(cctx, profileName, true)
if err != nil {
return err
}
// To make extra sure they meant to do this, we require the profile name
// as an explicit CLI arg. This prevents accidentally deleting the
// "default" profile.
if cctx.RootCommand.Profile == "" {
return fmt.Errorf("to delete an entire profile, --profile must be provided explicitly")
}
delete(conf.Profiles, profileName)
// Save
return writeEnvConfigFile(cctx, conf)
}
func (c *TemporalConfigGetCommand) run(cctx *CommandContext, _ []string) error {
// Load config profile
profileName := envConfigProfileName(cctx)
conf, confProfile, err := loadEnvConfigProfile(cctx, profileName, true)
if err != nil {
return err
}
type prop struct {
Property string `json:"property"`
Value any `json:"value"`
}
// If there is a specific key requested, show it, otherwise show all
if c.Prop != "" {
// We do not support asking for structures with children at this time,
// but "tls" is a special case because it's also a bool.
if c.Prop == "codec" || c.Prop == "grpc_meta" {
return fmt.Errorf("must provide exact property, not parent property")
}
var reflectVal reflect.Value
// gRPC meta is special
if strings.HasPrefix(c.Prop, "grpc_meta.") {
v, ok := confProfile.GRPCMeta[strings.TrimPrefix(c.Prop, "grpc_meta.")]
if !ok {
return fmt.Errorf("unknown property %q", c.Prop)
}
reflectVal = reflect.ValueOf(v)
} else {
// Single value goes into property-value structure
reflectVal, err = reflectEnvConfigProp(confProfile, c.Prop, false)
if err != nil {
return err
}
// Pointers become true/false
if reflectVal.Kind() == reflect.Pointer {
reflectVal = reflect.ValueOf(!reflectVal.IsNil())
}
}
return cctx.Printer.PrintStructured(
prop{Property: c.Prop, Value: reflectVal.Interface()},
printer.StructuredOptions{Table: &printer.TableOptions{}},
)
} else if cctx.JSONOutput {
// If it is JSON and not prop specific, we want to dump the TOML
// structure in JSON form
var tomlConf struct {
Profiles map[string]any `toml:"profile"`
}
if b, err := conf.ToTOML(envconfig.ClientConfigToTOMLOptions{}); err != nil {
return fmt.Errorf("failed converting to TOML: %w", err)
} else if err := toml.Unmarshal(b, &tomlConf); err != nil {
return fmt.Errorf("failed converting from TOML: %w", err)
}
return cctx.Printer.PrintStructured(tomlConf.Profiles[profileName], printer.StructuredOptions{})
} else {
// Capture whether TLS is configured before the loop below. Looking up
// any "tls.*" property via reflectEnvConfigProp lazily initializes
// confProfile.TLS to a non-nil empty struct, which would otherwise make
// TLS appear configured when it is not (#1077).
tlsConfigured := confProfile.TLS != nil
// Get every property individually as a property-value pair except zero
// vals
var props []prop
for k := range envConfigPropsToFieldNames {
// TLS is a special case
if k == "tls" {
if tlsConfigured {
props = append(props, prop{Property: "tls", Value: true})
}
continue
}
if val, err := reflectEnvConfigProp(confProfile, k, false); err != nil {
return err
} else if !val.IsZero() {
props = append(props, prop{Property: k, Value: val.Interface()})
}
}
// Add "grpc_meta"
for k, v := range confProfile.GRPCMeta {
props = append(props, prop{Property: "grpc_meta." + k, Value: v})
}
// Sort and display
sort.Slice(props, func(i, j int) bool { return props[i].Property < props[j].Property })
return cctx.Printer.PrintStructured(props, printer.StructuredOptions{Table: &printer.TableOptions{}})
}
}
func (c *TemporalConfigListCommand) run(cctx *CommandContext, _ []string) error {
clientConfig, err := envconfig.LoadClientConfig(envconfig.LoadClientConfigOptions{
ConfigFilePath: cctx.RootCommand.ConfigFile,
EnvLookup: cctx.Options.EnvLookup,
})
if err != nil {
return err
}
type profile struct {
Name string `json:"name"`
}
profiles := make([]profile, 0, len(clientConfig.Profiles))
for k := range clientConfig.Profiles {
profiles = append(profiles, profile{Name: k})
}
sort.Slice(profiles, func(i, j int) bool { return profiles[i].Name < profiles[j].Name })
return cctx.Printer.PrintStructured(profiles, printer.StructuredOptions{Table: &printer.TableOptions{}})
}
func (c *TemporalConfigSetCommand) run(cctx *CommandContext, _ []string) error {
// Load config
conf, confProfile, err := loadEnvConfigProfile(cctx, envConfigProfileName(cctx), false)
if err != nil {
return err
}
// As a special case, "grpc_meta." values are handled specifically
if strings.HasPrefix(c.Prop, "grpc_meta.") {
if confProfile.GRPCMeta == nil {
confProfile.GRPCMeta = map[string]string{}
}
confProfile.GRPCMeta[strings.TrimPrefix(c.Prop, "grpc_meta.")] = c.Value
} else {
// Get reflect value
reflectVal, err := reflectEnvConfigProp(confProfile, c.Prop, false)
if err != nil {
return err
}
// Set it from string
switch reflectVal.Kind() {
case reflect.String:
reflectVal.SetString(c.Value)
case reflect.Pointer:
// Used for "tls", true makes an empty object, false sets nil
switch c.Value {
case "true":
// Only set if not set
if reflectVal.IsZero() {
reflectVal.Set(reflect.New(reflectVal.Type().Elem()))
}
case "false":
reflectVal.SetZero()
default:
return fmt.Errorf("must be 'true' or 'false' to set this property")
}
case reflect.Slice:
if reflectVal.Type().Elem().Kind() != reflect.Uint8 {
return fmt.Errorf("unexpected slice of type %v", reflectVal.Type())
}
reflectVal.SetBytes([]byte(c.Value))
case reflect.Bool:
if c.Value != "true" && c.Value != "false" {
return fmt.Errorf("must be 'true' or 'false' to set this property")
}
reflectVal.SetBool(c.Value == "true")
case reflect.Map:
return fmt.Errorf("must set each individual value of a map")
default:
return fmt.Errorf("unexpected type %v", reflectVal.Type())
}
}
// Save
return writeEnvConfigFile(cctx, conf)
}
func envConfigProfileName(cctx *CommandContext) string {
if cctx.RootCommand.Profile != "" {
return cctx.RootCommand.Profile
} else if p, _ := cctx.Options.EnvLookup.LookupEnv("TEMPORAL_PROFILE"); p != "" {
return p
}
return envconfig.DefaultConfigFileProfile
}
func loadEnvConfigProfile(
cctx *CommandContext,
profile string,
failIfNotFound bool,
) (*envconfig.ClientConfig, *envconfig.ClientConfigProfile, error) {
clientConfig, err := envconfig.LoadClientConfig(envconfig.LoadClientConfigOptions{
ConfigFilePath: cctx.RootCommand.ConfigFile,
EnvLookup: cctx.Options.EnvLookup,
})
if err != nil {
return nil, nil, err
}
// Load profile
clientProfile := clientConfig.Profiles[profile]
if clientProfile == nil {
if failIfNotFound {
return nil, nil, fmt.Errorf("profile %q not found", profile)
}
clientProfile = &envconfig.ClientConfigProfile{}
clientConfig.Profiles[profile] = clientProfile
}
return &clientConfig, clientProfile, nil
}
var envConfigPropsToFieldNames = map[string]string{
"address": "Address",
"namespace": "Namespace",
"api_key": "APIKey",
"tls": "TLS",
"tls.disabled": "Disabled",
"tls.client_cert_path": "ClientCertPath",
"tls.client_cert_data": "ClientCertData",
"tls.client_key_path": "ClientKeyPath",
"tls.client_key_data": "ClientKeyData",
"tls.server_ca_cert_path": "ServerCACertPath",
"tls.server_ca_cert_data": "ServerCACertData",
"tls.server_name": "ServerName",
"tls.disable_host_verification": "DisableHostVerification",
"codec.endpoint": "Endpoint",
"codec.auth": "Auth",
}
func reflectEnvConfigProp(
prof *envconfig.ClientConfigProfile,
prop string,
failIfParentNotFound bool,
) (reflect.Value, error) {
// Get field name
field := envConfigPropsToFieldNames[prop]
if field == "" {
return reflect.Value{}, fmt.Errorf("unknown property %q", prop)
}
// Load reflect val
parentVal := reflect.ValueOf(prof)
if strings.HasPrefix(prop, "tls.") {
if prof.TLS == nil {
if failIfParentNotFound {
return reflect.Value{}, fmt.Errorf("no TLS options found")
}
prof.TLS = &envconfig.ClientConfigTLS{}
}
parentVal = reflect.ValueOf(prof.TLS)
} else if strings.HasPrefix(prop, "codec.") {
if prof.Codec == nil {
if failIfParentNotFound {
return reflect.Value{}, fmt.Errorf("no codec options found")
}
prof.Codec = &envconfig.ClientConfigCodec{}
}
parentVal = reflect.ValueOf(prof.Codec)
}
// Return reflected field
if parentVal.Kind() == reflect.Pointer {
parentVal = parentVal.Elem()
}
return parentVal.FieldByName(field), nil
}
func writeEnvConfigFile(cctx *CommandContext, conf *envconfig.ClientConfig) error {
// Get file
configFile := cctx.RootCommand.ConfigFile
if configFile == "" {
configFile, _ = cctx.Options.EnvLookup.LookupEnv("TEMPORAL_CONFIG_FILE")
if configFile == "" {
var err error
if configFile, err = envconfig.DefaultConfigFilePath(); err != nil {
return err
}
}
}
// Convert to TOML
b, err := conf.ToTOML(envconfig.ClientConfigToTOMLOptions{})
if err != nil {
return fmt.Errorf("failed building TOML: %w", err)
}
// Write to file, making dirs as needed
if err := os.MkdirAll(filepath.Dir(configFile), 0700); err != nil {
return fmt.Errorf("failed making config file parent dirs: %w", err)
} else if err := os.WriteFile(configFile, b, 0600); err != nil {
return fmt.Errorf("failed writing config file: %w", err)
}
return nil
}