Files
Packer-Cn/hcl2template/types.source.go
T

92 lines
2.1 KiB
Go
Raw Normal View History

2019-10-14 17:02:53 +02:00
package hcl2template
import (
"fmt"
"github.com/hashicorp/hcl/v2"
2019-12-17 11:25:56 +01:00
"github.com/hashicorp/packer/packer"
2019-10-14 17:02:53 +02:00
)
// SourceBlock references an HCL 'source' block.
type SourceBlock struct {
2019-10-14 17:02:53 +02:00
// Type of source; ex: virtualbox-iso
Type string
// Given name; if any
Name string
2019-12-17 11:25:56 +01:00
block *hcl.Block
2019-10-14 17:02:53 +02:00
}
func (p *Parser) decodeSource(block *hcl.Block) (*SourceBlock, hcl.Diagnostics) {
source := &SourceBlock{
2019-12-17 11:25:56 +01:00
Type: block.Labels[0],
Name: block.Labels[1],
block: block,
2019-10-14 17:02:53 +02:00
}
var diags hcl.Diagnostics
2019-12-17 11:25:56 +01:00
if !p.BuilderSchemas.Has(source.Type) {
2019-10-14 17:02:53 +02:00
diags = append(diags, &hcl.Diagnostic{
2019-12-17 11:25:56 +01:00
Summary: "Unknown " + buildSourceLabel + " type " + source.Type,
Subject: block.LabelRanges[0].Ptr(),
Detail: fmt.Sprintf("known builders: %v", p.BuilderSchemas.List()),
Severity: hcl.DiagError,
2019-10-14 17:02:53 +02:00
})
2019-12-17 11:25:56 +01:00
return nil, diags
2019-10-14 17:02:53 +02:00
}
return source, diags
}
func (p *Parser) startBuilder(source *SourceBlock, ectx *hcl.EvalContext) (packer.Builder, hcl.Diagnostics, []string) {
2019-12-17 11:25:56 +01:00
var diags hcl.Diagnostics
builder, err := p.BuilderSchemas.Start(source.Type)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Summary: "Failed to load " + sourceLabel + " type",
Detail: err.Error(),
Subject: &source.block.LabelRanges[0],
})
return builder, diags, nil
2019-12-17 11:25:56 +01:00
}
decoded, moreDiags := decodeHCL2Spec(source.block.Body, ectx, builder)
2019-12-17 11:25:56 +01:00
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
return nil, diags, nil
2019-12-17 11:25:56 +01:00
}
2019-12-17 16:31:22 +01:00
2020-04-09 11:14:37 +02:00
generatedVars, warning, err := builder.Prepare(source.builderVariables(), decoded)
2019-12-17 11:25:56 +01:00
moreDiags = warningErrorsToDiags(source.block, warning, err)
diags = append(diags, moreDiags...)
return builder, diags, generatedVars
2019-12-17 11:25:56 +01:00
}
2020-04-09 11:14:37 +02:00
func (source *SourceBlock) builderVariables() map[string]string {
return map[string]string{
"packer_build_name": source.Name,
"packer_builder_type": source.Type,
}
}
func (source *SourceBlock) Ref() SourceRef {
2019-10-14 17:02:53 +02:00
return SourceRef{
Type: source.Type,
Name: source.Name,
}
}
type SourceRef struct {
Type string
Name string
}
// NoSource is the zero value of sourceRef, representing the absense of an
// source.
var NoSource SourceRef
func (r SourceRef) String() string {
return fmt.Sprintf("%s.%s", r.Type, r.Name)
}