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

     1  // Copyright (c) 2018, 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 pipeline
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"net/http"
    21  
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  type deleteOptions struct {
    26  	*PipelineOptions
    27  	output      string
    28  	application string
    29  	name        string
    30  }
    31  
    32  var (
    33  	deletePipelineShort = "Delete the provided pipeline"
    34  	deletePipelineLong  = "Delete the provided pipeline"
    35  )
    36  
    37  func NewDeleteCmd(pipelineOptions *PipelineOptions) *cobra.Command {
    38  	options := &deleteOptions{
    39  		PipelineOptions: pipelineOptions,
    40  	}
    41  	cmd := &cobra.Command{
    42  		Use:     "delete",
    43  		Aliases: []string{"del"},
    44  		Short:   deletePipelineShort,
    45  		Long:    deletePipelineLong,
    46  		RunE: func(cmd *cobra.Command, args []string) error {
    47  			return deletePipeline(cmd, options)
    48  		},
    49  	}
    50  
    51  	cmd.PersistentFlags().StringVarP(&options.application, "application", "a", "", "Spinnaker application the pipeline lives in")
    52  	cmd.PersistentFlags().StringVarP(&options.name, "name", "n", "", "name of the pipeline to delete")
    53  
    54  	return cmd
    55  }
    56  
    57  func deletePipeline(cmd *cobra.Command, options *deleteOptions) error {
    58  	if options.application == "" || options.name == "" {
    59  		return errors.New("one of required parameters 'application' or 'name' not set")
    60  	}
    61  	resp, err := options.GateClient.PipelineControllerApi.DeletePipelineUsingDELETE(options.GateClient.Context, options.application, options.name)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	if resp.StatusCode != http.StatusOK {
    67  		return fmt.Errorf("Encountered an error deleting pipeline, status code: %d\n", resp.StatusCode)
    68  	}
    69  
    70  	options.Ui.Success("Pipeline deleted")
    71  	return nil
    72  }