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

Make the value arg optional for set command #394

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- Improve evaluation performance and memory footprint.
[#392](https://github.com/pulumi/esc/pull/392)

- Prompt for the value if the value arg is not passed.
[#394](https://github.com/pulumi/esc/pull/394)

### Bug Fixes

### Breaking changes
Expand Down
34 changes: 30 additions & 4 deletions cmd/esc/cli/env_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ package cli
import (
"context"
"fmt"
"io"
"os"
"regexp"
"strconv"

"github.com/ccojocar/zxcvbn-go"
"github.com/spf13/cobra"
"golang.org/x/term"
"gopkg.in/yaml.v3"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

Expand Down Expand Up @@ -44,9 +48,6 @@ func newEnvSetCmd(env *envCommand) *cobra.Command {
if ref.version != "" {
return fmt.Errorf("the set command does not accept versions")
}
if len(args) < 2 {
return fmt.Errorf("expected a path and a value")
}

path, err := resource.ParsePropertyPath(args[0])
if err != nil {
Expand All @@ -56,8 +57,33 @@ func newEnvSetCmd(env *envCommand) *cobra.Command {
return fmt.Errorf("path must contain at least one element")
}

var value string
switch {
case len(args) == 2:
value = args[1]
//nolint:gosec // os.Stdin.Fd() == 0: uintptr -> int conversion is always safe
case !term.IsTerminal(int(os.Stdin.Fd())):
b, readerr := io.ReadAll(os.Stdin)
if readerr != nil {
return readerr
}
value = cmdutil.RemoveTrailingNewline(string(b))
case !cmdutil.Interactive():
return fmt.Errorf("value must be specified in non-interactive mode")
case secret:
value, err = cmdutil.ReadConsoleNoEcho("value")
if err != nil {
return err
}
default:
value, err = cmdutil.ReadConsole("value")
if err != nil {
return err
}
}

var yamlValue yaml.Node
if err := yaml.Unmarshal([]byte(args[1]), &yamlValue); err != nil {
if err := yaml.Unmarshal([]byte(value), &yamlValue); err != nil {
return fmt.Errorf("invalid value: %w", err)
}
if len(yamlValue.Content) == 0 {
Expand Down
20 changes: 20 additions & 0 deletions cmd/esc/cli/testdata/env-set-secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ run: |
esc env get default/test
esc env set default/test password true --secret
esc env get default/test
echo 'mysecret' | esc env set default/test password --secret
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the PR description, I can't get this to work. It's as if echo 'mysecret' evaluates to an empty string because that's what the value of esc env set ... ends up getting. However, building a local version of the CLI and executing the same thing works as expected, so I am not sure what is going on with the test runner.

esc env get default/test --show-secrets
esc env set default/test password '[]' --secret || echo OK
stdout: |
> esc env init default/test
Expand Down Expand Up @@ -166,6 +168,22 @@ stdout: |

```

> esc env set default/test password --secret
> esc env get default/test --show-secrets
# Value
```json
{
"password": "mysecret"
}
```
# Definition
```yaml
values:
password:
fn::secret: mysecret

```

> esc env set default/test password [] --secret
stderr: |
> esc env init default/test
Expand All @@ -186,5 +204,7 @@ stderr: |
> esc env get default/test
> esc env set default/test password true --secret
> esc env get default/test
> esc env set default/test password --secret
> esc env get default/test --show-secrets
> esc env set default/test password [] --secret
test:3:21: secret values must be string literals
Loading