github.com/spinnaker/spin@v1.30.0/cmd/pipeline-template/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_template
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  
    21  	"github.com/antihax/optional"
    22  	"github.com/spf13/cobra"
    23  
    24  	gate "github.com/spinnaker/spin/gateapi"
    25  	"github.com/spinnaker/spin/util"
    26  )
    27  
    28  type deleteOptions struct {
    29  	*pipelineTemplateOptions
    30  	tag string
    31  }
    32  
    33  var (
    34  	deletePipelineTemplateShort = "Delete the provided pipeline template"
    35  	deletePipelineTemplateLong  = "Delete the provided pipeline template"
    36  )
    37  
    38  func NewDeleteCmd(pipelineTemplateOptions *pipelineTemplateOptions) *cobra.Command {
    39  	options := &deleteOptions{
    40  		pipelineTemplateOptions: pipelineTemplateOptions,
    41  	}
    42  
    43  	cmd := &cobra.Command{
    44  		Use:     "delete",
    45  		Aliases: []string{"del"},
    46  		Short:   deletePipelineTemplateShort,
    47  		Long:    deletePipelineTemplateLong,
    48  		RunE: func(cmd *cobra.Command, args []string) error {
    49  			return deletePipelineTemplate(cmd, options, args)
    50  		},
    51  	}
    52  
    53  	cmd.PersistentFlags().StringVar(&options.tag, "tag", "",
    54  		"(optional) specific tag to query")
    55  
    56  	return cmd
    57  }
    58  
    59  func deletePipelineTemplate(cmd *cobra.Command, options *deleteOptions, args []string) error {
    60  	id, err := util.ReadArgsOrStdin(args)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	queryParams := &gate.V2PipelineTemplatesControllerApiDeleteUsingDELETE1Opts{}
    66  	if options.tag != "" {
    67  		queryParams.Tag = optional.NewString(options.tag)
    68  	}
    69  
    70  	_, resp, err := options.GateClient.V2PipelineTemplatesControllerApi.DeleteUsingDELETE1(options.GateClient.Context, id, queryParams)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	if resp.StatusCode != http.StatusAccepted {
    76  		return fmt.Errorf("Encountered an error deleting pipeline template, status code: %d\n", resp.StatusCode)
    77  	}
    78  
    79  	options.Ui.Success(fmt.Sprintf("Pipeline template %s deleted", id))
    80  	return nil
    81  }