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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package fix
     5  
     6  import (
     7  	"github.com/mitchellh/mapstructure"
     8  )
     9  
    10  // FixerVirtualBoxRename changes "virtualbox" builders to "virtualbox-iso"
    11  type FixerVirtualBoxRename struct{}
    12  
    13  func (FixerVirtualBoxRename) DeprecatedOptions() map[string][]string {
    14  	return map[string][]string{}
    15  }
    16  
    17  func (FixerVirtualBoxRename) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    18  	type template struct {
    19  		Builders     []map[string]interface{}
    20  		Provisioners []interface{}
    21  	}
    22  
    23  	// Decode the input into our structure, if we can
    24  	var tpl template
    25  	if err := mapstructure.WeakDecode(input, &tpl); err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	for _, builder := range tpl.Builders {
    30  		builderTypeRaw, ok := builder["type"]
    31  		if !ok {
    32  			continue
    33  		}
    34  
    35  		builderType, ok := builderTypeRaw.(string)
    36  		if !ok {
    37  			continue
    38  		}
    39  
    40  		if builderType != "virtualbox" {
    41  			continue
    42  		}
    43  
    44  		builder["type"] = "virtualbox-iso"
    45  	}
    46  
    47  	for i, raw := range tpl.Provisioners {
    48  		var m map[string]interface{}
    49  		if err := mapstructure.WeakDecode(raw, &m); err != nil {
    50  			// Ignore errors, could be a non-map
    51  			continue
    52  		}
    53  
    54  		raw, ok := m["override"]
    55  		if !ok {
    56  			continue
    57  		}
    58  
    59  		var override map[string]interface{}
    60  		if err := mapstructure.WeakDecode(raw, &override); err != nil {
    61  			return nil, err
    62  		}
    63  
    64  		if raw, ok := override["virtualbox"]; ok {
    65  			override["virtualbox-iso"] = raw
    66  			delete(override, "virtualbox")
    67  
    68  			// Set the change
    69  			m["override"] = override
    70  			tpl.Provisioners[i] = m
    71  		}
    72  	}
    73  
    74  	if len(tpl.Builders) > 0 {
    75  		input["builders"] = tpl.Builders
    76  	}
    77  	if len(tpl.Provisioners) > 0 {
    78  		input["provisioners"] = tpl.Provisioners
    79  	}
    80  	return input, nil
    81  }
    82  
    83  func (FixerVirtualBoxRename) Synopsis() string {
    84  	return `Updates "virtualbox" builders to "virtualbox-iso"`
    85  }