diff --git a/cmd/createPromotePipelineJob.go b/cmd/createPromotePipelineJob.go index 25e0269..63df42f 100644 --- a/cmd/createPromotePipelineJob.go +++ b/cmd/createPromotePipelineJob.go @@ -15,7 +15,9 @@ package cmd import ( - "errors" + apiclient "github.com/equinor/radix-cli/generated-client/client" + "github.com/equinor/radix-cli/generated-client/client/environment" + "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/equinor/radix-cli/generated-client/client/application" @@ -35,14 +37,22 @@ var createPromotePipelineJobCmd = &cobra.Command{ return err } + useActiveDeployment, _ := cmd.Flags().GetBool("use-active-deployment") deploymentName, _ := cmd.Flags().GetString("deployment") fromEnvironment, _ := cmd.Flags().GetString("from-environment") toEnvironment, _ := cmd.Flags().GetString("to-environment") triggeredByUser, _ := cmd.Flags().GetString("user") follow, _ := cmd.Flags().GetBool("follow") - if appName == nil || *appName == "" || deploymentName == "" || fromEnvironment == "" || toEnvironment == "" { - return errors.New("application name, deployment name, from and to environments are required") + if !useActiveDeployment && deploymentName == "" { + return errors.New("Specifying deployment name or setting use-active-deployment is required") + } + if useActiveDeployment && deploymentName != "" { + return errors.New("You cannot set use-active-deployment and specify deployment name at the same time") + } + + if appName == nil || *appName == "" || fromEnvironment == "" || toEnvironment == "" { + return errors.New("application name, from and to environments are required") } cmd.SilenceUsage = true @@ -51,6 +61,16 @@ var createPromotePipelineJobCmd = &cobra.Command{ if err != nil { return err } + + if useActiveDeployment { + d, err := getActiveDeploymentName(apiClient, *appName, fromEnvironment) + if err != nil { + return err + } + + deploymentName = d + } + triggerPipelineParams := application.NewTriggerPipelinePromoteParams() triggerPipelineParams.SetAppName(*appName) triggerPipelineParams.SetPipelineParametersPromote(&models.PipelineParametersPromote{ @@ -75,6 +95,23 @@ var createPromotePipelineJobCmd = &cobra.Command{ }, } +func getActiveDeploymentName(apiClient *apiclient.Radixapi, appName, envName string) (string, error) { + params := environment.NewGetEnvironmentParams() + params.SetAppName(appName) + params.SetEnvName(envName) + + resp, err := apiClient.Environment.GetEnvironment(params, nil) + if err != nil { + return "", errors.Wrap(err, "Failed to get environment details") + } + + if resp.Payload.ActiveDeployment == nil || resp.Payload.ActiveDeployment.Name == "" { + return "", errors.Errorf("Environment '%s' does not have any active deployments", envName) + } + + return resp.Payload.ActiveDeployment.Name, nil +} + func init() { createJobCmd.AddCommand(createPromotePipelineJobCmd) createPromotePipelineJobCmd.Flags().StringP("application", "a", "", "Name of the application to be promoted") @@ -83,5 +120,6 @@ func init() { createPromotePipelineJobCmd.Flags().StringP("to-environment", "", "", "The deployment target environment") createPromotePipelineJobCmd.Flags().StringP("user", "u", "", "The user who triggered the promote pipeline job") createPromotePipelineJobCmd.Flags().BoolP("follow", "f", false, "Follow the promote pipeline job log") + createPromotePipelineJobCmd.Flags().BoolP("use-active-deployment", "", false, "Promote the active deployment") setContextSpecificPersistentFlags(createPromotePipelineJobCmd) } diff --git a/cmd/getBranchEnvironment.go b/cmd/getBranchEnvironment.go index 332412b..bb85a06 100644 --- a/cmd/getBranchEnvironment.go +++ b/cmd/getBranchEnvironment.go @@ -16,6 +16,7 @@ package cmd import ( "errors" + "fmt" "github.com/equinor/radix-cli/pkg/settings" "github.com/spf13/cobra" @@ -48,7 +49,7 @@ var getBranchEnvironmentCmd = &cobra.Command{ return err } - println(*environment) + fmt.Print(*environment) return nil }, } diff --git a/cmd/root.go b/cmd/root.go index da922a4..e1ea23e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,7 +19,7 @@ import ( const ( radixCLIError = "Error: Radix CLI executed with error" - version = "1.8.1" + version = "1.9.0" ) var rootLongHelp = strings.TrimSpace(` diff --git a/go.mod b/go.mod index 9950f26..b7e5e12 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/go-openapi/strfmt v0.21.7 github.com/go-openapi/swag v0.22.4 github.com/go-openapi/validate v0.22.1 + github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 k8s.io/utils v0.0.0-20230505201702-9f6742963106