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

     1  package storage
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"github.com/saucelabs/saucectl/internal/credentials"
     8  	"github.com/saucelabs/saucectl/internal/http"
     9  	"github.com/saucelabs/saucectl/internal/region"
    10  	"github.com/saucelabs/saucectl/internal/segment"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var (
    15  	appsClient http.AppStore
    16  )
    17  
    18  func Command(preRun func(cmd *cobra.Command, args []string)) *cobra.Command {
    19  	var regio string
    20  
    21  	cmd := &cobra.Command{
    22  		Use:              "storage",
    23  		Short:            "Interact with Sauce Storage",
    24  		SilenceUsage:     true,
    25  		TraverseChildren: true,
    26  		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    27  			if preRun != nil {
    28  				preRun(cmd, args)
    29  			}
    30  
    31  			reg := region.FromString(regio)
    32  			if reg == region.None {
    33  				return errors.New("invalid region")
    34  			}
    35  			if reg == region.Staging {
    36  				segment.DefaultTracker.Enabled = false
    37  			}
    38  
    39  			appsClient = *http.NewAppStore(reg.APIBaseURL(),
    40  				credentials.Get().Username, credentials.Get().AccessKey,
    41  				15*time.Minute)
    42  
    43  			return nil
    44  		},
    45  	}
    46  
    47  	flags := cmd.PersistentFlags()
    48  	flags.StringVarP(&regio, "region", "r", "us-west-1", "The Sauce Labs region. Options: us-west-1, eu-central-1.")
    49  
    50  	cmd.AddCommand(
    51  		ListCommand(),
    52  		UploadCommand(),
    53  		DownloadCommand(),
    54  		DeleteCommand(),
    55  	)
    56  
    57  	return cmd
    58  }