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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package fix
     5  
     6  import "github.com/mitchellh/mapstructure"
     7  
     8  // FixerVagrantPPOverride is a Fixer that replaces the provider-specific
     9  // overrides for the Vagrant post-processor with the new style introduced
    10  // as part of Packer 0.5.0.
    11  type FixerVagrantPPOverride struct{}
    12  
    13  func (FixerVagrantPPOverride) DeprecatedOptions() map[string][]string {
    14  	return map[string][]string{}
    15  }
    16  
    17  func (FixerVagrantPPOverride) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    18  	if input["post-processors"] == nil {
    19  		return input, nil
    20  	}
    21  
    22  	// Our template type we'll use for this fixer only
    23  	type template struct {
    24  		PP `mapstructure:",squash"`
    25  	}
    26  
    27  	// Decode the input into our structure, if we can
    28  	var tpl template
    29  	if err := mapstructure.Decode(input, &tpl); err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	pps := tpl.ppList()
    34  
    35  	// Go through each post-processor and make the fix if necessary
    36  	possible := []string{"aws", "digitalocean", "virtualbox", "vmware"}
    37  	for _, pp := range pps {
    38  		typeRaw, ok := pp["type"]
    39  		if !ok {
    40  			continue
    41  		}
    42  
    43  		if typeName, ok := typeRaw.(string); !ok {
    44  			continue
    45  		} else if typeName != "vagrant" {
    46  			continue
    47  		}
    48  
    49  		overrides := make(map[string]interface{})
    50  		for _, name := range possible {
    51  			if _, ok := pp[name]; !ok {
    52  				continue
    53  			}
    54  
    55  			overrides[name] = pp[name]
    56  			delete(pp, name)
    57  		}
    58  
    59  		if len(overrides) > 0 {
    60  			pp["override"] = overrides
    61  		}
    62  	}
    63  
    64  	input["post-processors"] = tpl.PostProcessors
    65  	return input, nil
    66  }
    67  
    68  func (FixerVagrantPPOverride) Synopsis() string {
    69  	return `Fixes provider-specific overrides for Vagrant post-processor`
    70  }