1// +build darwin
2
3package machineid
4
5import (
6	"bytes"
7	"fmt"
8	"os"
9	"strings"
10)
11
12// machineID returns the uuid returned by `ioreg -rd1 -c IOPlatformExpertDevice`.
13// If there is an error running the commad an empty string is returned.
14func machineID() (string, error) {
15	buf := &bytes.Buffer{}
16	err := run(buf, os.Stderr, "ioreg", "-rd1", "-c", "IOPlatformExpertDevice")
17	if err != nil {
18		return "", err
19	}
20	id, err := extractID(buf.String())
21	if err != nil {
22		return "", err
23	}
24	return trim(id), nil
25}
26
27func extractID(lines string) (string, error) {
28	for _, line := range strings.Split(lines, "\n") {
29		if strings.Contains(line, "IOPlatformUUID") {
30			parts := strings.SplitAfter(line, `" = "`)
31			if len(parts) == 2 {
32				return strings.TrimRight(parts[1], `"`), nil
33			}
34		}
35	}
36	return "", fmt.Errorf("Failed to extract 'IOPlatformUUID' value from `ioreg` output.\n%s", lines)
37}
38