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

     1  package artifacts
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/pkg/errors"
     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/validator"
    11  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    12  	"github.com/kubeshop/testkube/pkg/ui"
    13  )
    14  
    15  var (
    16  	executionID string
    17  )
    18  
    19  func NewListArtifactsCmd() *cobra.Command {
    20  	cmd := &cobra.Command{
    21  		Use:     "artifact <executionName>",
    22  		Aliases: []string{"artifacts"},
    23  		Short:   "List artifacts of the given test, test suite or test workflow execution name",
    24  		Args:    validator.ExecutionName,
    25  		Run: func(cmd *cobra.Command, args []string) {
    26  			executionID = args[0]
    27  			cmd.SilenceUsage = true
    28  			client, _, err := common.GetClient(cmd)
    29  			ui.ExitOnError("getting client", err)
    30  
    31  			execution, err := client.GetExecution(executionID)
    32  			var artifacts testkube.Artifacts
    33  			var errArtifacts error
    34  			if err == nil && execution.Id != "" {
    35  				artifacts, errArtifacts = client.GetExecutionArtifacts(execution.Id)
    36  				ui.ExitOnError("getting test artifacts", errArtifacts)
    37  				ui.Table(artifacts, os.Stdout)
    38  				return
    39  			}
    40  			tsExecution, err := client.GetTestSuiteExecution(executionID)
    41  			if err == nil && tsExecution.Id != "" {
    42  				artifacts, errArtifacts = client.GetTestSuiteExecutionArtifacts(tsExecution.Id)
    43  				ui.ExitOnError("getting test suite artifacts", errArtifacts)
    44  				ui.Table(artifacts, os.Stdout)
    45  				return
    46  			}
    47  			twExecution, err := client.GetTestWorkflowExecution(executionID)
    48  			if err == nil && twExecution.Id != "" {
    49  				artifacts, errArtifacts = client.GetTestWorkflowExecutionArtifacts(twExecution.Id)
    50  				ui.ExitOnError("getting test workflow artifacts", errArtifacts)
    51  				ui.Table(artifacts, os.Stdout)
    52  				return
    53  			}
    54  			if err == nil {
    55  				err = errors.New("no test, test suite or test workflow execution was found with the following id")
    56  			}
    57  			ui.Fail(err)
    58  		},
    59  	}
    60  
    61  	cmd.PersistentFlags().StringVarP(&executionID, "execution-id", "e", "", "ID of the execution")
    62  
    63  	// output renderer flags
    64  	return cmd
    65  }