-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils_darwin.go
More file actions
58 lines (49 loc) · 1.15 KB
/
utils_darwin.go
File metadata and controls
58 lines (49 loc) · 1.15 KB
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
//go:build darwin
// +build darwin
package system
import (
"bytes"
"fmt"
"os/exec"
"strings"
"sync"
)
var execLock sync.Mutex
func getMachineID() (string, error) {
out, err := execCmd("ioreg", "-rd1", "-c", "IOPlatformExpertDevice")
if err != nil {
return "", err
}
id, err := extractID(out)
if err != nil {
return "", err
}
return strings.TrimSpace(strings.Trim(id, "\n")), nil
}
func extractID(lines string) (string, error) {
const uuidParamName = "IOPlatformUUID"
for _, line := range strings.Split(lines, "\n") {
if strings.Contains(line, uuidParamName) {
parts := strings.SplitAfter(line, `" = "`)
if len(parts) == 2 {
return strings.TrimRight(parts[1], `"`), nil
}
}
}
return "", fmt.Errorf("failed to extract the '%s' value from the `ioreg` output", uuidParamName)
}
func execCmd(scmd string, args ...string) (string, error) {
execLock.Lock()
defer execLock.Unlock()
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command(scmd, args...)
cmd.Stdin = strings.NewReader("")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", err
}
return stdout.String(), nil
}