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

     1  package tests
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
    10  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render"
    11  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/tests/renderer"
    12  	"github.com/kubeshop/testkube/pkg/crd"
    13  	"github.com/kubeshop/testkube/pkg/ui"
    14  )
    15  
    16  func NewGetTestsCmd() *cobra.Command {
    17  	var (
    18  		selectors   []string
    19  		noExecution bool
    20  		crdOnly     bool
    21  	)
    22  
    23  	cmd := &cobra.Command{
    24  		Use:     "test <testName>",
    25  		Aliases: []string{"tests", "t"},
    26  		Short:   "Get all available tests",
    27  		Long:    `Getting all available tests from given namespace - if no namespace given "testkube" namespace is used`,
    28  		Run: func(cmd *cobra.Command, args []string) {
    29  			namespace := cmd.Flag("namespace").Value.String()
    30  			client, _, err := common.GetClient(cmd)
    31  			ui.ExitOnError("getting client", err)
    32  
    33  			var name string
    34  			firstEntry := true
    35  			if len(args) > 0 {
    36  				name = args[0]
    37  				test, err := client.GetTestWithExecution(name)
    38  				ui.ExitOnError("getting test in namespace "+namespace, err)
    39  
    40  				if test.Test != nil {
    41  					if crdOnly {
    42  						test.Test.QuoteTestTextFields()
    43  						common.UIPrintCRD(crd.TemplateTest, test.Test, &firstEntry)
    44  						return
    45  					}
    46  
    47  					ui.NL()
    48  					ui.Info("Test:")
    49  					err = render.Obj(cmd, *test.Test, os.Stdout, renderer.TestRenderer)
    50  					ui.ExitOnError("rendering obj", err)
    51  				}
    52  
    53  				if test.LatestExecution != nil && !noExecution {
    54  					ui.NL()
    55  					ui.Info("Latest execution")
    56  					err = render.Obj(cmd, *test.LatestExecution, os.Stdout, renderer.ExecutionRenderer)
    57  					ui.ExitOnError("rendering obj", err)
    58  				}
    59  
    60  			} else {
    61  				if noExecution {
    62  					tests, err := client.ListTests(strings.Join(selectors, ","))
    63  					ui.ExitOnError("getting all tests in namespace "+namespace, err)
    64  
    65  					if crdOnly {
    66  						for _, test := range tests {
    67  							test.QuoteTestTextFields()
    68  							common.UIPrintCRD(crd.TemplateTest, test, &firstEntry)
    69  						}
    70  
    71  						return
    72  					}
    73  
    74  					err = render.List(cmd, tests, os.Stdout)
    75  					ui.PrintOnError("Rendering list", err)
    76  				} else {
    77  					tests, err := client.ListTestWithExecutionSummaries(strings.Join(selectors, ","))
    78  					ui.ExitOnError("getting all test with execution summaries in namespace "+namespace, err)
    79  					if crdOnly {
    80  						for _, test := range tests {
    81  							if test.Test != nil {
    82  								test.Test.QuoteTestTextFields()
    83  								common.UIPrintCRD(crd.TemplateTest, test.Test, &firstEntry)
    84  							}
    85  						}
    86  
    87  						return
    88  					}
    89  
    90  					err = render.List(cmd, tests, os.Stdout)
    91  					ui.PrintOnError("Rendering list", err)
    92  				}
    93  			}
    94  		},
    95  	}
    96  	cmd.Flags().StringSliceVarP(&selectors, "label", "l", nil, "label key value pair: --label key1=value1")
    97  	cmd.Flags().BoolVar(&noExecution, "no-execution", false, "don't show latest execution")
    98  	cmd.Flags().BoolVar(&crdOnly, "crd-only", false, "show only test crd")
    99  
   100  	return cmd
   101  }