diff --git a/bundle/deploy/files/delete.go b/bundle/deploy/files/delete.go index 3af255210..5774c73aa 100644 --- a/bundle/deploy/files/delete.go +++ b/bundle/deploy/files/delete.go @@ -16,8 +16,6 @@ func (m *delete) Name() string { return "files.Delete" } -// TODO: autoapprove and tty detection for destroy. Don't allow destroy without auto-approve otherwise. Note this is a breaking change - func (m *delete) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) { // Do not delete files if terraform destroy was not consented if !b.Plan.IsEmpty && !b.Plan.ConfirmApply { diff --git a/bundle/deploy/terraform/show.go b/bundle/deploy/terraform/show.go index bb3e887d6..3eb402587 100644 --- a/bundle/deploy/terraform/show.go +++ b/bundle/deploy/terraform/show.go @@ -35,15 +35,6 @@ func (m *showPlan) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutato // compute bundle specific change events changeEvents := make([]*ResourceChangeEvent, 0) for _, change := range plan.ResourceChanges { - if change.Change == nil { - continue - } - if change.Change.Actions.Replace() { - b.Plan.IsReplacingResource = true - } - if change.Change.Actions.Delete() { - b.Plan.IsDeletingResource = true - } event := toResourceChangeEvent(change) if event == nil { continue @@ -59,6 +50,12 @@ func (m *showPlan) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutato // log resource changes cmdio.LogString(ctx, "The following resource changes will be applied:") for _, event := range changeEvents { + if event.Action == ActionDelete { + b.Plan.IsDeletingResource = true + } + if event.Action == ActionReplace { + b.Plan.IsReplacingResource = true + } cmdio.Log(ctx, event) } cmdio.LogNewline(ctx) diff --git a/bundle/deploy/terraform/show_events.go b/bundle/deploy/terraform/show_events.go index 9d8f2f5d8..2fdd43b9a 100644 --- a/bundle/deploy/terraform/show_events.go +++ b/bundle/deploy/terraform/show_events.go @@ -12,36 +12,30 @@ import ( type ResourceChangeEvent struct { Name string `json:"name"` ResourceType string `json:"resource_type"` - Action string `json:"action"` + Action Action `json:"action"` } -func toAction(actions tfjson.Actions) string { - action := "no-op" +type Action string + +const ( + ActionCreate = Action("create") + ActionUpdate = Action("update") + ActionDelete = Action("delete") + ActionReplace = Action("replace") + ActionNoop = Action("no-op") +) + +func toAction(actions tfjson.Actions) Action { + action := ActionNoop switch { case actions.Create(): - action = "create" - case actions.Read(): - action = "read" + action = ActionCreate case actions.Update(): - action = "update" + action = ActionUpdate case actions.Delete(): - action = "delete" + action = ActionDelete case actions.Replace(): - action = "replace" - } - - red := color.New(color.FgRed).SprintFunc() - green := color.New(color.FgGreen).SprintFunc() - yellow := color.New(color.FgYellow).SprintFunc() - isTty := term.IsTerminal(int(os.Stderr.Fd())) - if isTty && action == "create" { - action = green(action) - } - if isTty && action == "delete" { - action = red(action) - } - if isTty && action == "replace" { - action = yellow(action) + action = ActionReplace } return action } @@ -89,7 +83,24 @@ func toResourceChangeEvent(change *tfjson.ResourceChange) *ResourceChangeEvent { } func (event *ResourceChangeEvent) String() string { - return strings.Join([]string{" ", string(event.Action), event.ResourceType, event.Name}, " ") + action := string(event.Action) + + // color create, replace and delete events + red := color.New(color.FgRed).SprintFunc() + green := color.New(color.FgGreen).SprintFunc() + yellow := color.New(color.FgYellow).SprintFunc() + isTty := term.IsTerminal(int(os.Stderr.Fd())) + if isTty && action == "create" { + action = green(action) + } + if isTty && action == "delete" { + action = red(action) + } + if isTty && action == "replace" { + action = yellow(action) + } + + return strings.Join([]string{" ", action, event.ResourceType, event.Name}, " ") } func (event *ResourceChangeEvent) IsInplaceSupported() bool {