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

     1  package webhooks
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
    11  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render"
    12  	"github.com/kubeshop/testkube/pkg/crd"
    13  	"github.com/kubeshop/testkube/pkg/ui"
    14  )
    15  
    16  func NewGetWebhookCmd() *cobra.Command {
    17  	var name string
    18  	var selectors []string
    19  	var crdOnly bool
    20  
    21  	cmd := &cobra.Command{
    22  		Use:     "webhook <webhookName>",
    23  		Aliases: []string{"webhooks", "wh"},
    24  		Short:   "Get webhook details",
    25  		Long:    `Get webhook, you can change output format, to get single details pass name as first arg`,
    26  		Run: func(cmd *cobra.Command, args []string) {
    27  			client, _, err := common.GetClient(cmd)
    28  			ui.ExitOnError("getting client", err)
    29  
    30  			firstEntry := true
    31  			if len(args) > 0 {
    32  				name := args[0]
    33  				webhook, err := client.GetWebhook(name)
    34  				ui.ExitOnError("getting webhook: "+name, err)
    35  
    36  				if crdOnly {
    37  					if webhook.PayloadTemplate != "" {
    38  						webhook.PayloadTemplate = fmt.Sprintf("%q", webhook.PayloadTemplate)
    39  					}
    40  
    41  					common.UIPrintCRD(crd.TemplateWebhook, webhook, &firstEntry)
    42  					return
    43  				}
    44  
    45  				err = render.Obj(cmd, webhook, os.Stdout)
    46  				ui.ExitOnError("rendering obj", err)
    47  			} else {
    48  				webhooks, err := client.ListWebhooks(strings.Join(selectors, ","))
    49  				ui.ExitOnError("getting webhooks", err)
    50  
    51  				if crdOnly {
    52  					for _, webhook := range webhooks {
    53  						if webhook.PayloadTemplate != "" {
    54  							webhook.PayloadTemplate = fmt.Sprintf("%q", webhook.PayloadTemplate)
    55  						}
    56  
    57  						common.UIPrintCRD(crd.TemplateWebhook, webhook, &firstEntry)
    58  					}
    59  
    60  					return
    61  				}
    62  
    63  				err = render.List(cmd, webhooks, os.Stdout)
    64  				ui.ExitOnError("rendering list", err)
    65  			}
    66  		},
    67  	}
    68  
    69  	cmd.Flags().StringVarP(&name, "name", "n", "", "unique webhook name, you can also pass it as argument")
    70  	cmd.Flags().StringSliceVarP(&selectors, "label", "l", nil, "label key value pair: --label key1=value1")
    71  	cmd.Flags().BoolVar(&crdOnly, "crd-only", false, "show only test crd")
    72  
    73  	return cmd
    74  }