-
Notifications
You must be signed in to change notification settings - Fork 13
/
post-processor.go
73 lines (59 loc) · 1.82 KB
/
post-processor.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
//go:generate mapstructure-to-hcl2 -type PostProcessorConfig
package main
import (
"context"
"fmt"
"github.com/common-nighthawk/go-figure"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
type PostProcessorConfig struct {
Comment string `mapstructure:"comment"`
SendToUi bool `mapstructure:"ui"`
Bubble bool `mapstructure:"bubble_text"`
PackerSay bool `mapstructure:"packer_say"`
ctx interpolate.Context
}
type PostProcessor struct {
config PostProcessorConfig
}
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec {
return p.config.FlatMapstructure().HCL2Spec()
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.config.ctx,
}, raws...)
if err != nil {
return err
}
if p.config.PackerSay && p.config.Bubble {
return fmt.Errorf("Can't have both packer_say and bubble_text options set.")
}
return nil
}
func (p *PostProcessor) PostProcess(_ context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
comment, err := interpolate.Render(p.config.Comment, &p.config.ctx)
if err != nil {
return artifact, true, false, fmt.Errorf("Error interpolating comment: %s", err)
}
if p.config.SendToUi {
if p.config.Bubble {
myFigure := figure.NewFigure(comment, "", false)
ui.Say(myFigure.String())
} else if p.config.PackerSay {
// CreatePackerFriend is defined in happy_packy.go
packyText, err := CreatePackerFriend(comment)
if err != nil {
return artifact, true, false, err
}
ui.Say(packyText)
} else {
ui.Say(comment)
}
}
return artifact, true, false, nil
}