github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/webhooks/delete.go (about)

     1  package webhooks
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
     9  	"github.com/kubeshop/testkube/pkg/ui"
    10  )
    11  
    12  func NewDeleteWebhookCmd() *cobra.Command {
    13  	var name string
    14  	var selectors []string
    15  
    16  	cmd := &cobra.Command{
    17  
    18  		Use:     "webhook <webhookName>",
    19  		Aliases: []string{"wh"},
    20  		Short:   "Delete webhook",
    21  		Long:    `Delete webhook, pass webhook name which should be deleted`,
    22  		Run: func(cmd *cobra.Command, args []string) {
    23  			client, _, err := common.GetClient(cmd)
    24  			ui.ExitOnError("getting client", err)
    25  
    26  			if len(args) > 0 {
    27  				name = args[0]
    28  				err := client.DeleteWebhook(name)
    29  				ui.ExitOnError("deleting webhook: "+name, err)
    30  				ui.SuccessAndExit("Succesfully deleted webhook", name)
    31  			}
    32  
    33  			if len(selectors) != 0 {
    34  				selector := strings.Join(selectors, ",")
    35  				err := client.DeleteWebhooks(selector)
    36  				ui.ExitOnError("deleting webhooks by labels: "+selector, err)
    37  				ui.SuccessAndExit("Succesfully deleted webhooks by labels", selector)
    38  			}
    39  
    40  			ui.Failf("Pass Webhook name or labels to delete by labels")
    41  		},
    42  	}
    43  
    44  	cmd.Flags().StringVarP(&name, "name", "n", "", "unique webhook name, you can also pass it as first argument")
    45  	cmd.Flags().StringSliceVarP(&selectors, "label", "l", nil, "label key value pair: --label key1=value1")
    46  
    47  	return cmd
    48  }