github.com/saucelabs/saucectl@v0.175.1/internal/flags/emulator.go (about)

     1  package flags
     2  
     3  import (
     4  	"encoding/csv"
     5  	"errors"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/saucelabs/saucectl/internal/config"
    10  	"github.com/saucelabs/saucectl/internal/msg"
    11  )
    12  
    13  // Emulator represents the emulator configuration.
    14  type Emulator struct {
    15  	config.Emulator
    16  	Changed bool
    17  }
    18  
    19  // String returns a string represenation of the emulator.
    20  func (e Emulator) String() string {
    21  	if !e.Changed {
    22  		return ""
    23  	}
    24  	return fmt.Sprintf("%+v", e.Emulator)
    25  }
    26  
    27  // Set sets the emulator to the values present in s.
    28  // The input has to be a comma separated string in the format of "key=value,key2=value2".
    29  // This method is called by cobra when CLI flags are parsed.
    30  func (e *Emulator) Set(s string) error {
    31  	e.Changed = true
    32  
    33  	rec, err := csv.NewReader(strings.NewReader(s)).Read()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	// TODO consider defaulting this in a common place (between config and CLI flags)
    39  	e.PlatformName = "Android"
    40  
    41  	for _, v := range rec {
    42  		vs := strings.Split(v, "=")
    43  
    44  		if len(vs) < 2 {
    45  			msg.Error("--emulator must be specified using a key-value format, e.g. \"--emulator name=Android Emulator,platformVersion=11.0\"")
    46  			return errors.New(msg.InvalidKeyValueInputFormat)
    47  		}
    48  
    49  		val := vs[1]
    50  		switch vs[0] {
    51  		case "name":
    52  			e.Name = val
    53  		case "orientation":
    54  			e.Orientation = val
    55  		case "platformVersion":
    56  			e.PlatformVersions = []string{val}
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  // Type returns the value type.
    64  func (e Emulator) Type() string {
    65  	return "emulator"
    66  }