github.com/hashicorp/packer@v1.14.3/fix/fixer_virtualbox_gaattach.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  // FixerVirtualBoxGAAttach changes the "guest_additions_attach" of a template
    11  // to "guest_additions_mode".
    12  type FixerVirtualBoxGAAttach struct{}
    13  
    14  func (FixerVirtualBoxGAAttach) DeprecatedOptions() map[string][]string {
    15  	return map[string][]string{
    16  		"mitchellh.virtualbox": []string{"guest_additions_attach"},
    17  	}
    18  }
    19  
    20  func (FixerVirtualBoxGAAttach) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    21  	// The type we'll decode into; we only care about builders
    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  	for _, builder := range tpl.Builders {
    33  		builderTypeRaw, ok := builder["type"]
    34  		if !ok {
    35  			continue
    36  		}
    37  
    38  		builderType, ok := builderTypeRaw.(string)
    39  		if !ok {
    40  			continue
    41  		}
    42  
    43  		if builderType != "virtualbox" {
    44  			continue
    45  		}
    46  
    47  		gaAttachRaw, ok := builder["guest_additions_attach"]
    48  		if !ok {
    49  			continue
    50  		}
    51  
    52  		gaAttach, ok := gaAttachRaw.(bool)
    53  		if !ok {
    54  			continue
    55  		}
    56  
    57  		gaMode := "upload"
    58  		if gaAttach {
    59  			gaMode = "attach"
    60  		}
    61  
    62  		delete(builder, "guest_additions_attach")
    63  		builder["guest_additions_mode"] = gaMode
    64  	}
    65  
    66  	input["builders"] = tpl.Builders
    67  	return input, nil
    68  }
    69  
    70  func (FixerVirtualBoxGAAttach) Synopsis() string {
    71  	return `Updates VirtualBox builders using "guest_additions_attach" to use "guest_additions_mode"`
    72  }