github.com/spinnaker/spin@v1.30.0/cmd/application/cancel_pipeline.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 "errors" 19 "fmt" 20 "net/http" 21 22 "github.com/antihax/optional" 23 "github.com/spf13/cobra" 24 25 gate "github.com/spinnaker/spin/gateapi" 26 ) 27 28 type cancelPipelineOptions struct { 29 *pipelineOptions 30 id string 31 reason string 32 } 33 34 var ( 35 cancelPipelinesShort = "Cancel the pipeline for the specified pipeline execution id" 36 cancelPipelinesLong = "Cancel the pipeline for the specified pipeline execution id" 37 cancelPipelinesExample = "usage: spin application pipelines cancel [options] id reason" 38 ) 39 40 func NewCancelPipelineCmd(pipeOptions *pipelineOptions) *cobra.Command { 41 options := &cancelPipelineOptions{ 42 pipelineOptions: pipeOptions, 43 } 44 45 cmd := &cobra.Command{ 46 Use: "cancel", 47 Aliases: []string{"cancel"}, 48 Short: cancelPipelinesShort, 49 Long: cancelPipelinesLong, 50 Example: cancelPipelinesExample, 51 RunE: func(cmd *cobra.Command, args []string) error { 52 return cancelPipeline(cmd, options, args) 53 }, 54 } 55 56 cmd.PersistentFlags().StringVarP(&options.reason, "reason", "r", "", "reason for cancelling pipeline") 57 cmd.PersistentFlags().StringVarP(&options.id, "id", "i", "", "id of pipeline execution to cancel") 58 return cmd 59 } 60 61 func cancelPipeline(cmd *cobra.Command, options *cancelPipelineOptions, args []string) error { 62 err := cancelPipelineWithID(options, options.id) 63 64 if err != nil { 65 return err 66 } 67 68 return nil 69 } 70 71 func cancelPipelineWithID(options *cancelPipelineOptions, id string) error { 72 if id == "" { 73 return errors.New("execution ID must be passed in") 74 } 75 76 pipeline, resp, err := options.GateClient.ApplicationControllerApi.CancelPipelineUsingPUT(options.GateClient.Context, id, &gate.ApplicationControllerApiCancelPipelineUsingPUTOpts{Reason: optional.NewString(options.reason)}) 77 78 if resp != nil { 79 switch resp.StatusCode { 80 case http.StatusOK: 81 // pass 82 case http.StatusNotFound: 83 return fmt.Errorf("Execution ID '%s' not found\n", options.id) 84 default: 85 return fmt.Errorf("Encountered an error getting execution ID, status code: %d\n%v", resp.StatusCode, pipeline) 86 } 87 } 88 89 if err != nil { 90 return err 91 } 92 93 options.Ui.Info(fmt.Sprintf("Pipeline %v was cancelled.", id)) 94 return nil 95 }