fix(policy-devel): inputs don't accept multiple values#2355
Conversation
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
| cmd.Flags().StringVar(&kind, "kind", "", fmt.Sprintf("Kind of the material: %q", schemaapi.ListAvailableMaterialKind())) | ||
| cmd.Flags().StringSliceVar(&annotations, "annotation", []string{}, "Key-value pairs of material annotations (key=value)") | ||
| cmd.Flags().StringVarP(&policyPath, "policy", "p", "policy.yaml", "Path to custom policy file") | ||
| cmd.Flags().StringSliceVar(&inputs, "input", []string{}, "Key-value pairs of policy inputs (key=value)") |
There was a problem hiding this comment.
mind explaining why the previous one didn't work? Thanks!
There was a problem hiding this comment.
The issue was caused by how Cobra treats commas for StringSliceVar. Here Cobra does preprocessing where values are split with , into separate strings, where in StringArrayVar strings are kept as is.
There was a problem hiding this comment.
so in that case we should use String no? StringSliceVar allows us to do
--input foo=bar --input bar=baz
is what I wrote correct?
There was a problem hiding this comment.
StringArrayVar also allows
--input foo=bar --input bar=baz
but also allows
--input foo=bar,baz
to be treated as multi value, but has a trade-off of not being able to declare
--input foo=bar,bar=baz
because currently that becomes
[]string{"foo=bar,bar", "baz"}
Both StringSliceVar and StringArrayVar have drawbacks that can be fixed with custom parsing logic
There was a problem hiding this comment.
right, but my question is, do we need multi input or regular strings are enough? I thought that we were asking to provide comma separated values and the engine was doing smth, I guess that not, that we want to have arrays.
There was a problem hiding this comment.
We want multi input, the previous change related to commas was for escaping comma separated values, before there was no way to pass e.g.
with:
foo: bar, baz
to get
"foo": "bar, baz"
now \, can be used to get that output. But in this case using StringArrayVar makes it so the input is preprocessed by Cobra before it is passed to engine
This PR fixes issue with passing arrays in
--inputflag.Example
By passing
--input licenses=AGPL,GPLin the eval command values can now be accessed withinput.args.licenses[0]andinput.args.licenses[1]in the rego script.Closes #2351