-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cli: add a --string flag to env set #467
base: main
Are you sure you want to change the base?
Conversation
faacaa3
to
9e00b86
Compare
@@ -57,8 +59,17 @@ func newEnvSetCmd(env *envCommand) *cobra.Command { | |||
return fmt.Errorf("path must contain at least one element") | |||
} | |||
|
|||
input := args[1] | |||
if rawString { | |||
js, err := json.Marshal(input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yaml.Node has a SetString helper that you can use, rather than this marshal/unmarshal round trip.
this also sets the block style automatically. a slight change lets us preserve that so we use the nicer block string syntax when there are newlines, rather than escaping them like the json marshaling does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I worry that we need to be a little bit more sophisticated than SetString
. That helper doesn't handle non-printable characters, for example (literal style scalars don't offer any escape sequences): https://github.com/go-yaml/yaml/blob/v3.0.1/yaml.go#L471-L483
We could use that method and then override the quoting style for strings with non-printable characters:
node.SetString(input)
if mustEscape := strings.ContainsFunc(input, func(r rune) bool { return !strconv.IsPrint(r) }); mustEscape {
node.Style = yaml.DoubleQuotedStyle
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems like they were trying to do it by falling back to base64 encoding if it !utf8.IsValid(), but if that's the wrong check then yeah, we should override the quoting style 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's not an adequate check. That'll catch invalid UTF-8 but not valid UTF-8 with non-printable chars.
@@ -0,0 +1,10 @@ | |||
run: | | |||
esc env init default/test | |||
esc env set default/test path "[a" --string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should also run an esc env get
to ensure that the string's content is as expected
Add a
--string
flag toenv set
to treat given value as a string