-
Notifications
You must be signed in to change notification settings - Fork 143
/
id_darwin.go
37 lines (33 loc) · 885 Bytes
/
id_darwin.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
// +build darwin
package machineid
import (
"bytes"
"fmt"
"os"
"strings"
)
// machineID returns the uuid returned by `ioreg -rd1 -c IOPlatformExpertDevice`.
// If there is an error running the commad an empty string is returned.
func machineID() (string, error) {
buf := &bytes.Buffer{}
err := run(buf, os.Stderr, "ioreg", "-rd1", "-c", "IOPlatformExpertDevice")
if err != nil {
return "", err
}
id, err := extractID(buf.String())
if err != nil {
return "", err
}
return trim(id), nil
}
func extractID(lines string) (string, error) {
for _, line := range strings.Split(lines, "\n") {
if strings.Contains(line, "IOPlatformUUID") {
parts := strings.SplitAfter(line, `" = "`)
if len(parts) == 2 {
return strings.TrimRight(parts[1], `"`), nil
}
}
}
return "", fmt.Errorf("Failed to extract 'IOPlatformUUID' value from `ioreg` output.\n%s", lines)
}