github.com/spinnaker/spin@v1.30.0/cmd/pipeline/execution/cancel.go (about)

     1  // Copyright (c) 2019, Google, Inc.
     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 execution
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"net/http"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	gate "github.com/spinnaker/spin/gateapi"
    25  	"github.com/spinnaker/spin/util"
    26  )
    27  
    28  var (
    29  	cancelExecutionShort = "Cancel the executions for the provided execution id"
    30  	cancelExecutionLong  = "Cancel the executions for the provided execution id"
    31  )
    32  
    33  type cancelOptions struct {
    34  	*executionOptions
    35  }
    36  
    37  func NewCancelCmd(executionOptions *executionOptions) *cobra.Command {
    38  	options := &cancelOptions{
    39  		executionOptions: executionOptions,
    40  	}
    41  	cmd := &cobra.Command{
    42  		Use:   "cancel",
    43  		Short: cancelExecutionShort,
    44  		Long:  cancelExecutionLong,
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  			return cancelExecution(cmd, options, args)
    47  		},
    48  	}
    49  
    50  	return cmd
    51  }
    52  
    53  func cancelExecution(cmd *cobra.Command, options *cancelOptions, args []string) error {
    54  	executionId, err := util.ReadArgsOrStdin(args)
    55  	if executionId == "" {
    56  		return errors.New("no execution id supplied, exiting")
    57  	}
    58  
    59  	resp, err := options.GateClient.PipelineControllerApi.CancelPipelineUsingPUT1(options.GateClient.Context,
    60  		executionId,
    61  		&gate.PipelineControllerApiCancelPipelineUsingPUT1Opts{})
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	if resp.StatusCode != http.StatusOK {
    67  		return fmt.Errorf("encountered an error canceling execution with id %s, status code: %d\n",
    68  			executionId,
    69  			resp.StatusCode)
    70  	}
    71  
    72  	options.Ui.Success(fmt.Sprintf("Execution %s successfully canceled", executionId))
    73  	return nil
    74  }