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"
}
// 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 {

View File

@ -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)

View File

@ -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 {