github.com/hashicorp/packer@v1.14.3/fix/fixer_createtime.go (about)

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