Skip to content

Commit dbc0f20

Browse files
committed
code review
1 parent 6b2bfe4 commit dbc0f20

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

disk/disk_windows.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,10 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
7979
return ret, nil
8080
}
8181

82-
type errorCollector []error
83-
84-
func (c *errorCollector) collect(e error) { *c = append(*c, e) }
85-
86-
func (c *errorCollector) Error() (err string) {
87-
err = ""
88-
for i, e := range *c {
89-
err += fmt.Sprintf("\tError %d: %s\n", i, e.Error())
90-
}
91-
92-
return err
93-
}
94-
9582
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
96-
collector := new(errorCollector)
83+
warnings := common.Warnings{
84+
Verbose: true,
85+
}
9786
var ret []PartitionStat
9887
lpBuffer := make([]byte, 254)
9988
diskret, _, err := procGetLogicalDriveStringsW.Call(
@@ -109,7 +98,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
10998
typeret, _, _ := procGetDriveType.Call(uintptr(unsafe.Pointer(typepath)))
11099
if typeret == 0 {
111100
err := windows.GetLastError()
112-
collector.collect(err)
101+
warnings.Add(err)
113102
continue
114103
}
115104
// 2: DRIVE_REMOVABLE 3: DRIVE_FIXED 4: DRIVE_REMOTE 5: DRIVE_CDROM
@@ -134,7 +123,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
134123
if typeret == 5 || typeret == 2 {
135124
continue // device is not ready will happen if there is no disk in the drive
136125
}
137-
collector.collect(err)
126+
warnings.Add(err)
138127
continue
139128
}
140129
opts := []string{"rw"}
@@ -155,11 +144,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
155144
}
156145
}
157146
}
158-
if len(*collector) == 0 {
159-
return ret, nil
160-
} else {
161-
return ret, collector
162-
}
147+
return ret, warnings.Reference()
163148
}
164149

165150
func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {

host/host_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
395395
}
396396
}
397397

398-
var warns Warnings
398+
var warns common.Warnings
399399

400400
if len(files) == 0 { // handle distributions without hwmon, like raspbian #391, parse legacy thermal_zone files
401401
files, err = filepath.Glob(common.HostSys("/class/thermal/thermal_zone*/"))

host/types.go renamed to internal/common/warnings.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package host
1+
package common
22

3-
import (
4-
"fmt"
5-
)
3+
import "fmt"
64

75
type Warnings struct {
8-
List []error
6+
List []error
7+
Verbose bool
98
}
109

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

2221
func (w *Warnings) Error() string {
22+
if w.Verbose {
23+
str := ""
24+
for i, e := range w.List {
25+
str += fmt.Sprintf("\tError %d: %s\n", i, e.Error())
26+
}
27+
return str
28+
}
2329
return fmt.Sprintf("Number of warnings: %v", len(w.List))
2430
}

0 commit comments

Comments
 (0)