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