| 1 |
|
| 2 |
|
| 3 |
package machineid |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"strings" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
func 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 |
|
| 27 |
func 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 |
} |