@ -8,6 +8,7 @@ import (
"sort"
"strings"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/mitchellh/colorstring"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
@ -320,6 +321,7 @@ func (p *blockBodyDiffPrinter) writeAttrsDiff(
blankBeforeBlocks := false
attrNames := make ( [ ] string , 0 , len ( attrsS ) )
displayAttrNames := make ( map [ string ] string , len ( attrsS ) )
attrNameLen := 0
for name := range attrsS {
oldVal := ctyGetAttrMaybeNull ( old , name )
@ -333,8 +335,9 @@ func (p *blockBodyDiffPrinter) writeAttrsDiff(
}
attrNames = append ( attrNames , name )
if len ( name ) > attrNameLen {
attrNameLen = len ( name )
displayAttrNames [ name ] = displayAttributeName ( name )
if len ( displayAttrNames [ name ] ) > attrNameLen {
attrNameLen = len ( displayAttrNames [ name ] )
}
}
sort . Strings ( attrNames )
@ -348,7 +351,7 @@ func (p *blockBodyDiffPrinter) writeAttrsDiff(
newVal := ctyGetAttrMaybeNull ( new , name )
result . bodyWritten = true
skipped := p . writeAttrDiff ( name, attrS , oldVal , newVal , attrNameLen , indent , path )
skipped := p . writeAttrDiff ( displayAttrNames[ name] , attrS , oldVal , newVal , attrNameLen , indent , path )
if skipped {
result . skippedAttributes ++
}
@ -1110,23 +1113,26 @@ func (p *blockBodyDiffPrinter) writeValue(val cty.Value, action plans.Action, in
atys := ty . AttributeTypes ( )
attrNames := make ( [ ] string , 0 , len ( atys ) )
displayAttrNames := make ( map [ string ] string , len ( atys ) )
nameLen := 0
for attrName := range atys {
attrNames = append ( attrNames , attrName )
if len ( attrName ) > nameLen {
nameLen = len ( attrName )
displayAttrNames [ attrName ] = displayAttributeName ( attrName )
if len ( displayAttrNames [ attrName ] ) > nameLen {
nameLen = len ( displayAttrNames [ attrName ] )
}
}
sort . Strings ( attrNames )
for _ , attrName := range attrNames {
val := val . GetAttr ( attrName )
displayAttrName := displayAttrNames [ attrName ]
p . buf . WriteString ( "\n" )
p . buf . WriteString ( strings . Repeat ( " " , indent + 2 ) )
p . writeActionSymbol ( action )
p . buf . WriteString ( attrName)
p . buf . WriteString ( strings . Repeat ( " " , nameLen - len ( attrName) ) )
p . buf . WriteString ( displ ayA ttrName)
p . buf . WriteString ( strings . Repeat ( " " , nameLen - len ( displ ayA ttrName) ) )
p . buf . WriteString ( " = " )
p . writeValue ( val , action , indent + 4 )
}
@ -1458,21 +1464,24 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
p . buf . WriteString ( "\n" )
var allKeys [ ] string
displayKeys := make ( map [ string ] string )
keyLen := 0
for it := old . ElementIterator ( ) ; it . Next ( ) ; {
k , _ := it . Element ( )
keyStr := k . AsString ( )
allKeys = append ( allKeys , keyStr )
if len ( keyStr ) > keyLen {
keyLen = len ( keyStr )
displayKeys [ keyStr ] = displayAttributeName ( keyStr )
if len ( displayKeys [ keyStr ] ) > keyLen {
keyLen = len ( displayKeys [ keyStr ] )
}
}
for it := new . ElementIterator ( ) ; it . Next ( ) ; {
k , _ := it . Element ( )
keyStr := k . AsString ( )
allKeys = append ( allKeys , keyStr )
if len ( keyStr ) > keyLen {
keyLen = len ( keyStr )
displayKeys [ keyStr ] = displayAttributeName ( keyStr )
if len ( displayKeys [ keyStr ] ) > keyLen {
keyLen = len ( displayKeys [ keyStr ] )
}
}
@ -1515,8 +1524,8 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
p . buf . WriteString ( strings . Repeat ( " " , indent + 2 ) )
p . writeActionSymbol ( action )
p . writeValue ( kV , action , indent + 4 )
p . buf . WriteString ( strings . Repeat ( " " , keyLen - len ( k) ) )
p . writeValue ( cty. StringVal ( displayKeys [ k ] ) , action , indent + 4 )
p . buf . WriteString ( strings . Repeat ( " " , keyLen - len ( displayKeys[ k] ) ) )
p . buf . WriteString ( " = " )
switch action {
case plans . Create , plans . NoOp :
@ -1563,21 +1572,24 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
forcesNewResource := p . pathForcesNewResource ( path )
var allKeys [ ] string
displayKeys := make ( map [ string ] string )
keyLen := 0
for it := old . ElementIterator ( ) ; it . Next ( ) ; {
k , _ := it . Element ( )
keyStr := k . AsString ( )
allKeys = append ( allKeys , keyStr )
if len ( keyStr ) > keyLen {
keyLen = len ( keyStr )
displayKeys [ keyStr ] = displayAttributeName ( keyStr )
if len ( displayKeys [ keyStr ] ) > keyLen {
keyLen = len ( displayKeys [ keyStr ] )
}
}
for it := new . ElementIterator ( ) ; it . Next ( ) ; {
k , _ := it . Element ( )
keyStr := k . AsString ( )
allKeys = append ( allKeys , keyStr )
if len ( keyStr ) > keyLen {
keyLen = len ( keyStr )
displayKeys [ keyStr ] = displayAttributeName ( keyStr )
if len ( displayKeys [ keyStr ] ) > keyLen {
keyLen = len ( displayKeys [ keyStr ] )
}
}
@ -1615,8 +1627,8 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
p . buf . WriteString ( strings . Repeat ( " " , indent + 2 ) )
p . writeActionSymbol ( action )
p . buf . WriteString ( k)
p . buf . WriteString ( strings . Repeat ( " " , keyLen - len ( k) ) )
p . buf . WriteString ( displayKeys[ k] )
p . buf . WriteString ( strings . Repeat ( " " , keyLen - len ( displayKeys[ k] ) ) )
p . buf . WriteString ( " = " )
switch action {
@ -2001,3 +2013,10 @@ func (p *blockBodyDiffPrinter) writeSkippedElems(skipped, indent int) {
p . buf . WriteString ( "\n" )
}
}
func displayAttributeName ( name string ) string {
if ! hclsyntax . ValidIdentifier ( name ) {
return fmt . Sprintf ( "%q" , name )
}
return name
}