command/format: Show resource diff with header "comment"

This extra comment line gives us a place to show the full resource address
(since the block header line only includes type and name) and also allows
us to explain in long form the meaning of the change icon on the following
line.
This commit is contained in:
Martin Atkins 2018-08-29 17:25:13 -07:00
parent 239a54ad6f
commit a2f4b8b058
1 changed files with 30 additions and 20 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/plans" "github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/plans/objchange" "github.com/hashicorp/terraform/plans/objchange"
"github.com/hashicorp/terraform/states"
) )
// ResourceChange returns a string representation of a change to a particular // ResourceChange returns a string representation of a change to a particular
@ -41,7 +42,27 @@ func ResourceChange(
} }
} }
buf.WriteString(color.Color("[reset]")) dispAddr := addr.String()
if change.DeposedKey != states.NotDeposed {
dispAddr = fmt.Sprintf("%s (deposed object %s)", dispAddr, change.DeposedKey)
}
switch change.Action {
case plans.Create:
buf.WriteString(color.Color(fmt.Sprintf("[bold] # %s[reset] will be created", dispAddr)))
case plans.Read:
buf.WriteString(color.Color(fmt.Sprintf("[bold] # %s[reset] will be read during apply\n # (config refers to values not yet known)", dispAddr)))
case plans.Update:
buf.WriteString(color.Color(fmt.Sprintf("[bold] # %s[reset] will be updated in-place", dispAddr)))
case plans.Replace:
buf.WriteString(color.Color(fmt.Sprintf("[bold] # %s[reset] must be [bold][red]replaced", dispAddr)))
case plans.Delete:
buf.WriteString(color.Color(fmt.Sprintf("[bold] # %s[reset] will be [bold][red]destroyed", dispAddr)))
default:
// should never happen, since the above is exhaustive
buf.WriteString(fmt.Sprintf("%s has an action the plan renderer doesn't support (this is a bug)", dispAddr))
}
buf.WriteString(color.Color("[reset]\n"))
switch change.Action { switch change.Action {
case plans.Create: case plans.Create:
@ -55,39 +76,28 @@ func ResourceChange(
case plans.Delete: case plans.Delete:
buf.WriteString(color.Color("[red] -[reset] ")) buf.WriteString(color.Color("[red] -[reset] "))
default: default:
// should never happen, since the above is exhaustive
buf.WriteString(color.Color("??? ")) buf.WriteString(color.Color("??? "))
} }
switch addr.Resource.Resource.Mode { switch addr.Resource.Resource.Mode {
case addrs.ManagedResourceMode: case addrs.ManagedResourceMode:
buf.WriteString(color.Color(fmt.Sprintf( buf.WriteString(fmt.Sprintf(
"resource [bold]%q[reset] [bold]%q[reset]", "resource %q %q",
addr.Resource.Resource.Type, addr.Resource.Resource.Type,
addr.Resource.Resource.Name, addr.Resource.Resource.Name,
))) ))
if addr.Resource.Key != addrs.NoKey {
buf.WriteString(fmt.Sprintf(" %s", addr.Resource.Key))
}
case addrs.DataResourceMode: case addrs.DataResourceMode:
buf.WriteString(color.Color(fmt.Sprintf( buf.WriteString(fmt.Sprintf(
"data [bold]%q[reset] [bold]%q[reset] ", "data %q %q ",
addr.Resource.Resource.Type, addr.Resource.Resource.Type,
addr.Resource.Resource.Name, addr.Resource.Resource.Name,
))) ))
if addr.Resource.Key != addrs.NoKey {
buf.WriteString(fmt.Sprintf(" %s", addr.Resource.Key))
}
default: default:
// should never happen, since the above is exhaustive // should never happen, since the above is exhaustive
buf.WriteString(addr.String()) buf.WriteString(addr.String())
} }
buf.WriteString(" {") buf.WriteString(" {\n")
if change.Action == plans.Replace {
buf.WriteString(color.Color(" [bold][red]# new resource required[reset]"))
}
buf.WriteString("\n")
p := blockBodyDiffPrinter{ p := blockBodyDiffPrinter{
buf: &buf, buf: &buf,
@ -128,7 +138,7 @@ type blockBodyDiffPrinter struct {
requiredReplace cty.PathSet requiredReplace cty.PathSet
} }
const forcesNewResourceCaption = " [red]# (forces new resource)[reset]" const forcesNewResourceCaption = " [red]# forces replacement[reset]"
func (p *blockBodyDiffPrinter) writeBlockBodyDiff(schema *configschema.Block, old, new cty.Value, indent int, path cty.Path) { func (p *blockBodyDiffPrinter) writeBlockBodyDiff(schema *configschema.Block, old, new cty.Value, indent int, path cty.Path) {
path = ctyEnsurePathCapacity(path, 1) path = ctyEnsurePathCapacity(path, 1)