added enum for actions

This commit is contained in:
Shreyas Goenka 2023-04-19 15:52:10 +02:00
parent 24506357dc
commit e0369512b4
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 41 additions and 35 deletions

View File

@ -16,8 +16,6 @@ func (m *delete) Name() string {
return "files.Delete" 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) { func (m *delete) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) {
// Do not delete files if terraform destroy was not consented // Do not delete files if terraform destroy was not consented
if !b.Plan.IsEmpty && !b.Plan.ConfirmApply { if !b.Plan.IsEmpty && !b.Plan.ConfirmApply {

View File

@ -35,15 +35,6 @@ func (m *showPlan) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutato
// compute bundle specific change events // compute bundle specific change events
changeEvents := make([]*ResourceChangeEvent, 0) changeEvents := make([]*ResourceChangeEvent, 0)
for _, change := range plan.ResourceChanges { 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) event := toResourceChangeEvent(change)
if event == nil { if event == nil {
continue continue
@ -59,6 +50,12 @@ func (m *showPlan) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutato
// log resource changes // log resource changes
cmdio.LogString(ctx, "The following resource changes will be applied:") cmdio.LogString(ctx, "The following resource changes will be applied:")
for _, event := range changeEvents { 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.Log(ctx, event)
} }
cmdio.LogNewline(ctx) cmdio.LogNewline(ctx)

View File

@ -12,36 +12,30 @@ import (
type ResourceChangeEvent struct { type ResourceChangeEvent struct {
Name string `json:"name"` Name string `json:"name"`
ResourceType string `json:"resource_type"` ResourceType string `json:"resource_type"`
Action string `json:"action"` Action Action `json:"action"`
} }
func toAction(actions tfjson.Actions) string { type Action string
action := "no-op"
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 { switch {
case actions.Create(): case actions.Create():
action = "create" action = ActionCreate
case actions.Read():
action = "read"
case actions.Update(): case actions.Update():
action = "update" action = ActionUpdate
case actions.Delete(): case actions.Delete():
action = "delete" action = ActionDelete
case actions.Replace(): case actions.Replace():
action = "replace" action = ActionReplace
}
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 action return action
} }
@ -89,7 +83,24 @@ func toResourceChangeEvent(change *tfjson.ResourceChange) *ResourceChangeEvent {
} }
func (event *ResourceChangeEvent) String() string { 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 { func (event *ResourceChangeEvent) IsInplaceSupported() bool {