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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     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/validator"
    11  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/tests"
    12  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/testsuites"
    13  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
    14  	"github.com/kubeshop/testkube/pkg/ui"
    15  )
    16  
    17  var (
    18  	executionID string
    19  	filename    string
    20  	destination string
    21  	downloadDir string
    22  	format      string
    23  	masks       []string
    24  )
    25  
    26  func NewDownloadCmd() *cobra.Command {
    27  
    28  	cmd := &cobra.Command{
    29  		Use:   "download <resource>",
    30  		Short: "Artifacts management commands",
    31  		Args:  validator.ExecutionName,
    32  		Run: func(cmd *cobra.Command, args []string) {
    33  			err := cmd.Help()
    34  			ui.PrintOnError("Displaying help", err)
    35  		},
    36  		PersistentPreRun: func(cmd *cobra.Command, args []string) {
    37  			cfg, err := config.Load()
    38  			ui.ExitOnError("loading config", err)
    39  			common.UiContextHeader(cmd, cfg)
    40  
    41  			validator.PersistentPreRunVersionCheck(cmd, common.Version)
    42  		}}
    43  
    44  	cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "should I show additional debug messages")
    45  
    46  	cmd.AddCommand(NewDownloadSingleArtifactsCmd())
    47  	cmd.AddCommand(NewDownloadAllArtifactsCmd())
    48  	cmd.AddCommand(NewDownloadTestSuiteArtifactsCmd())
    49  
    50  	return cmd
    51  }
    52  
    53  func NewListArtifactsCmd() *cobra.Command {
    54  	cmd := &cobra.Command{
    55  		Use:   "list <executionName>",
    56  		Short: "List artifacts of the given execution name",
    57  		Args:  validator.ExecutionName,
    58  		Run: func(cmd *cobra.Command, args []string) {
    59  			executionID = args[0]
    60  			cmd.SilenceUsage = true
    61  			client, _, err := common.GetClient(cmd)
    62  			ui.ExitOnError("getting client", err)
    63  
    64  			artifacts, err := client.GetExecutionArtifacts(executionID)
    65  			ui.ExitOnError("getting artifacts ", err)
    66  
    67  			ui.Table(artifacts, os.Stdout)
    68  		},
    69  	}
    70  
    71  	cmd.PersistentFlags().StringVarP(&client, "client", "c", "proxy", "Client used for connecting to testkube API one of proxy|direct|cluster")
    72  	cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "should I show additional debug messages")
    73  
    74  	cmd.PersistentFlags().StringVarP(&executionID, "execution-id", "e", "", "ID of the execution")
    75  
    76  	// output renderer flags
    77  	return cmd
    78  }
    79  
    80  func NewDownloadSingleArtifactsCmd() *cobra.Command {
    81  	cmd := &cobra.Command{
    82  		Use:   "artifact <executionName> <fileName> <destinationDir>",
    83  		Short: "download artifact",
    84  		Args:  validator.ExecutionIDAndFileNames,
    85  		Run: func(cmd *cobra.Command, args []string) {
    86  			executionID := args[0]
    87  			filename := args[1]
    88  			destination := args[2]
    89  
    90  			client, _, err := common.GetClient(cmd)
    91  			ui.ExitOnError("getting client", err)
    92  
    93  			execution, err := client.GetExecution(executionID)
    94  			if err == nil && execution.Id != "" {
    95  				f, err := client.DownloadFile(executionID, filename, destination)
    96  				ui.ExitOnError("downloading file "+filename, err)
    97  				ui.Info(fmt.Sprintf("File %s downloaded.\n", f))
    98  				return
    99  			}
   100  			twExecution, err := client.GetTestWorkflowExecution(executionID)
   101  			if err == nil && twExecution.Id != "" {
   102  				f, err := client.DownloadTestWorkflowArtifact(executionID, filename, destination)
   103  				ui.ExitOnError("downloading file "+filename, err)
   104  				ui.Info(fmt.Sprintf("File %s downloaded.\n", f))
   105  				return
   106  			}
   107  
   108  			ui.ExitOnError("retrieving execution", err)
   109  		},
   110  	}
   111  
   112  	cmd.PersistentFlags().StringVarP(&client, "client", "c", "proxy", "Client used for connecting to testkube API one of proxy|direct|cluster")
   113  	cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "should I show additional debug messages")
   114  
   115  	cmd.PersistentFlags().StringVarP(&executionID, "execution-id", "e", "", "ID of the execution")
   116  	cmd.PersistentFlags().StringVarP(&filename, "filename", "f", "", "name of the file")
   117  	cmd.PersistentFlags().StringVarP(&destination, "destination", "d", "", "name of the file")
   118  
   119  	// output renderer flags
   120  	return cmd
   121  }
   122  
   123  func NewDownloadAllArtifactsCmd() *cobra.Command {
   124  	cmd := &cobra.Command{
   125  		Use:     "artifacts <executionName>",
   126  		Aliases: []string{"a"},
   127  		Short:   "download artifacts",
   128  		Args:    validator.ExecutionName,
   129  		Run: func(cmd *cobra.Command, args []string) {
   130  			executionID := args[0]
   131  			client, _, err := common.GetClient(cmd)
   132  			ui.ExitOnError("getting client", err)
   133  
   134  			execution, err := client.GetExecution(executionID)
   135  			if err == nil && execution.Id != "" {
   136  				tests.DownloadTestArtifacts(executionID, downloadDir, format, masks, client)
   137  				return
   138  			}
   139  			twExecution, err := client.GetTestWorkflowExecution(executionID)
   140  			if err == nil && twExecution.Id != "" {
   141  				tests.DownloadTestWorkflowArtifacts(executionID, downloadDir, format, masks, client)
   142  				return
   143  			}
   144  		},
   145  	}
   146  
   147  	cmd.PersistentFlags().StringVarP(&client, "client", "c", "proxy", "Client used for connecting to testkube API one of proxy|direct|cluster")
   148  	cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "should I show additional debug messages")
   149  
   150  	cmd.PersistentFlags().StringVarP(&executionID, "execution-id", "e", "", "ID of the execution")
   151  	cmd.Flags().StringVar(&downloadDir, "download-dir", "artifacts", "download dir")
   152  	cmd.Flags().StringVar(&format, "format", "folder", "data format for storing files, one of folder|archive")
   153  	cmd.Flags().StringArrayVarP(&masks, "mask", "", []string{}, "regexp to filter downloaded files, single or comma separated, like report/.* or .*\\.json,.*\\.js$")
   154  
   155  	// output renderer flags
   156  	return cmd
   157  }
   158  
   159  func NewDownloadTestSuiteArtifactsCmd() *cobra.Command {
   160  	cmd := &cobra.Command{
   161  		Use:   "testsuite-artifacts <executionName>",
   162  		Short: "download test suite artifacts",
   163  		Args:  validator.ExecutionName,
   164  		Run: func(cmd *cobra.Command, args []string) {
   165  			executionID := args[0]
   166  			client, _, err := common.GetClient(cmd)
   167  			ui.ExitOnError("getting client", err)
   168  
   169  			testsuites.DownloadArtifacts(executionID, downloadDir, format, masks, client)
   170  		},
   171  	}
   172  
   173  	cmd.PersistentFlags().StringVarP(&client, "client", "c", "proxy", "Client used for connecting to testkube API one of proxy|direct|cluster")
   174  	cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "should I show additional debug messages")
   175  
   176  	cmd.PersistentFlags().StringVarP(&executionID, "execution-id", "e", "", "ID of the test suite execution")
   177  	cmd.Flags().StringVar(&downloadDir, "download-dir", "artifacts", "download dir")
   178  	cmd.Flags().StringVar(&format, "format", "folder", "data format for storing files, one of folder|archive")
   179  	cmd.Flags().StringArrayVarP(&masks, "mask", "", []string{}, "regexp to filter downloaded files, single or comma separated, like report/.* or .*\\.json,.*\\.js$")
   180  
   181  	// output renderer flags
   182  	return cmd
   183  }