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

     1  package ini
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/rs/zerolog/log"
     8  	cmds "github.com/saucelabs/saucectl/internal/cmd"
     9  	"github.com/saucelabs/saucectl/internal/config"
    10  	"github.com/saucelabs/saucectl/internal/espresso"
    11  	"github.com/saucelabs/saucectl/internal/segment"
    12  	"github.com/saucelabs/saucectl/internal/usage"
    13  	"github.com/spf13/cobra"
    14  	"golang.org/x/text/cases"
    15  	"golang.org/x/text/language"
    16  )
    17  
    18  func EspressoCmd() *cobra.Command {
    19  	cfg := &initConfig{
    20  		frameworkName: espresso.Kind,
    21  	}
    22  
    23  	cmd := &cobra.Command{
    24  		Use:          "espresso",
    25  		Short:        "Bootstrap an Espresso project.",
    26  		SilenceUsage: true,
    27  		Run: func(cmd *cobra.Command, args []string) {
    28  			tracker := segment.DefaultTracker
    29  
    30  			go func() {
    31  				tracker.Collect(
    32  					cases.Title(language.English).String(cmds.FullName(cmd)),
    33  					usage.Properties{}.SetFlags(cmd.Flags()),
    34  				)
    35  				_ = tracker.Close()
    36  			}()
    37  
    38  			err := Run(cmd, cfg)
    39  			if err != nil {
    40  				log.Err(err).Msg("failed to execute init command")
    41  				os.Exit(1)
    42  			}
    43  		},
    44  	}
    45  
    46  	cmd.Flags().StringVar(&cfg.app, "app", "", "Path to application under test.")
    47  	cmd.Flags().StringVar(&cfg.testApp, "test-app", "", "Path to test application.")
    48  	cmd.Flags().StringSliceVar(&cfg.otherApps, "other-apps", []string{}, "Path to additional applications.")
    49  	cmd.Flags().StringVar(&cfg.artifactWhenStr, "artifacts-when", "fail", "When to download artifacts.")
    50  	cmd.Flags().Var(&cfg.emulatorFlag, "emulator", "Android emulator to use for testing.")
    51  	cmd.Flags().Var(&cfg.deviceFlag, "device", "Real device to use for testing.")
    52  	return cmd
    53  }
    54  
    55  func configureEspresso(cfg *initConfig) interface{} {
    56  	var devices []config.Device
    57  	var emulators []config.Emulator
    58  
    59  	if !noPrompt || cfg.emulatorFlag.Changed {
    60  		emulators = append(emulators, cfg.emulator)
    61  	}
    62  	if !noPrompt || cfg.deviceFlag.Changed {
    63  		devices = append(devices, cfg.device)
    64  	}
    65  
    66  	return espresso.Project{
    67  		TypeDef: config.TypeDef{
    68  			APIVersion: espresso.APIVersion,
    69  			Kind:       espresso.Kind,
    70  		},
    71  		Sauce: config.SauceConfig{
    72  			Region:      cfg.region,
    73  			Concurrency: cfg.concurrency,
    74  		},
    75  		Espresso: espresso.Espresso{
    76  			App:       cfg.app,
    77  			TestApp:   cfg.testApp,
    78  			OtherApps: cfg.otherApps,
    79  		},
    80  		Suites: []espresso.Suite{
    81  			{
    82  				Name:      fmt.Sprintf("espresso - %s - %s", cfg.device.Name, cfg.emulator.Name),
    83  				Devices:   devices,
    84  				Emulators: emulators,
    85  			},
    86  		},
    87  		Artifacts: config.Artifacts{
    88  			Download: config.ArtifactDownload{
    89  				When:      cfg.artifactWhen,
    90  				Match:     []string{"*"},
    91  				Directory: "artifacts",
    92  			},
    93  		},
    94  	}
    95  }