id_darwin.go 713 B

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