Implement UniqueID on Darwin#31
Conversation
andrewkroh
left a comment
There was a problem hiding this comment.
Thank you for adding this.
| ret, err := C.gethostuuid(&id[0], &wait) | ||
| if ret == 0 && err == nil { | ||
| bytes := C.GoBytes(unsafe.Pointer(&id[0]), C.int(size)) | ||
| h.info.UniqueID = hex.EncodeToString(bytes) |
There was a problem hiding this comment.
Is this the same UUID that I get when I look at "About This Mac" -> "System Report" -> "Hardware UUID" (or ioreg -rd1 -c IOPlatformExpertDevice | grep IOPlatformUUID)?
If so I think people will find it useful if we report the value as a UUID (probably using uuid_unparse) since then they can correlate the ID with asset tracking tools that know the hardware ID.
There was a problem hiding this comment.
It is. I've added uuid_unparse_upper to have it look exactly the same as it does on macOS.
| wait := C.struct_timespec{5, 0} // 5 seconds | ||
|
|
||
| ret, err := C.gethostuuid(&id[0], &wait) | ||
| if ret == 0 && err == nil { |
There was a problem hiding this comment.
Most of the time in Go we handle the error case immediately after the function call then return.
if ret != 0 {
if err != nil {
// The errno should be set if rtn is non-zero to detail
// what went wrong (like EWOULDBLOCK).
return errors.Wrap(err, "gethostuuid failed")
}
// It's still an error even if the errno information wasn't set.
// Note the usage of errors rather than fmt b/c it adds stack
// context to the error that can be accessed later.
// https://godoc.org/github.com/pkg/errors#hdr-Formatted_printing_of_errors
return errors.Errorf("gethostuuid failed")
}
// Now make sense of the uuid_t bits.
|
Even though all three platforms (windows, linux, darwin) have UUIDs as their UniqueIDs they would each be displayed in a different way:
Would it make sense to unify? |
andrewkroh
left a comment
There was a problem hiding this comment.
LGTM. Can you please add that one godoc.
Regarding normalizing, I think leaving them as close to their "native" format will be beneficial to users of the library.
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| func MachineID() (string, error) { |
There was a problem hiding this comment.
Can you add godocs for this that explain that it returns the hardware ID.
Implements #29.