-
Notifications
You must be signed in to change notification settings - Fork 16
/
device.go
80 lines (69 loc) · 1.61 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package flags
import (
"encoding/csv"
"errors"
"fmt"
"strconv"
"strings"
"github.com/saucelabs/saucectl/internal/config"
"github.com/saucelabs/saucectl/internal/msg"
)
// Device represents the RDC device configuration.
type Device struct {
config.Device
Changed bool
}
// String returns a string represenation of the device.
func (d Device) String() string {
if !d.Changed {
return ""
}
return fmt.Sprintf("%+v", d.Device)
}
// Set sets the device to the values present in s.
// The input has to be a comma separated string in the format of "key=value,key2=value2".
// This method is called by cobra when CLI flags are parsed.
func (d *Device) Set(s string) error {
d.Changed = true
rec, err := csv.NewReader(strings.NewReader(s)).Read()
if err != nil {
return err
}
for _, v := range rec {
vs := strings.Split(v, "=")
if len(vs) < 2 {
msg.Error("--device must be specified using a key-value format, e.g. \"--device name=iPhone,private=true\"")
return errors.New(msg.InvalidKeyValueInputFormat)
}
val := vs[1]
switch vs[0] {
case "id":
d.ID = val
case "name":
d.Name = val
case "platformName":
d.PlatformName = val
case "platformVersion":
d.PlatformVersion = val
case "carrierConnectivity":
b, err := strconv.ParseBool(val)
if err != nil {
return err
}
d.Options.CarrierConnectivity = b
case "deviceType":
d.Options.DeviceType = val
case "private":
b, err := strconv.ParseBool(val)
if err != nil {
return err
}
d.Options.Private = b
}
}
return nil
}
// Type returns the value type.
func (d Device) Type() string {
return "device"
}