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

     1  package docker
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"github.com/saucelabs/saucectl/internal/http"
     8  	"github.com/saucelabs/saucectl/internal/region"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	imageRunnerService        http.ImageRunner
    14  	imageRunnerServiceTimeout = 1 * time.Minute
    15  )
    16  
    17  func Command(preRun func(cmd *cobra.Command, args []string)) *cobra.Command {
    18  	var regio string
    19  
    20  	cmd := &cobra.Command{
    21  		Use:              "docker",
    22  		Short:            "Interact with Sauce Container Registry",
    23  		SilenceUsage:     true,
    24  		TraverseChildren: 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: must be one of [us-west-1, eu-central-1]")
    33  			}
    34  
    35  			creds := reg.Credentials()
    36  			url := reg.APIBaseURL()
    37  
    38  			asyncEventManager, err := http.NewAsyncEventMgr()
    39  			if err != nil {
    40  				return err
    41  			}
    42  
    43  			imageRunnerService = http.NewImageRunner(url, creds, imageRunnerServiceTimeout, asyncEventManager)
    44  
    45  			return nil
    46  		},
    47  	}
    48  
    49  	flags := cmd.PersistentFlags()
    50  	flags.StringVarP(&regio, "region", "r", "us-west-1", "The Sauce Labs region to login. Options: us-west-1, eu-central-1.")
    51  
    52  	cmd.AddCommand(
    53  		PushCommand(),
    54  	)
    55  
    56  	return cmd
    57  }