forked from cliffano/packer-post-processor-json-updater
/
post-processor.go
46 lines (35 loc) · 894 Bytes
/
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
package main
import (
"regexp"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
)
type Config struct {
AmiId map[string][]string `mapstructure:"ami_id"`
}
type PostProcessor struct {
config Config
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
_, err := common.DecodeConfig(&p.config, raws...)
if err != nil {
return err
}
if p.config.AmiId == nil {
p.config.AmiId = make(map[string][]string, 0)
}
return nil
}
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
if p.config.AmiId != nil {
r, _ := regexp.Compile("ami-[a-z0-9]+")
amiId := r.FindString(artifact.Id())
for file, properties := range p.config.AmiId {
err := UpdateJsonFile(file, properties, amiId, ui)
if err != nil {
return artifact, false, err
}
}
}
return artifact, true, nil
}