github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/fix/fixer_createtime.go (about)

     1  package fix
     2  
     3  import (
     4  	"github.com/mitchellh/mapstructure"
     5  	"regexp"
     6  )
     7  
     8  // FixerCreateTime is a Fixer that replaces the ".CreateTime" template
     9  // calls with "{{timestamp}"
    10  type FixerCreateTime struct{}
    11  
    12  func (FixerCreateTime) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    13  	// Our template type we'll use for this fixer only
    14  	type template struct {
    15  		Builders []map[string]interface{}
    16  	}
    17  
    18  	// Decode the input into our structure, if we can
    19  	var tpl template
    20  	if err := mapstructure.Decode(input, &tpl); err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	badKeys := []string{
    25  		"ami_name",
    26  		"bundle_prefix",
    27  		"snapshot_name",
    28  	}
    29  
    30  	re := regexp.MustCompile(`{{\s*\.CreateTime\s*}}`)
    31  
    32  	// Go through each builder and replace CreateTime if we can
    33  	for _, builder := range tpl.Builders {
    34  		for _, key := range badKeys {
    35  			raw, ok := builder[key]
    36  			if !ok {
    37  				continue
    38  			}
    39  
    40  			v, ok := raw.(string)
    41  			if !ok {
    42  				continue
    43  			}
    44  
    45  			builder[key] = re.ReplaceAllString(v, "{{timestamp}}")
    46  		}
    47  	}
    48  
    49  	input["builders"] = tpl.Builders
    50  	return input, nil
    51  }
    52  
    53  func (FixerCreateTime) Synopsis() string {
    54  	return `Replaces ".CreateTime" in builder configs with "{{timestamp}}"`
    55  }