github.com/hashicorp/packer@v1.14.3/fix/fixer_pp_manifest_filename.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  // FixerManifestFilename renames any Filename to Output
    11  type FixerManifestFilename struct{}
    12  
    13  func (FixerManifestFilename) DeprecatedOptions() map[string][]string {
    14  	return map[string][]string{
    15  		"packer.post-processor.manifest": []string{"filename"},
    16  	}
    17  }
    18  
    19  func (FixerManifestFilename) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    20  	if input["post-processors"] == nil {
    21  		return input, nil
    22  	}
    23  
    24  	// Our template type we'll use for this fixer only
    25  	type template struct {
    26  		PP `mapstructure:",squash"`
    27  	}
    28  
    29  	// Decode the input into our structure, if we can
    30  	var tpl template
    31  	if err := mapstructure.Decode(input, &tpl); err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	// Go through each post-processor and get out all the complex configs
    36  	pps := tpl.ppList()
    37  
    38  	for _, pp := range pps {
    39  		ppTypeRaw, ok := pp["type"]
    40  		if !ok {
    41  			continue
    42  		}
    43  
    44  		if ppType, ok := ppTypeRaw.(string); !ok {
    45  			continue
    46  		} else if ppType != "manifest" {
    47  			continue
    48  		}
    49  
    50  		filenameRaw, ok := pp["filename"]
    51  		if !ok {
    52  			continue
    53  		}
    54  
    55  		if filename, ok := filenameRaw.(string); ok {
    56  			delete(pp, "filename")
    57  			pp["output"] = filename
    58  		}
    59  
    60  	}
    61  
    62  	input["post-processors"] = tpl.PostProcessors
    63  	return input, nil
    64  }
    65  
    66  func (FixerManifestFilename) Synopsis() string {
    67  	return `Updates "manifest" post-processor so any "filename" field is renamed to "output".`
    68  }