github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/packager/deprecated/pluralize-set-variable.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package deprecated handles package deprecations and migrations
     5  package deprecated
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/Racer159/jackal/src/types"
    11  )
    12  
    13  func migrateSetVariableToSetVariables(c types.JackalComponent) (types.JackalComponent, string) {
    14  	hasSetVariable := false
    15  
    16  	migrate := func(actions []types.JackalComponentAction) []types.JackalComponentAction {
    17  		for i := range actions {
    18  			if actions[i].DeprecatedSetVariable != "" && len(actions[i].SetVariables) < 1 {
    19  				hasSetVariable = true
    20  				actions[i].SetVariables = []types.JackalComponentActionSetVariable{
    21  					{
    22  						Name:      actions[i].DeprecatedSetVariable,
    23  						Sensitive: false,
    24  					},
    25  				}
    26  			}
    27  		}
    28  
    29  		return actions
    30  	}
    31  
    32  	// Migrate OnCreate SetVariables
    33  	c.Actions.OnCreate.After = migrate(c.Actions.OnCreate.After)
    34  	c.Actions.OnCreate.Before = migrate(c.Actions.OnCreate.Before)
    35  	c.Actions.OnCreate.OnSuccess = migrate(c.Actions.OnCreate.OnSuccess)
    36  	c.Actions.OnCreate.OnFailure = migrate(c.Actions.OnCreate.OnFailure)
    37  
    38  	// Migrate OnDeploy SetVariables
    39  	c.Actions.OnDeploy.After = migrate(c.Actions.OnDeploy.After)
    40  	c.Actions.OnDeploy.Before = migrate(c.Actions.OnDeploy.Before)
    41  	c.Actions.OnDeploy.OnSuccess = migrate(c.Actions.OnDeploy.OnSuccess)
    42  	c.Actions.OnDeploy.OnFailure = migrate(c.Actions.OnDeploy.OnFailure)
    43  
    44  	// Migrate OnRemove SetVariables
    45  	c.Actions.OnRemove.After = migrate(c.Actions.OnRemove.After)
    46  	c.Actions.OnRemove.Before = migrate(c.Actions.OnRemove.Before)
    47  	c.Actions.OnRemove.OnSuccess = migrate(c.Actions.OnRemove.OnSuccess)
    48  	c.Actions.OnRemove.OnFailure = migrate(c.Actions.OnRemove.OnFailure)
    49  
    50  	// Leave deprecated setVariable in place, but warn users
    51  	if hasSetVariable {
    52  		return c, fmt.Sprintf("Component '%s' is using setVariable in actions which will be removed in Jackal v1.0.0. Please migrate to the list form of setVariables.", c.Name)
    53  	}
    54  
    55  	return c, ""
    56  }
    57  
    58  func clearSetVariables(c types.JackalComponent) types.JackalComponent {
    59  	clear := func(actions []types.JackalComponentAction) []types.JackalComponentAction {
    60  		for i := range actions {
    61  			actions[i].DeprecatedSetVariable = ""
    62  		}
    63  
    64  		return actions
    65  	}
    66  
    67  	// Clear OnCreate SetVariables
    68  	c.Actions.OnCreate.After = clear(c.Actions.OnCreate.After)
    69  	c.Actions.OnCreate.Before = clear(c.Actions.OnCreate.Before)
    70  	c.Actions.OnCreate.OnSuccess = clear(c.Actions.OnCreate.OnSuccess)
    71  	c.Actions.OnCreate.OnFailure = clear(c.Actions.OnCreate.OnFailure)
    72  
    73  	// Clear OnDeploy SetVariables
    74  	c.Actions.OnDeploy.After = clear(c.Actions.OnDeploy.After)
    75  	c.Actions.OnDeploy.Before = clear(c.Actions.OnDeploy.Before)
    76  	c.Actions.OnDeploy.OnSuccess = clear(c.Actions.OnDeploy.OnSuccess)
    77  	c.Actions.OnDeploy.OnFailure = clear(c.Actions.OnDeploy.OnFailure)
    78  
    79  	// Clear OnRemove SetVariables
    80  	c.Actions.OnRemove.After = clear(c.Actions.OnRemove.After)
    81  	c.Actions.OnRemove.Before = clear(c.Actions.OnRemove.Before)
    82  	c.Actions.OnRemove.OnSuccess = clear(c.Actions.OnRemove.OnSuccess)
    83  	c.Actions.OnRemove.OnFailure = clear(c.Actions.OnRemove.OnFailure)
    84  
    85  	return c
    86  }