github.com/saucelabs/saucectl@v0.175.1/internal/cmd/apit/cmd.go (about) 1 package apit 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 apitesterClient http.APITester 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: "apit", 23 Short: "Commands for interacting with API Testing", 24 SilenceUsage: true, 25 PersistentPreRunE: func(cmd *cobra.Command, args []string) error { 26 if preRun != nil { 27 preRun(cmd, args) 28 } 29 30 reg := region.FromString(regio) 31 if reg == region.None { 32 return errors.New("invalid region") 33 } 34 if reg == region.Staging { 35 segment.DefaultTracker.Enabled = false 36 } 37 38 creds := credentials.Get() 39 url := reg.APIBaseURL() 40 41 apitesterClient = http.NewAPITester(url, creds.Username, creds.AccessKey, 15*time.Minute) 42 43 return nil 44 }, 45 } 46 47 flags := cmd.PersistentFlags() 48 flags.StringVarP(®io, "region", "r", "us-west-1", "The Sauce Labs region. Options: us-west-1, eu-central-1.") 49 50 cmd.AddCommand( 51 VaultCommand(cmd.PersistentPreRunE), 52 ) 53 return cmd 54 }