Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for get disk serial number on darwin #1791

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

samburba
Copy link

Hello! This is my first PR on this project, so let me know if I got anything wrong and should change it.

This fixes #801 and adds support for getting (NVMe) drives' serial number. It does this by executing system_profiler and unmarshalling.

Copy link
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! I haven't tested it myself, but using the JSON output of system_profiler seems like a good approach. I've left a few comments, so I'd appreciate it if you could address them.

@@ -88,8 +91,60 @@ func getFsType(stat unix.Statfs_t) string {
return common.ByteToString(stat.Fstypename[:])
}

type SPNVMeDataTypeItem struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change to a private struct?

func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
cmd := exec.Command("system_profiler", "SPNVMeDataType", "-json")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change to use invoker.CommandWithContext() like here?

Comment on lines +130 to +133
err = json.Unmarshal(output, &temp)
if err != nil {
return "", fmt.Errorf("failed to unmarshal JSON: %w", err)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err = json.Unmarshal(output, &temp)
if err != nil {
return "", fmt.Errorf("failed to unmarshal JSON: %w", err)
}
if err := json.Unmarshal(output, &temp); err != nil {
return "", fmt.Errorf("failed to unmarshal JSON: %w", err)
}

Comment on lines +124 to +128
var temp struct {
SPNVMeDataType []struct {
Items []SPNVMeDataTypeItem `json:"_items"`
} `json:"SPNVMeDataType"`
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about defining the struct at the top level instead of dynamically defining it here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for getting disk serial number on darwin
2 participants