github.com/saucelabs/saucectl@v0.175.1/internal/cmd/artifacts/upload.go (about)

     1  package artifacts
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  
     8  	cmds "github.com/saucelabs/saucectl/internal/cmd"
     9  	"github.com/saucelabs/saucectl/internal/http"
    10  	"github.com/saucelabs/saucectl/internal/segment"
    11  	"github.com/saucelabs/saucectl/internal/usage"
    12  	"github.com/schollz/progressbar/v3"
    13  	"github.com/spf13/cobra"
    14  	"golang.org/x/text/cases"
    15  	"golang.org/x/text/language"
    16  )
    17  
    18  func UploadCommand() *cobra.Command {
    19  	var out string
    20  
    21  	cmd := &cobra.Command{
    22  		Use:          "upload <jobID> <filename>",
    23  		Short:        "Uploads an artifact for the job.",
    24  		Long:         "Uploads an artifact for the job. Real Device job is not supported.",
    25  		SilenceUsage: true,
    26  		Args: func(cmd *cobra.Command, args []string) error {
    27  			if len(args) == 0 || args[0] == "" {
    28  				return errors.New("no job ID specified")
    29  			}
    30  			if len(args) == 1 || args[1] == "" {
    31  				return errors.New("no file name specified")
    32  			}
    33  
    34  			return nil
    35  		},
    36  		PreRunE: func(cmd *cobra.Command, args []string) error {
    37  			err := http.CheckProxy()
    38  			if err != nil {
    39  				return fmt.Errorf("invalid HTTP_PROXY value")
    40  			}
    41  
    42  			tracker := segment.DefaultTracker
    43  
    44  			go func() {
    45  				tracker.Collect(
    46  					cases.Title(language.English).String(cmds.FullName(cmd)),
    47  					usage.Properties{}.SetFlags(cmd.Flags()),
    48  				)
    49  				_ = tracker.Close()
    50  			}()
    51  			return nil
    52  		},
    53  		RunE: func(cmd *cobra.Command, args []string) error {
    54  			jobID := args[0]
    55  			filePath := args[1]
    56  			file, err := os.Open(filePath)
    57  			if err != nil {
    58  				return fmt.Errorf("failed to open file [%s]: %w", filePath, err)
    59  			}
    60  			finfo, err := file.Stat()
    61  			if err != nil {
    62  				return fmt.Errorf("failed to inspect file: %w", err)
    63  			}
    64  
    65  			bar := newProgressBar(out, finfo.Size(), "Uploading")
    66  			content, err := os.ReadFile(filePath)
    67  			if err != nil {
    68  				return fmt.Errorf("failed to read file: %w", err)
    69  			}
    70  
    71  			err = artifactSvc.Upload(jobID, finfo.Name(), content)
    72  			if err != nil {
    73  				return fmt.Errorf("failed to upload file: %w", err)
    74  			}
    75  			bar.Close()
    76  
    77  			switch out {
    78  			case "text":
    79  				fmt.Println("Success!")
    80  			case "json":
    81  				if err := renderJSON(filePath); err != nil {
    82  					return fmt.Errorf("failed to render output: %w", err)
    83  				}
    84  			default:
    85  				return errors.New("unknown output format")
    86  			}
    87  
    88  			return nil
    89  		},
    90  	}
    91  
    92  	flags := cmd.Flags()
    93  	flags.StringVarP(&out, "out", "o", "text", "Output out to the console. Options: text, json.")
    94  
    95  	return cmd
    96  }
    97  
    98  func newProgressBar(outputFormat string, size int64, description ...string) *progressbar.ProgressBar {
    99  	switch outputFormat {
   100  	case "text":
   101  		return progressbar.DefaultBytes(size, description...)
   102  	default:
   103  		return progressbar.DefaultBytesSilent(size, description...)
   104  	}
   105  }