github.com/hashicorp/packer@v1.14.3/fix/fixer_galaxy_command.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  // FixerGalaxyCommand removes the escape character from user
    11  // environment variables and replace galaxycommand with galaxy_command
    12  type FixerGalaxyCommand struct{}
    13  
    14  func (FixerGalaxyCommand) DeprecatedOptions() map[string][]string {
    15  	return map[string][]string{
    16  		"ansible": {"galaxycommand"},
    17  	}
    18  }
    19  
    20  func (FixerGalaxyCommand) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    21  	type template struct {
    22  		Provisioners []interface{}
    23  	}
    24  
    25  	// Decode the input into our structure, if we can
    26  	var tpl template
    27  	if err := mapstructure.WeakDecode(input, &tpl); err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	for i, raw := range tpl.Provisioners {
    32  		var provisioners map[string]interface{}
    33  		if err := mapstructure.Decode(raw, &provisioners); err != nil {
    34  			// Ignore errors, could be a non-map
    35  			continue
    36  		}
    37  
    38  		if ok := provisioners["type"] == "ansible-local"; !ok {
    39  			continue
    40  		}
    41  
    42  		if _, ok := provisioners["galaxy_command"]; ok {
    43  			// drop galaxycommand if it is also included
    44  			delete(provisioners, "galaxycommand")
    45  		} else {
    46  
    47  			// replace galaxycommand with galaxy_command if it exists
    48  			galaxyCommandRaw, ok := provisioners["galaxycommand"]
    49  			if !ok {
    50  				continue
    51  			}
    52  
    53  			galaxyCommandString, ok := galaxyCommandRaw.(string)
    54  			if !ok {
    55  				continue
    56  			}
    57  
    58  			delete(provisioners, "galaxycommand")
    59  			provisioners["galaxy_command"] = galaxyCommandString
    60  		}
    61  
    62  		// Write all changes back to template
    63  		tpl.Provisioners[i] = provisioners
    64  	}
    65  
    66  	if len(tpl.Provisioners) > 0 {
    67  		input["provisioners"] = tpl.Provisioners
    68  	}
    69  
    70  	return input, nil
    71  }
    72  
    73  func (FixerGalaxyCommand) Synopsis() string {
    74  	return `Replaces "galaxycommand" in ansible-local provisioner configs with "galaxy_command"`
    75  }