Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -1,5 +1,8 @@
### Improvements

- Surface warnings when editing environments with the CLI
[#631](https://github.com/pulumi/esc/pull/631)

### Bug Fixes

### Breaking changes
23 changes: 16 additions & 7 deletions cmd/esc/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/hashicorp/hcl/v2"
"github.com/pgavlin/fx/v2"
"github.com/pgavlin/fx/v2/maps"
"github.com/pulumi/esc"
Expand Down Expand Up @@ -388,10 +389,16 @@ func mapDiags(diags syntax.Diagnostics) []client.EnvironmentDiagnostic {
}
}

severity := client.DiagError
if d.Severity == hcl.DiagWarning {
severity = client.DiagWarning
}

out[i] = client.EnvironmentDiagnostic{
Range: rng,
Summary: d.Summary,
Detail: d.Detail,
Range: rng,
Summary: d.Summary,
Detail: d.Detail,
Severity: severity,
}
}
return out
Expand Down Expand Up @@ -669,12 +676,13 @@ func (c *testPulumiClient) UpdateEnvironmentWithRevision(
return nil, 0, errors.New("etag mismatch")
}

_, diags, err := c.checkEnvironment(ctx, orgName, envName, yaml, nil)
if err == nil && len(diags) == 0 {
envId := projectName + "/" + envName
_, diags, err := c.checkEnvironment(ctx, orgName, envId, yaml, nil)
if err == nil && !client.DiagnosticsHaveErrors(diags) {
h := fnv.New32()
h.Write(yaml)

yaml, err = eval.EncryptSecrets(ctx, envName, yaml, rot128{})
yaml, err = eval.EncryptSecrets(ctx, envId, yaml, rot128{})
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -713,7 +721,8 @@ func (c *testPulumiClient) CreateEnvironmentDraft(
return "", nil, errors.New("etag mismatch")
}

_, diags, err := c.checkEnvironment(ctx, orgName, envName, yaml, nil)
envId := projectName + "/" + envName
_, diags, err := c.checkEnvironment(ctx, orgName, envId, yaml, nil)
if err != nil || len(diags) != 0 {
return "", diags, nil
}
Expand Down
27 changes: 24 additions & 3 deletions cmd/esc/cli/client/apitype.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,31 @@ import (
"github.com/pulumi/esc"
)

type EnvironmentDiagnosticSeverity string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is there a reason we use string instead of hcl.DiagnosticSeverity? (I'm fine with either)


const (
DiagError EnvironmentDiagnosticSeverity = "error"
DiagWarning EnvironmentDiagnosticSeverity = "warning"
)

type EnvironmentDiagnostic struct {
Range *esc.Range `json:"range,omitempty"`
Summary string `json:"summary,omitempty"`
Detail string `json:"detail,omitempty"`
Range *esc.Range `json:"range,omitempty"`
Summary string `json:"summary,omitempty"`
Detail string `json:"detail,omitempty"`
Severity EnvironmentDiagnosticSeverity `json:"severity,omitempty"`
}

func (d EnvironmentDiagnostic) IsError() bool {
return d.Severity != DiagWarning
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i would check if severity == error

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added the == error check but we also need == "" for backward compat.

}

func DiagnosticsHaveErrors(diags []EnvironmentDiagnostic) bool {
for _, d := range diags {
if d.IsError() {
return true
}
}
return false
}

type EnvironmentErrorResponse struct {
Expand Down
8 changes: 7 additions & 1 deletion cmd/esc/cli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,13 +713,19 @@ func (pc *client) UpdateEnvironmentWithRevision(
}
return nil, 0, err
}
defer contract.IgnoreClose(resp.Body)

revision, err := strconv.Atoi(resp.Header.Get(revisionHeader))
if err != nil {
return nil, 0, fmt.Errorf("parsing revision number: %w", err)
}

return nil, revision, nil
var updateResp UpdateEnvironmentResponse
if err := json.NewDecoder(resp.Body).Decode(&updateResp); err != nil {
return nil, revision, nil
}

return updateResp.Diagnostics, revision, nil
}

func (pc *client) CreateEnvironmentDraft(
Expand Down
12 changes: 8 additions & 4 deletions cmd/esc/cli/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,12 @@ func (cmd *envCommand) writeYAMLEnvironmentDiagnostics(
},
}
}
severity := hcl.DiagError
if d.Severity == client.DiagWarning {
severity = hcl.DiagWarning
}
err := writer.WriteDiagnostic(&hcl.Diagnostic{
Severity: hcl.DiagError,
Severity: severity,
Summary: d.Summary,
Subject: subject,
})
Expand Down Expand Up @@ -439,7 +443,7 @@ func (esc *escCommand) updateEnvironment(
if err != nil {
return nil, fmt.Errorf("creating environment draft: %w", err)
}
if len(diags) == 0 {
if !client.DiagnosticsHaveErrors(diags) {
fmt.Fprintf(esc.stdout, "Change request created: %v\n", changeRequestID)
fmt.Fprintf(esc.stdout, "Change request URL: %v\n", esc.changeRequestURL(ref, changeRequestID))

Expand All @@ -456,7 +460,7 @@ func (esc *escCommand) updateEnvironment(
if err != nil {
return nil, fmt.Errorf("updating environment draft: %w", err)
}
if len(diags) == 0 {
if !client.DiagnosticsHaveErrors(diags) {
fmt.Fprintln(esc.stdout, "Change request updated")
fmt.Fprintf(esc.stdout, "Change request URL: %v\n", esc.changeRequestURL(ref, changeRequestID))
}
Expand All @@ -467,7 +471,7 @@ func (esc *escCommand) updateEnvironment(
if err != nil {
return nil, fmt.Errorf("updating environment definition: %w", err)
}
if len(diags) == 0 && envUpdateSuccessMessage != "" {
if !client.DiagnosticsHaveErrors(diags) && envUpdateSuccessMessage != "" {
fmt.Fprintln(esc.stdout, envUpdateSuccessMessage)
}
return diags, nil
Expand Down
15 changes: 10 additions & 5 deletions cmd/esc/cli/env_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func newEnvEditCmd(env *envCommand) *cobra.Command {
}

if len(diags) != 0 {
return edit.env.writeYAMLEnvironmentDiagnostics(edit.env.esc.stderr, ref.envName, yaml, diags)
err = edit.env.writeYAMLEnvironmentDiagnostics(edit.env.esc.stderr, ref.projectName+"/"+ref.envName, yaml, diags)
contract.IgnoreError(err)
}
if client.DiagnosticsHaveErrors(diags) {
return err
}

return nil
Expand Down Expand Up @@ -131,13 +135,14 @@ func newEnvEditCmd(env *envCommand) *cobra.Command {
return err
}

if len(diags) == 0 {
if len(diags) != 0 {
err = edit.env.writeYAMLEnvironmentDiagnostics(edit.env.esc.stderr, ref.projectName+"/"+ref.envName, newYAML, diags)
contract.IgnoreError(err)
}
if !client.DiagnosticsHaveErrors(diags) {
return nil
}

err = edit.env.writeYAMLEnvironmentDiagnostics(edit.env.esc.stderr, ref.envName, newYAML, diags)
contract.IgnoreError(err)

fmt.Fprintln(edit.env.esc.stderr, "Press ENTER to continue editing or ^D to exit")

var b [1]byte
Expand Down
2 changes: 1 addition & 1 deletion cmd/esc/cli/env_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func newEnvInitCmd(env *envCommand) *cobra.Command {
return fmt.Errorf("updating environment definition: %w", err)
}
if len(diags) != 0 {
err = env.writeYAMLEnvironmentDiagnostics(env.esc.stderr, ref.envName, yaml, diags)
err = env.writeYAMLEnvironmentDiagnostics(env.esc.stderr, ref.projectName+"/"+ref.envName, yaml, diags)
contract.IgnoreError(err)

return fmt.Errorf("updating environment definition: too many errors")
Expand Down
2 changes: 1 addition & 1 deletion cmd/esc/cli/env_version_rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func newEnvVersionRollbackCmd(env *envCommand) *cobra.Command {
}

if len(diags) != 0 {
err = env.writeYAMLEnvironmentDiagnostics(env.esc.stderr, ref.envName, yaml, diags)
err = env.writeYAMLEnvironmentDiagnostics(env.esc.stderr, ref.projectName+"/"+ref.envName, yaml, diags)
contract.IgnoreError(err)
return errors.New("could not roll back: too many errors")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/esc/cli/testdata/env-edit-editor-invalid-draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ environments:
> esc env edit default/test --draft
Error: imports must be a list

on test line 1:
on default/test line 1:
1: imports:

Press ENTER to continue editing or ^D to exit
Expand Down
2 changes: 1 addition & 1 deletion cmd/esc/cli/testdata/env-edit-editor-invalid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ environments:
> esc env edit default/test
Error: imports must be a list

on test line 1:
on default/test line 1:
1: imports:

Press ENTER to continue editing or ^D to exit
Expand Down
36 changes: 36 additions & 0 deletions cmd/esc/cli/testdata/env-edit-file-warnings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
run: |
printf 'values:\n x: 1\nvalues:\n y: 2\n' | esc env edit default/test -f=-
esc env get default/test
environments:
test-user/default/test:
values:
foo: bar

---
> esc env edit default/test -f=-
Environment updated.
> esc env get default/test
# Value
```json
{
"y": 2
}
```
# Definition
```yaml
values:
x: 1
values:
y: 2

```


---
> esc env edit default/test -f=-
Warning: duplicate key "values"

on default/test line 3:
3: values:

> esc env get default/test
2 changes: 1 addition & 1 deletion cmd/esc/cli/testdata/env-init-diags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Environment created: test-user/default/test-stdin
> esc env init default/test-stdin -f=-
Error: unknown property "bar"

on test-stdin line 1:
on default/test-stdin line 1:
1: {"values":{"foo":"${bar}"}}

Error: updating environment definition: too many errors
2 changes: 1 addition & 1 deletion cmd/esc/cli/testdata/env-set-secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,4 @@ Error: value looks like a secret; rerun with --secret to mark it as such, or --p
> esc env set default/test password true --secret
> esc env get default/test
> esc env set default/test password [] --secret
test:3:21: secret values must be string literals
default/test:3:21: secret values must be string literals
2 changes: 1 addition & 1 deletion cmd/esc/cli/testdata/env-version-rollback-draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Change request URL: https://app.fake.pulumi.com/test-user/esc/default/test?versi
> esc env version rollback default/test@2 --draft
Error: not found

on test line 2:
on default/test line 2:
2: - c

Error: could not roll back: too many errors
2 changes: 1 addition & 1 deletion cmd/esc/cli/testdata/env-version-rollback.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ values:
> esc env version rollback default/test@2
Error: not found

on test line 2:
on default/test line 2:
2: - c

Error: could not roll back: too many errors
Loading