forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.source.go
133 lines (115 loc) · 3.53 KB
/
types.source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package hcl2template
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/packer/packer"
)
// A source field in an HCL file will load into the Source type.
//
type Source struct {
// Type of source; ex: virtualbox-iso
Type string
// Given name; if any
Name string
block *hcl.Block
}
func (p *Parser) decodeSource(block *hcl.Block) (*Source, hcl.Diagnostics) {
source := &Source{
Type: block.Labels[0],
Name: block.Labels[1],
block: block,
}
var diags hcl.Diagnostics
if !p.BuilderSchemas.Has(source.Type) {
diags = append(diags, &hcl.Diagnostic{
Summary: "Unknown " + buildSourceLabel + " type " + source.Type,
Subject: block.LabelRanges[0].Ptr(),
Detail: fmt.Sprintf("known builders: %v", p.BuilderSchemas.List()),
Severity: hcl.DiagError,
})
return nil, diags
}
return source, diags
}
func (p *Parser) StartBuilder(source *Source) (packer.Builder, hcl.Diagnostics, []string) {
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
}
decoded, moreDiags := decodeHCL2Spec(source.block, nil, builder)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
return nil, diags, nil
}
generatedVars, warning, err := builder.Prepare(decoded)
moreDiags = warningErrorsToDiags(source.block, warning, err)
diags = append(diags, moreDiags...)
return builder, diags, generatedVars
}
func (source *Source) Ref() SourceRef {
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 sourceRefFromAbsTraversal(t hcl.Traversal) (SourceRef, hcl.Diagnostics) {
var diags hcl.Diagnostics
if len(t) != 3 {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid " + sourceLabel + " reference",
Detail: "A " + sourceLabel + " reference must have three parts separated by periods: the keyword \"" + sourceLabel + "\", the builder type name, and the source name.",
Subject: t.SourceRange().Ptr(),
})
return NoSource, diags
}
if t.RootName() != sourceLabel {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid " + sourceLabel + " reference",
Detail: "The first part of an source reference must be the keyword \"" + sourceLabel + "\".",
Subject: t[0].SourceRange().Ptr(),
})
return NoSource, diags
}
btStep, ok := t[1].(hcl.TraverseAttr)
if !ok {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid " + sourceLabel + " reference",
Detail: "The second part of an " + sourceLabel + " reference must be an identifier giving the builder type of the " + sourceLabel + ".",
Subject: t[1].SourceRange().Ptr(),
})
return NoSource, diags
}
nameStep, ok := t[2].(hcl.TraverseAttr)
if !ok {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid " + sourceLabel + " reference",
Detail: "The third part of an " + sourceLabel + " reference must be an identifier giving the name of the " + sourceLabel + ".",
Subject: t[2].SourceRange().Ptr(),
})
return NoSource, diags
}
return SourceRef{
Type: btStep.Name,
Name: nameStep.Name,
}, diags
}
func (r SourceRef) String() string {
return fmt.Sprintf("%s.%s", r.Type, r.Name)
}