-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
utils.go
executable file
·68 lines (61 loc) · 2.17 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
package ios
import (
"encoding/binary"
"errors"
"fmt"
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 {
log.Fatal("Failed converting to plist", 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 udid. If udid equals emptystring, it returns the first device in the list.
func GetDevice(udid string) (DeviceEntry, error) {
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}).Debug("no udid specified using default")
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)
}