github.com/spinnaker/spin@v1.30.0/cmd/application/cancel_pipelines.go (about)

     1  // Copyright (c) 2020, Anosua "Chini" Mukhopadhyay
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //   Unless required by applicable law or agreed to in writing, software
    10  //   distributed under the License is distributed on an "AS IS" BASIS,
    11  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //   See the License for the specific language governing permissions and
    13  //   limitations under the License.
    14  
    15  package application
    16  
    17  import (
    18  	"fmt"
    19  	// "github.com/antihax/optional"
    20  	"github.com/spf13/cobra"
    21  	//gate "github.com/spinnaker/spin/gateapi"
    22  )
    23  
    24  type cancelAllPipelinesOptions struct {
    25  	*pipelineOptions
    26  	getPipelinesOptions   getPipelinesOptions
    27  	cancelPipelineOptions cancelPipelineOptions
    28  }
    29  
    30  var (
    31  	cancelAllPipelinesShort   = "Cancel all the pipelines for the specified application with the specified status"
    32  	cancelAllPipelinesLong    = "Cancel all the pipelines for the specified application with the specified status"
    33  	cancelAllPipelinesExample = "usage: spin application pipelines cancel-all [options] id reason"
    34  )
    35  
    36  func NewCancelAllPipelinesCmd(pipeOptions *pipelineOptions) *cobra.Command {
    37  	options := &cancelAllPipelinesOptions{
    38  		pipelineOptions: pipeOptions,
    39  		getPipelinesOptions: getPipelinesOptions{
    40  			pipelineOptions: pipeOptions,
    41  			expand:          false,
    42  		},
    43  		cancelPipelineOptions: cancelPipelineOptions{
    44  			pipelineOptions: pipeOptions,
    45  		},
    46  	}
    47  
    48  	cmd := &cobra.Command{
    49  		Use:     "cancel-all",
    50  		Aliases: []string{"cancel-all"},
    51  		Short:   cancelAllPipelinesShort,
    52  		Long:    cancelAllPipelinesLong,
    53  		Example: cancelAllPipelinesExample,
    54  		RunE: func(cmd *cobra.Command, args []string) error {
    55  			return cancelAllPipelines(cmd, options, args)
    56  		},
    57  	}
    58  
    59  	cmd.PersistentFlags().StringVarP(&options.getPipelinesOptions.applicationName, "application-name", "a", "", "name of the application")
    60  	cmd.PersistentFlags().StringVarP(&options.getPipelinesOptions.status, "status", "s", "", "status pipeline to search for")
    61  	cmd.PersistentFlags().StringVarP(&options.cancelPipelineOptions.reason, "reason", "r", "", "reason for cancelling pipeline")
    62  	return cmd
    63  }
    64  
    65  func cancelAllPipelines(cmd *cobra.Command, options *cancelAllPipelinesOptions, args []string) error {
    66  	app, err := getPipelines(cmd, &options.getPipelinesOptions, args)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	if options.getPipelinesOptions.status == "" {
    72  		options.Ui.Warn(fmt.Sprintf("No status was passed in. Cancelling all pipelines under application %s", options.getPipelinesOptions.applicationName))
    73  	}
    74  
    75  	for _, foundPipeline := range app {
    76  		foundPipeline, ok := foundPipeline.(map[string]interface{})
    77  		if !ok {
    78  			return fmt.Errorf("foundPipeline should be an interface array")
    79  		}
    80  		if foundPipeline == nil {
    81  			return fmt.Errorf("Output in NIL")
    82  		}
    83  		for key, value := range foundPipeline {
    84  			if key == "id" {
    85  				options.Ui.Info(fmt.Sprintf("Cancelling execution ID %s...", value))
    86  				err := cancelPipelineWithID(&options.cancelPipelineOptions, fmt.Sprintf("%v", value))
    87  				if err != nil {
    88  					return err
    89  				}
    90  			}
    91  		}
    92  	}
    93  
    94  	return nil
    95  }