Skip to content

Commit cb8c1a7

Browse files
committed
fix: support [a].[b] in set argument
1 parent 093dfe1 commit cb8c1a7

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

cmd/kk/app/options/option.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,13 @@ func (o *CommonOptions) completeInventory(inventory *kkcorev1.Inventory) {
232232
}
233233
}
234234

235-
// setValue set key: val in config.
236-
// If val is json string. convert to map or slice
237-
// If val is TRUE,YES,Y. convert to bool type true.
238-
// If val is FALSE,NO,N. convert to bool type false.
235+
// setValue sets a value in the config based on a key-value pair.
236+
// It supports different value types:
237+
// - JSON objects (starting with '{' and ending with '}')
238+
// - JSON arrays (starting with '[' and ending with ']')
239+
// - Boolean values (true/false, yes/no, y/n - case insensitive)
240+
// - String values (default case)
241+
// The key can contain dots to indicate nested fields.
239242
func setValue(config *kkcorev1.Config, key, val string) error {
240243
switch {
241244
case strings.HasPrefix(val, "{") && strings.HasSuffix(val, "{"):
@@ -245,7 +248,7 @@ func setValue(config *kkcorev1.Config, key, val string) error {
245248
return errors.Wrapf(err, "failed to unmarshal json object value for \"--set %s\"", key)
246249
}
247250

248-
return errors.Wrapf(unstructured.SetNestedMap(config.Value(), value, key),
251+
return errors.Wrapf(unstructured.SetNestedMap(config.Value(), value, strings.Split(key, ".")...),
249252
"failed to set \"--set %s\" to config", key)
250253
case strings.HasPrefix(val, "[") && strings.HasSuffix(val, "]"):
251254
var value []any
@@ -254,16 +257,16 @@ func setValue(config *kkcorev1.Config, key, val string) error {
254257
return errors.Wrapf(err, "failed to unmarshal json array value for \"--set %s\"", key)
255258
}
256259

257-
return errors.Wrapf(unstructured.SetNestedSlice(config.Value(), value, key),
260+
return errors.Wrapf(unstructured.SetNestedSlice(config.Value(), value, strings.Split(key, ".")...),
258261
"failed to set \"--set %s\" to config", key)
259262
case strings.EqualFold(val, "TRUE") || strings.EqualFold(val, "YES") || strings.EqualFold(val, "Y"):
260-
return errors.Wrapf(unstructured.SetNestedField(config.Value(), true, key),
263+
return errors.Wrapf(unstructured.SetNestedField(config.Value(), true, strings.Split(key, ".")...),
261264
"failed to set \"--set %s\" to config", key)
262265
case strings.EqualFold(val, "FALSE") || strings.EqualFold(val, "NO") || strings.EqualFold(val, "N"):
263-
return errors.Wrapf(unstructured.SetNestedField(config.Value(), false, key),
266+
return errors.Wrapf(unstructured.SetNestedField(config.Value(), false, strings.Split(key, ".")...),
264267
"failed to set \"--set %s\" to config", key)
265268
default:
266-
return errors.Wrapf(unstructured.SetNestedField(config.Value(), val, key),
269+
return errors.Wrapf(unstructured.SetNestedField(config.Value(), val, strings.Split(key, ".")...),
267270
"failed to set \"--set %s\" to config", key)
268271
}
269272
}

0 commit comments

Comments
 (0)