github.phpd.cn/hashicorp/packer@v1.3.2/fix/fixer_pp_manifest_filename.go (about)

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