Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
samburba committed Mar 1, 2025
1 parent 15c5b67 commit 409e7d2
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions disk/disk_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func getFsType(stat unix.Statfs_t) string {
return common.ByteToString(stat.Fstypename[:])
}

type SPNVMeDataTypeItem struct {
type spnvmeDataTypeItem struct {
Name string `json:"_name"`
BsdName string `json:"bsd_name"`
DetachableDrive string `json:"detachable_drive"`
Expand All @@ -113,29 +113,27 @@ type SPNVMeDataTypeItem struct {
} `json:"volumes"`
}

func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
cmd := exec.Command("system_profiler", "SPNVMeDataType", "-json")
type spnvmeDataWrapper struct {
SPNVMeDataType []struct {
Items []spnvmeDataTypeItem `json:"_items"`
} `json:"SPNVMeDataType"`
}

output, err := cmd.Output()
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
output, err := invoke.CommandWithContext(ctx, "system_profiler", "SPNVMeDataType", "-json")
if err != nil {
return "", err
}

var temp struct {
SPNVMeDataType []struct {
Items []SPNVMeDataTypeItem `json:"_items"`
} `json:"SPNVMeDataType"`
}

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

// Extract all serial numbers into a single string
var serialNumbers []string
for _, data := range temp.SPNVMeDataType {
for _, item := range data.Items {
for _, spnvmeData := range data.SPNVMeDataType {
for _, item := range spnvmeData.Items {
serialNumbers = append(serialNumbers, item.DeviceSerial)
}
}
Expand Down

0 comments on commit 409e7d2

Please sign in to comment.