github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonformat/computed/renderers/util.go (about)

     1  package renderers
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/internal/command/format"
     8  
     9  	"github.com/hashicorp/hcl/v2/hclsyntax"
    10  
    11  	"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
    12  	"github.com/hashicorp/terraform/internal/plans"
    13  )
    14  
    15  // NoWarningsRenderer defines a Warnings function that returns an empty list of
    16  // warnings. This can be used by other renderers to ensure we don't see lots of
    17  // repeats of this empty function.
    18  type NoWarningsRenderer struct{}
    19  
    20  // WarningsHuman returns an empty slice, as the name NoWarningsRenderer suggests.
    21  func (render NoWarningsRenderer) WarningsHuman(_ computed.Diff, _ int, _ computed.RenderHumanOpts) []string {
    22  	return nil
    23  }
    24  
    25  // nullSuffix returns the `-> null` suffix if the change is a delete action, and
    26  // it has not been overridden.
    27  func nullSuffix(action plans.Action, opts computed.RenderHumanOpts) string {
    28  	if !opts.OverrideNullSuffix && action == plans.Delete {
    29  		return opts.Colorize.Color(" [dark_gray]-> null[reset]")
    30  	}
    31  	return ""
    32  }
    33  
    34  // forcesReplacement returns the `# forces replacement` suffix if this change is
    35  // driving the entire resource to be replaced.
    36  func forcesReplacement(replace bool, opts computed.RenderHumanOpts) string {
    37  	if replace || opts.OverrideForcesReplacement {
    38  		return opts.Colorize.Color(" [red]# forces replacement[reset]")
    39  	}
    40  	return ""
    41  }
    42  
    43  // indent returns whitespace that is the required length for the specified
    44  // indent.
    45  func formatIndent(indent int) string {
    46  	return strings.Repeat("    ", indent)
    47  }
    48  
    49  // unchanged prints out a description saying how many of 'keyword' have been
    50  // hidden because they are unchanged or noop actions.
    51  func unchanged(keyword string, count int, opts computed.RenderHumanOpts) string {
    52  	if count == 1 {
    53  		return opts.Colorize.Color(fmt.Sprintf("[dark_gray]# (%d unchanged %s hidden)[reset]", count, keyword))
    54  	}
    55  	return opts.Colorize.Color(fmt.Sprintf("[dark_gray]# (%d unchanged %ss hidden)[reset]", count, keyword))
    56  }
    57  
    58  // EnsureValidAttributeName checks if `name` contains any HCL syntax and calls
    59  // and returns hclEscapeString.
    60  func EnsureValidAttributeName(name string) string {
    61  	if !hclsyntax.ValidIdentifier(name) {
    62  		return hclEscapeString(name)
    63  	}
    64  	return name
    65  }
    66  
    67  // hclEscapeString formats the input string into a format that is safe for
    68  // rendering within HCL.
    69  //
    70  // Note, this function doesn't actually do a very good job of this currently. We
    71  // need to expose some internal functions from HCL in a future version and call
    72  // them from here. For now, just use "%q" formatting.
    73  func hclEscapeString(str string) string {
    74  	// TODO: Replace this with more complete HCL logic instead of the simple
    75  	// go workaround.
    76  	return fmt.Sprintf("%q", str)
    77  }
    78  
    79  // writeDiffActionSymbol writes out the symbols for the associated action, and
    80  // handles localized colorization of the symbol as well as indenting the symbol
    81  // to be 4 spaces wide.
    82  //
    83  // If the opts has HideDiffActionSymbols set then this function returns an empty
    84  // string.
    85  func writeDiffActionSymbol(action plans.Action, opts computed.RenderHumanOpts) string {
    86  	if opts.HideDiffActionSymbols {
    87  		return ""
    88  	}
    89  	return fmt.Sprintf("%s ", opts.Colorize.Color(format.DiffActionSymbol(action)))
    90  }