hcl2template: rename `Name`->`LocalName` for local

Local variables had an attribute called Name with the name of the local
variable.

However, when producing an error while walking the DAG of
local/datasources, if an error is encountered during validation, the raw
structure of the vertex was printed out, making the error message
produced hard to understand.

Therefore in order to clean it up, we rename the `Name` attribute for
Local variables as `LocalName`, and introduce a `Name()` function for
that block so that the complete name of the variable is clearly
reported.
pull/13197/head
Lucas Bajolet 1 year ago committed by Lucas Bajolet
parent 09774aaeb8
commit 4a4b837386

@ -362,7 +362,7 @@ func (cfg *PackerConfig) detectBuildPrereqDependencies() hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "failed to process local dependency",
Detail: fmt.Sprintf("An error occurred while processing a dependency for local variable %s: %s",
loc.Name, err),
loc.LocalName, err),
})
continue
}
@ -373,7 +373,7 @@ func (cfg *PackerConfig) detectBuildPrereqDependencies() hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "failed to register local dependency",
Detail: fmt.Sprintf("An error occurred while registering %q as a dependency for local variable %s: %s",
rs, loc.Name, err),
rs, loc.LocalName, err),
})
}
}
@ -394,7 +394,7 @@ func (cfg *PackerConfig) buildPrereqsDAG() (*dag.AcyclicGraph, error) {
}
for _, local := range cfg.LocalBlocks {
v := retGraph.Add(local)
verticesMap[fmt.Sprintf("local.%s", local.Name)] = v
verticesMap[fmt.Sprintf("local.%s", local.LocalName)] = v
}
// Connect the vertices together
@ -411,7 +411,7 @@ func (cfg *PackerConfig) buildPrereqsDAG() (*dag.AcyclicGraph, error) {
}
}
for _, loc := range cfg.LocalBlocks {
locName := fmt.Sprintf("local.%s", loc.Name)
locName := fmt.Sprintf("local.%s", loc.LocalName)
for _, dep := range loc.dependencies {
retGraph.Connect(

@ -207,8 +207,8 @@ func parseLocalVariableBlocks(f *hcl.File) ([]*LocalBlock, hcl.Diagnostics) {
diags = append(diags, moreDiags...)
for name, attr := range attrs {
locals = append(locals, &LocalBlock{
Name: name,
Expr: attr.Expr,
LocalName: name,
Expr: attr.Expr,
})
}
}
@ -219,7 +219,7 @@ func parseLocalVariableBlocks(f *hcl.File) ([]*LocalBlock, hcl.Diagnostics) {
func (c *PackerConfig) localByName(local string) (*LocalBlock, error) {
for _, loc := range c.LocalBlocks {
if loc.Name != local {
if loc.LocalName != local {
continue
}
@ -260,7 +260,7 @@ func (c *PackerConfig) evaluateLocalVariables(locals []*LocalBlock) hcl.Diagnost
Summary: "failed to extract dependency name from traversal ref",
Detail: fmt.Sprintf("while preparing for evaluation of local variable %q, "+
"a dependency was unable to be converted to a refString. "+
"This is likely a Packer bug, please consider reporting it.", local.Name),
"This is likely a Packer bug, please consider reporting it.", local.Name()),
})
continue
}
@ -290,19 +290,19 @@ func (c *PackerConfig) checkForDuplicateLocalDefinition() hcl.Diagnostics {
localNames := map[string]*LocalBlock{}
for _, block := range c.LocalBlocks {
loc, ok := localNames[block.Name]
loc, ok := localNames[block.LocalName]
if ok {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Duplicate local definition",
Detail: fmt.Sprintf("Local variable %q is defined twice in your templates. Other definition found at %q",
block.Name, loc.Expr.Range()),
block.LocalName, loc.Expr.Range()),
Subject: block.Expr.Range().Ptr(),
})
continue
}
localNames[block.Name] = block
localNames[block.LocalName] = block
}
return diags
@ -333,7 +333,7 @@ func (c *PackerConfig) recursivelyEvaluateLocalVariable(local *LocalBlock, depth
Severity: hcl.DiagError,
Summary: "failed to get local variable",
Detail: fmt.Sprintf("While evaluating %q, its dependency %q was not found, is it defined?",
local.Name, dep.String()),
local.Name(), dep.String()),
})
}
@ -355,8 +355,8 @@ func (cfg *PackerConfig) evaluateLocalVariable(local *LocalBlock) hcl.Diagnostic
if moreDiags.HasErrors() {
return diags
}
cfg.LocalVariables[local.Name] = &Variable{
Name: local.Name,
cfg.LocalVariables[local.LocalName] = &Variable{
Name: local.LocalName,
Sensitive: local.Sensitive,
Values: []VariableAssignment{{
Value: value,

@ -100,7 +100,7 @@ func (cfg *PackerConfig) getComponentByRef(rs refString) (interface{}, error) {
return nil, fmt.Errorf("failed to get datasource '%s.%s': component unknown", rs.Type, rs.Name)
case "local":
for _, loc := range cfg.LocalBlocks {
if loc.Name != rs.Name {
if loc.LocalName != rs.Name {
continue
}
return loc, nil

@ -25,8 +25,8 @@ const badIdentifierDetail = "A name must start with a letter or underscore and m
// The "locals" block itself is not represented, because it serves only to
// provide context for us to interpret its contents.
type LocalBlock struct {
Name string
Expr hcl.Expression
LocalName string
Expr hcl.Expression
// When Sensitive is set to true Packer will try its best to hide/obfuscate
// the variable from the output stream. By replacing the text.
Sensitive bool
@ -43,6 +43,10 @@ type LocalBlock struct {
evaluated bool
}
func (l LocalBlock) Name() string {
return fmt.Sprintf("local.%s", l.LocalName)
}
// VariableAssignment represents a way a variable was set: the expression
// setting it and the value of that expression. It helps pinpoint were
// something was set in diagnostics.
@ -303,7 +307,7 @@ func decodeLocalBlock(block *hcl.Block) (*LocalBlock, hcl.Diagnostics) {
}
l := &LocalBlock{
Name: name,
LocalName: name,
}
if attr, exists := content.Attributes["sensitive"]; exists {

Loading…
Cancel
Save