id_darwin.go 677 B

1234567891011121314151617181920212223242526272829
  1. // +build darwin
  2. package machineid
  3. import (
  4. "os/exec"
  5. )
  6. func getMachineID() (s string, err error) {
  7. var (
  8. buf []byte
  9. )
  10. extractID := func(lines string) (string, error) {
  11. for _, line := range strings.Split(lines, "\n") {
  12. if strings.Contains(line, "IOPlatformUUID") {
  13. parts := strings.SplitAfter(line, `" = "`)
  14. if len(parts) == 2 {
  15. return strings.TrimRight(parts[1], `"`), nil
  16. }
  17. }
  18. }
  19. return "", fmt.Errorf("Failed to extract 'IOPlatformUUID' value from `ioreg` output.\n%s", lines)
  20. }
  21. cmd := exec.Command("ioreg", "-rd", "-c", "IOPlatformExpertDevice")
  22. if buf, err = cmd.Output(); err == nil {
  23. s, err = extractID(string(buf))
  24. }
  25. return
  26. }