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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package fix
     5  
     6  import (
     7  	"fmt"
     8  	"regexp"
     9  
    10  	"github.com/mitchellh/mapstructure"
    11  )
    12  
    13  // FixerCleanImageName is a Fixer that replaces the "clean_(image|ami)_name" template
    14  // calls with "clean_resource_name"
    15  type FixerCleanImageName struct{}
    16  
    17  func (FixerCleanImageName) DeprecatedOptions() map[string][]string {
    18  	return map[string][]string{
    19  		"*amazon*":             []string{"clean_ami_name"},
    20  		"packer.googlecompute": []string{"clean_image_name"},
    21  		"Azure*":               []string{"clean_image_name"},
    22  	}
    23  }
    24  
    25  func (FixerCleanImageName) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    26  	// Our template type we'll use for this fixer only
    27  	type template struct {
    28  		Builders []map[string]interface{}
    29  	}
    30  
    31  	// Decode the input into our structure, if we can
    32  	var tpl template
    33  	if err := mapstructure.Decode(input, &tpl); err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	re := regexp.MustCompile(`clean_(image|ami)_name`)
    38  
    39  	// Go through each builder and replace CreateTime if we can
    40  	for _, builder := range tpl.Builders {
    41  		for key, value := range builder {
    42  			switch v := value.(type) {
    43  			case string:
    44  				changed := re.ReplaceAllString(v, "clean_resource_name")
    45  				builder[key] = changed
    46  			case map[string]string:
    47  				for k := range v {
    48  					v[k] = re.ReplaceAllString(v[k], "clean_resource_name")
    49  				}
    50  				builder[key] = v
    51  			case map[string]interface{}:
    52  				for k := range v {
    53  					if s, ok := v[k].(string); ok {
    54  						v[k] = re.ReplaceAllString(s, "clean_resource_name")
    55  					}
    56  				}
    57  				builder[key] = v
    58  			default:
    59  				if key == "image_labels" {
    60  
    61  					panic(fmt.Sprintf("value: %#v", value))
    62  				}
    63  			}
    64  		}
    65  	}
    66  
    67  	input["builders"] = tpl.Builders
    68  	return input, nil
    69  }
    70  
    71  func (FixerCleanImageName) Synopsis() string {
    72  	return `Replaces /clean_(image|ami)_name/ in builder configs with "clean_resource_name"`
    73  }