Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

seanyeh
Copy link
Contributor

@seanyeh seanyeh commented Feb 27, 2025

Add a --string flag to env set to treat given value as a string

@seanyeh seanyeh force-pushed the pgavlin/env-set-string branch from faacaa3 to 9e00b86 Compare February 27, 2025 18:08
@seanyeh seanyeh linked an issue Feb 27, 2025 that may be closed by this pull request
@seanyeh seanyeh marked this pull request as ready for review February 27, 2025 18:10
@seanyeh seanyeh requested review from pgavlin and nyobe February 27, 2025 18:23
@@ -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)
Copy link
Contributor

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

Copy link
Member

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
}

Copy link
Contributor

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 👍

Copy link
Member

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't set value starting with [ or ]
3 participants