Skip to content

Commit

Permalink
cli: do not panic in env set foo bar "" (#110)
Browse files Browse the repository at this point in the history
After interpretation by the shell, `env set foo bar ""` passes an empty
string as the value to set. The YAML parser treats the empty string as
"no document" and doesn't return any content from Unmarshal. Work around
this by treating "no document" as an empty string _literal_ (which is
likely the intent).

Fixes #109.
  • Loading branch information
pgavlin authored Oct 12, 2023
1 parent 2274105 commit e0462cc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### Improvements

### Bug Fixes

- Do not panic when `env set` is passed an empty value.
[#110](https://github.com/pulumi/esc/pull/110)
6 changes: 6 additions & 0 deletions cmd/esc/cli/env_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func newEnvSetCmd(env *envCommand) *cobra.Command {
if err := yaml.Unmarshal([]byte(args[1]), &yamlValue); err != nil {
return fmt.Errorf("invalid value: %w", err)
}
if len(yamlValue.Content) == 0 {
// This can happen when the value is empty (e.g. when "" is present on the command line). Treat this
// as the empty string.
err = yaml.Unmarshal([]byte(`""`), &yamlValue)
contract.IgnoreError(err)
}
yamlValue = *yamlValue.Content[0]

if looksLikeSecret(path, yamlValue) && !secret && !plaintext {
Expand Down
55 changes: 51 additions & 4 deletions cmd/esc/cli/testdata/env-set.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ run: |
esc env set test 'array[1]' esc && esc env get test
esc env set test 'array[2]' '{}' && esc env get test
esc env set test 'array[2].foo' bar && esc env get test
esc env set test 'array[2].foo' '' && esc env get test
esc env init test2
esc env set test 'imports[0]' test2 && esc env get test
esc env init test3
Expand Down Expand Up @@ -351,6 +352,50 @@ stdout: |+
```
> esc env set test array[2].foo
> esc env get test
# Value
```json
{
"array": [
"hello",
"esc",
{
"foo": ""
}
],
"foo": {
"bar": {
"alpha": "zed",
"baz": "qux"
},
"beta": [
"gamma",
42
]
},
"open": "[unknown]"
}
```
# Definition
```yaml
values:
foo:
bar:
baz: qux
alpha: zed
beta:
- gamma
- 42
open:
"fn::open::test": "cse"
array:
- hello
- esc
- {foo: ""}
```
> esc env init test2
Environment created.
> esc env set test imports[0] test2
Expand All @@ -362,7 +407,7 @@ stdout: |+
"hello",
"esc",
{
"foo": "bar"
"foo": ""
}
],
"foo": {
Expand Down Expand Up @@ -393,7 +438,7 @@ stdout: |+
array:
- hello
- esc
- {foo: bar}
- {foo: ""}
imports:
- test2
Expand All @@ -410,7 +455,7 @@ stdout: |+
"hello",
"esc",
{
"foo": "bar"
"foo": ""
}
],
"foo": {
Expand Down Expand Up @@ -441,7 +486,7 @@ stdout: |+
array:
- hello
- esc
- {foo: bar}
- {foo: ""}
imports:
- test2
- test3
Expand Down Expand Up @@ -470,6 +515,8 @@ stderr: |
> esc env get test
> esc env set test array[2].foo bar
> esc env get test
> esc env set test array[2].foo
> esc env get test
> esc env init test2
> esc env set test imports[0] test2
> esc env get test
Expand Down

0 comments on commit e0462cc

Please sign in to comment.