-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
utils.go
executable file
·153 lines (138 loc) · 4.15 KB
/
utils.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package ios
import (
"encoding/binary"
"errors"
"fmt"
"github.com/Masterminds/semver"
"os"
"strings"
log "github.com/sirupsen/logrus"
plist "howett.net/plist"
)
// ToPlist converts a given struct to a Plist using the
// github.com/DHowett/go-plist library. Make sure your struct is exported.
// It returns a string containing the plist.
func ToPlist(data interface{}) string {
return string(ToPlistBytes(data))
}
// ParsePlist tries to parse the given bytes, which should be a Plist, into a map[string]interface.
// It returns the map or an error if the decoding step fails.
func ParsePlist(data []byte) (map[string]interface{}, error) {
var result map[string]interface{}
_, err := plist.Unmarshal(data, &result)
return result, err
}
// ToPlistBytes converts a given struct to a Plist using the
// github.com/DHowett/go-plist library. Make sure your struct is exported.
// It returns a byte slice containing the plist.
func ToPlistBytes(data interface{}) []byte {
bytes, err := plist.Marshal(data, plist.XMLFormat)
if err != nil {
//this should not happen
panic(fmt.Sprintf("Failed converting to plist %v error:%v", data, err))
}
return bytes
}
func ToBinPlistBytes(data interface{}) []byte {
bytes, err := plist.Marshal(data, plist.BinaryFormat)
if err != nil {
//this should not happen
panic(fmt.Sprintf("Failed converting to plist %v error:%v", data, err))
}
return bytes
}
// Ntohs is a re-implementation of the C function Ntohs.
// it means networkorder to host oder and basically swaps
// the endianness of the given int.
// It returns port converted to little endian.
func Ntohs(port uint16) uint16 {
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, port)
return binary.LittleEndian.Uint16(buf)
}
// GetDevice returns:
// the device for the udid if a valid udid is provided.
// if the env variable 'udid' is specified, the device with that udid
// otherwise it returns the first device in the list.
func GetDevice(udid string) (DeviceEntry, error) {
if udid == "" {
udid = os.Getenv("udid")
if udid != "" {
log.Info("using udid from env.udid variable")
}
}
log.Debugf("Looking for device '%s'", udid)
deviceList, err := ListDevices()
if err != nil {
return DeviceEntry{}, err
}
if udid == "" {
if len(deviceList.DeviceList) == 0 {
return DeviceEntry{}, errors.New("no iOS devices are attached to this host")
}
log.WithFields(log.Fields{"udid": deviceList.DeviceList[0].Properties.SerialNumber}).
Info("no udid specified using first device in list")
return deviceList.DeviceList[0], nil
}
for _, device := range deviceList.DeviceList {
if device.Properties.SerialNumber == udid {
return device, nil
}
}
return DeviceEntry{}, fmt.Errorf("Device '%s' not found. Is it attached to the machine?", udid)
}
// PathExists is used to determine whether the path folder exists
// True if it exists, false otherwise
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func IOS14() *semver.Version {
return semver.MustParse("14.0")
}
func IOS12() *semver.Version {
return semver.MustParse("12.0")
}
func IOS11() *semver.Version {
return semver.MustParse("11.0")
}
// FixWindowsPaths replaces backslashes with forward slashes and removes the X: style
// windows drive letters
func FixWindowsPaths(path string) string {
path = strings.ReplaceAll(path, "\\", "/")
if strings.Contains(path, ":/") {
path = strings.Split(path, ":/")[1]
}
return path
}
func ByteCountDecimal(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%dB", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cB", float64(b)/float64(div), "kMGTPE"[exp])
}
// InterfaceToStringSlice casts an interface{} to []interface{} and then converts each entry to a string.
// It returns an empty slice in case of an error.
func InterfaceToStringSlice(intfSlice interface{}) []string {
slice, ok := intfSlice.([]interface{})
if !ok {
return []string{}
}
result := make([]string, len(slice))
for i, v := range slice {
result[i] = v.(string)
}
return result
}