Skip to content

Commit

Permalink
Add --definition to get to print definition (#416)
Browse files Browse the repository at this point in the history
* Add --definition to get to print definition

* Tests

* Update changelog
  • Loading branch information
seanyeh authored Nov 5, 2024
1 parent 401ddcb commit 8a29190
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
### Improvements

- Add `--definition` flag to `esc env get` to output definition
[#416](https://github.com/pulumi/esc/pull/416)

### Bug Fixes

### Breaking changes
Expand Down
15 changes: 14 additions & 1 deletion cmd/esc/cli/env_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type envGetCommand struct {

func newEnvGetCmd(env *envCommand) *cobra.Command {
var value string
var defOnly bool
var showSecrets bool

get := &envGetCommand{env: env}
Expand Down Expand Up @@ -67,6 +68,10 @@ func newEnvGetCmd(env *envCommand) *cobra.Command {
}
}

if defOnly && value != "" {
return fmt.Errorf("`--value` and `--definition` flags cannot be used together")
}

switch value {
case "":
// OK
Expand Down Expand Up @@ -94,6 +99,11 @@ func newEnvGetCmd(env *envCommand) *cobra.Command {
return nil
}

if defOnly {
fmt.Fprint(get.env.esc.stdout, data.Definition)
return nil
}

var markdown bytes.Buffer
if err := envGetTemplate.Execute(&markdown, data); err != nil {
return fmt.Errorf("internal error: rendering: %w", err)
Expand All @@ -117,9 +127,12 @@ func newEnvGetCmd(env *envCommand) *cobra.Command {
},
}

cmd.Flags().BoolVar(
&defOnly, "definition", false,
"Set to print just the definition.")
cmd.Flags().StringVar(
&value, "value", "",
"set to print just the value in the given format. may be 'dotenv', 'json', 'detailed', or 'shell'")
"Set to print just the value in the given format. May be 'dotenv', 'json', 'detailed', or 'shell'")
cmd.Flags().BoolVar(
&showSecrets, "show-secrets", false,
"Show static secrets in plaintext rather than ciphertext")
Expand Down
7 changes: 7 additions & 0 deletions cmd/esc/cli/testdata/env-get-definition-error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
run: esc env get default/test --definition --value=json
error: exit status 1
stdout: |
> esc env get default/test --definition --value=json
stderr: |
> esc env get default/test --definition --value=json
Error: `--value` and `--definition` flags cannot be used together
22 changes: 22 additions & 0 deletions cmd/esc/cli/testdata/env-get-definition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
run: |
esc env get default/test --definition
environments:
test-user/default/test:
imports:
- a
values:
# comment
string: esc
files:
FILE: ${string}
stdout: |
> esc env get default/test --definition
imports:
- a
values:
# comment
string: esc
files:
FILE: ${string}
stderr: |
> esc env get default/test --definition

0 comments on commit 8a29190

Please sign in to comment.