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

     1  package testsuites
     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/testsuites/renderer"
    12  	"github.com/kubeshop/testkube/pkg/crd"
    13  	"github.com/kubeshop/testkube/pkg/ui"
    14  )
    15  
    16  func NewGetTestSuiteCmd() *cobra.Command {
    17  	var (
    18  		selectors   []string
    19  		noExecution bool
    20  		crdOnly     bool
    21  	)
    22  
    23  	cmd := &cobra.Command{
    24  		Use:     "testsuite <testSuiteName>",
    25  		Aliases: []string{"testsuites", "ts"},
    26  		Short:   "Get test suite by name",
    27  		Long:    `Getting test suite from given namespace - if no namespace given "testkube" namespace is used`,
    28  		Run: func(cmd *cobra.Command, args []string) {
    29  			client, _, err := common.GetClient(cmd)
    30  			ui.ExitOnError("getting client", err)
    31  
    32  			firstEntry := true
    33  			if len(args) > 0 {
    34  				ui.NL()
    35  				name := args[0]
    36  				testSuite, err := client.GetTestSuiteWithExecution(name)
    37  				ui.ExitOnError("getting test suite "+name, err)
    38  
    39  				if testSuite.TestSuite != nil {
    40  					if crdOnly {
    41  						testSuite.TestSuite.QuoteTestSuiteTextFields()
    42  						common.UIPrintCRD(crd.TemplateTestSuite, testSuite.TestSuite, &firstEntry)
    43  						return
    44  					}
    45  
    46  					ui.Info("Test Suite:")
    47  					err = render.Obj(cmd, *testSuite.TestSuite, os.Stdout, renderer.TestSuiteRenderer)
    48  					ui.ExitOnError("rendering obj", err)
    49  				}
    50  
    51  				if testSuite.LatestExecution != nil && !noExecution {
    52  					ui.NL()
    53  					ui.Info("Latest execution")
    54  					err = render.Obj(cmd, *testSuite.LatestExecution, os.Stdout, renderer.TestSuiteExecutionRenderer)
    55  					ui.ExitOnError("rendering obj", err)
    56  				}
    57  			} else {
    58  				if noExecution {
    59  					testSuites, err := client.ListTestSuites(strings.Join(selectors, ","))
    60  					ui.ExitOnError("getting test suites", err)
    61  
    62  					if crdOnly {
    63  						for _, testSuite := range testSuites {
    64  							testSuite.QuoteTestSuiteTextFields()
    65  							common.UIPrintCRD(crd.TemplateTestSuite, testSuite, &firstEntry)
    66  						}
    67  
    68  						return
    69  					}
    70  
    71  					err = render.List(cmd, testSuites, os.Stdout)
    72  					ui.ExitOnError("rendering list", err)
    73  				} else {
    74  					testSuites, err := client.ListTestSuiteWithExecutionSummaries(strings.Join(selectors, ","))
    75  					ui.ExitOnError("getting test suite with execution summaries", err)
    76  
    77  					if crdOnly {
    78  						for _, testSuite := range testSuites {
    79  							if testSuite.TestSuite != nil {
    80  								testSuite.TestSuite.QuoteTestSuiteTextFields()
    81  								common.UIPrintCRD(crd.TemplateTestSuite, testSuite.TestSuite, &firstEntry)
    82  							}
    83  						}
    84  
    85  						return
    86  					}
    87  
    88  					err = render.List(cmd, testSuites, os.Stdout)
    89  					ui.ExitOnError("rendering list", err)
    90  				}
    91  			}
    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  }