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

     1  package templates
     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 NewGetTemplateCmd() *cobra.Command {
    17  	var name string
    18  	var selectors []string
    19  	var crdOnly bool
    20  
    21  	cmd := &cobra.Command{
    22  		Use:     "template <templateName>",
    23  		Aliases: []string{"templates", "tp"},
    24  		Short:   "Get template details.",
    25  		Long:    `Get template allows you to change the output format. To get single details, pass the template name as the first argument.`,
    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  				template, err := client.GetTemplate(name)
    34  				ui.ExitOnError("getting template: "+name, err)
    35  
    36  				if crdOnly {
    37  					if template.Body != "" {
    38  						template.Body = fmt.Sprintf("%q", template.Body)
    39  					}
    40  
    41  					common.UIPrintCRD(crd.TemplateTemplate, template, &firstEntry)
    42  					return
    43  				}
    44  
    45  				err = render.Obj(cmd, template, os.Stdout)
    46  				ui.ExitOnError("rendering obj", err)
    47  			} else {
    48  				templates, err := client.ListTemplates(strings.Join(selectors, ","))
    49  				ui.ExitOnError("getting templates", err)
    50  
    51  				if crdOnly {
    52  					for _, template := range templates {
    53  						if template.Body != "" {
    54  							template.Body = fmt.Sprintf("%q", template.Body)
    55  						}
    56  
    57  						common.UIPrintCRD(crd.TemplateTemplate, template, &firstEntry)
    58  					}
    59  
    60  					return
    61  				}
    62  
    63  				err = render.List(cmd, templates, os.Stdout)
    64  				ui.ExitOnError("rendering list", err)
    65  			}
    66  		},
    67  	}
    68  
    69  	cmd.Flags().StringVarP(&name, "name", "n", "", "unique template 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  }