Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Oct 3, 2022
1 parent 6b2bfe4 commit dbc0f20
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
27 changes: 6 additions & 21 deletions disk/disk_windows.go
Expand Up @@ -79,21 +79,10 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return ret, nil
}

type errorCollector []error

func (c *errorCollector) collect(e error) { *c = append(*c, e) }

func (c *errorCollector) Error() (err string) {
err = ""
for i, e := range *c {
err += fmt.Sprintf("\tError %d: %s\n", i, e.Error())
}

return err
}

func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
collector := new(errorCollector)
warnings := common.Warnings{
Verbose: true,
}
var ret []PartitionStat
lpBuffer := make([]byte, 254)
diskret, _, err := procGetLogicalDriveStringsW.Call(
Expand All @@ -109,7 +98,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
typeret, _, _ := procGetDriveType.Call(uintptr(unsafe.Pointer(typepath)))
if typeret == 0 {
err := windows.GetLastError()
collector.collect(err)
warnings.Add(err)
continue
}
// 2: DRIVE_REMOVABLE 3: DRIVE_FIXED 4: DRIVE_REMOTE 5: DRIVE_CDROM
Expand All @@ -134,7 +123,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
if typeret == 5 || typeret == 2 {
continue // device is not ready will happen if there is no disk in the drive
}
collector.collect(err)
warnings.Add(err)
continue
}
opts := []string{"rw"}
Expand All @@ -155,11 +144,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
}
}
}
if len(*collector) == 0 {
return ret, nil
} else {
return ret, collector
}
return ret, warnings.Reference()
}

func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {
Expand Down
2 changes: 1 addition & 1 deletion host/host_linux.go
Expand Up @@ -395,7 +395,7 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
}
}

var warns Warnings
var warns common.Warnings

if len(files) == 0 { // handle distributions without hwmon, like raspbian #391, parse legacy thermal_zone files
files, err = filepath.Glob(common.HostSys("/class/thermal/thermal_zone*/"))
Expand Down
16 changes: 11 additions & 5 deletions host/types.go → internal/common/warnings.go
@@ -1,11 +1,10 @@
package host
package common

import (
"fmt"
)
import "fmt"

type Warnings struct {
List []error
List []error
Verbose bool
}

func (w *Warnings) Add(err error) {
Expand All @@ -20,5 +19,12 @@ func (w *Warnings) Reference() error {
}

func (w *Warnings) Error() string {
if w.Verbose {
str := ""
for i, e := range w.List {
str += fmt.Sprintf("\tError %d: %s\n", i, e.Error())
}
return str
}
return fmt.Sprintf("Number of warnings: %v", len(w.List))
}

0 comments on commit dbc0f20

Please sign in to comment.