github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/packager/deprecated/scripts-to-actions.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  	"math"
    10  
    11  	"github.com/Racer159/jackal/src/types"
    12  )
    13  
    14  // migrateScriptsToActions coverts the deprecated scripts to the new actions
    15  // The following have no migration:
    16  // - Actions.Create.After
    17  // - Actions.Remove.*
    18  // - Actions.*.OnSuccess
    19  // - Actions.*.OnFailure
    20  // - Actions.*.*.Env
    21  func migrateScriptsToActions(c types.JackalComponent) (types.JackalComponent, string) {
    22  	var hasScripts bool
    23  
    24  	// Convert a script configs to action defaults.
    25  	defaults := types.JackalComponentActionDefaults{
    26  		// ShowOutput (default false) -> Mute (default false)
    27  		Mute: !c.DeprecatedScripts.ShowOutput,
    28  		// TimeoutSeconds -> MaxSeconds
    29  		MaxTotalSeconds: c.DeprecatedScripts.TimeoutSeconds,
    30  	}
    31  
    32  	// Retry is now an integer vs a boolean (implicit infinite retries), so set to an absurdly high number
    33  	if c.DeprecatedScripts.Retry {
    34  		defaults.MaxRetries = math.MaxInt
    35  	}
    36  
    37  	// Scripts.Prepare -> Actions.Create.Before
    38  	if len(c.DeprecatedScripts.Prepare) > 0 {
    39  		hasScripts = true
    40  		c.Actions.OnCreate.Defaults = defaults
    41  		for _, s := range c.DeprecatedScripts.Prepare {
    42  			c.Actions.OnCreate.Before = append(c.Actions.OnCreate.Before, types.JackalComponentAction{Cmd: s})
    43  		}
    44  	}
    45  
    46  	// Scripts.Before -> Actions.Deploy.Before
    47  	if len(c.DeprecatedScripts.Before) > 0 {
    48  		hasScripts = true
    49  		c.Actions.OnDeploy.Defaults = defaults
    50  		for _, s := range c.DeprecatedScripts.Before {
    51  			c.Actions.OnDeploy.Before = append(c.Actions.OnDeploy.Before, types.JackalComponentAction{Cmd: s})
    52  		}
    53  	}
    54  
    55  	// Scripts.After -> Actions.Deploy.After
    56  	if len(c.DeprecatedScripts.After) > 0 {
    57  		hasScripts = true
    58  		c.Actions.OnDeploy.Defaults = defaults
    59  		for _, s := range c.DeprecatedScripts.After {
    60  			c.Actions.OnDeploy.After = append(c.Actions.OnDeploy.After, types.JackalComponentAction{Cmd: s})
    61  		}
    62  	}
    63  
    64  	// Leave deprecated scripts in place, but warn users
    65  	if hasScripts {
    66  		return c, fmt.Sprintf("Component '%s' is using scripts which will be removed in Jackal v1.0.0. Please migrate to actions.", c.Name)
    67  	}
    68  
    69  	return c, ""
    70  }