mirror of https://github.com/hashicorp/packer
commit
75e111d448
@ -0,0 +1,108 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
// FixerDockerTagtoTags renames tag to tags
|
||||
type FixerDockerTagtoTags struct{}
|
||||
|
||||
func (FixerDockerTagtoTags) Fix(input map[string]interface{}) (map[string]interface{}, error) {
|
||||
if input["post-processors"] == nil {
|
||||
return input, nil
|
||||
}
|
||||
|
||||
// Our template type we'll use for this fixer only
|
||||
type template struct {
|
||||
PP `mapstructure:",squash"`
|
||||
}
|
||||
|
||||
// Decode the input into our structure, if we can
|
||||
var tpl template
|
||||
if err := mapstructure.Decode(input, &tpl); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Go through each post-processor and get out all the complex configs
|
||||
pps := tpl.ppList()
|
||||
|
||||
for _, pp := range pps {
|
||||
ppTypeRaw, ok := pp["type"]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if ppType, ok := ppTypeRaw.(string); !ok {
|
||||
continue
|
||||
} else if ppType != "docker-tag" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Create a []string to hold tag and tags values
|
||||
allTags := []string{}
|
||||
|
||||
tagRaw, ok := pp["tag"]
|
||||
if ok {
|
||||
// Gather all "tag" into the []string
|
||||
switch t := tagRaw.(type) {
|
||||
case []interface{}:
|
||||
for _, tag := range t {
|
||||
allTags = append(allTags, tag.(string))
|
||||
}
|
||||
case []string:
|
||||
allTags = append(allTags, t...)
|
||||
case string:
|
||||
tList := strings.Split(t, ",")
|
||||
for _, tag := range tList {
|
||||
allTags = append(allTags, strings.TrimSpace(tag))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now check to see if they already have the "tags" field
|
||||
tagsRaw, ok := pp["tags"]
|
||||
if ok {
|
||||
// Gather all "tag" into the []string
|
||||
switch t := tagsRaw.(type) {
|
||||
case []interface{}:
|
||||
for _, tag := range t {
|
||||
allTags = append(allTags, tag.(string))
|
||||
}
|
||||
case []string:
|
||||
allTags = append(allTags, t...)
|
||||
case string:
|
||||
tList := strings.Split(t, ",")
|
||||
for _, tag := range tList {
|
||||
allTags = append(allTags, strings.TrimSpace(tag))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now deduplicate the tags in the list so we don't tag the same thing
|
||||
// multiple times.
|
||||
deduplicater := map[string]bool{}
|
||||
for _, tag := range allTags {
|
||||
if ok := deduplicater[tag]; !ok {
|
||||
deduplicater[tag] = true
|
||||
}
|
||||
}
|
||||
|
||||
finalTags := []string{}
|
||||
for k := range deduplicater {
|
||||
finalTags = append(finalTags, k)
|
||||
}
|
||||
|
||||
// Delete tag key, and set tags key to the final deduplicated list.
|
||||
delete(pp, "tag")
|
||||
pp["tags"] = finalTags
|
||||
}
|
||||
|
||||
input["post-processors"] = tpl.PostProcessors
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func (FixerDockerTagtoTags) Synopsis() string {
|
||||
return `Updates "docker" post-processor so any "tag" field is renamed to "tags".`
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFixerDockerTags(t *testing.T) {
|
||||
var _ Fixer = new(FixerVagrantPPOverride)
|
||||
}
|
||||
|
||||
func TestFixerDockerTags_Fix(t *testing.T) {
|
||||
var f FixerDockerTagtoTags
|
||||
|
||||
input := map[string]interface{}{
|
||||
"post-processors": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "docker-tag",
|
||||
"tag": "foo",
|
||||
"tags": []string{"foo", "bar"},
|
||||
},
|
||||
[]interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "docker-tag",
|
||||
"tag": []string{"baz"},
|
||||
"tags": []string{"foo", "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expected := map[string]interface{}{
|
||||
"post-processors": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "docker-tag",
|
||||
"tags": []string{"foo", "bar"},
|
||||
},
|
||||
[]interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "docker-tag",
|
||||
"tags": []string{"baz", "foo", "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
output, err := f.Fix(input)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equalf(t, expected, output, "should have removed tag from template")
|
||||
}
|
||||
Loading…
Reference in new issue