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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package fix
     5  
     6  import (
     7  	"strconv"
     8  
     9  	"github.com/mitchellh/mapstructure"
    10  )
    11  
    12  // FixerQEMUDiskSize updates disk_size from a string to int for QEMU builders
    13  type FixerQEMUDiskSize struct{}
    14  
    15  func (FixerQEMUDiskSize) DeprecatedOptions() map[string][]string {
    16  	return map[string][]string{}
    17  }
    18  
    19  func (FixerQEMUDiskSize) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    20  	type template struct {
    21  		Builders []map[string]interface{}
    22  	}
    23  
    24  	// Decode the input into our structure, if we can
    25  	var tpl template
    26  	if err := mapstructure.Decode(input, &tpl); err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	for _, builder := range tpl.Builders {
    31  		builderTypeRaw, ok := builder["type"]
    32  		if !ok {
    33  			continue
    34  		}
    35  
    36  		builderType, ok := builderTypeRaw.(string)
    37  		if !ok {
    38  			continue
    39  		}
    40  
    41  		if builderType != "qemu" {
    42  			continue
    43  		}
    44  
    45  		switch diskSize := builder["disk_size"].(type) {
    46  		case float64:
    47  			builder["disk_size"] = strconv.Itoa(int(diskSize)) + "M"
    48  		case int:
    49  			builder["disk_size"] = strconv.Itoa(diskSize) + "M"
    50  		}
    51  
    52  	}
    53  
    54  	input["builders"] = tpl.Builders
    55  	return input, nil
    56  }
    57  
    58  func (FixerQEMUDiskSize) Synopsis() string {
    59  	return `Updates "disk_size" from int to string in QEMU builders.`
    60  }