1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-02 22:17:08 +08:00

Merge pull request #675 from marcv81/fix_665_v2

Fix for #665
This commit is contained in:
shirou 2019-06-01 10:28:14 +09:00 committed by GitHub
commit d8686bcd5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 3 deletions

View File

@ -635,6 +635,7 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
return temperatures, err return temperatures, err
} }
} }
var warns Warnings
// example directory // example directory
// device/ temp1_crit_alarm temp2_crit_alarm temp3_crit_alarm temp4_crit_alarm temp5_crit_alarm temp6_crit_alarm temp7_crit_alarm // device/ temp1_crit_alarm temp2_crit_alarm temp3_crit_alarm temp4_crit_alarm temp5_crit_alarm temp6_crit_alarm temp7_crit_alarm
@ -660,16 +661,19 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
// Get the name of the temperature you are reading // Get the name of the temperature you are reading
name, err := ioutil.ReadFile(filepath.Join(filepath.Dir(file), "name")) name, err := ioutil.ReadFile(filepath.Join(filepath.Dir(file), "name"))
if err != nil { if err != nil {
return temperatures, err warns.Add(err)
continue
} }
// Get the temperature reading // Get the temperature reading
current, err := ioutil.ReadFile(file) current, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
return temperatures, err warns.Add(err)
continue
} }
temperature, err := strconv.ParseFloat(strings.TrimSpace(string(current)), 64) temperature, err := strconv.ParseFloat(strings.TrimSpace(string(current)), 64)
if err != nil { if err != nil {
warns.Add(err)
continue continue
} }
@ -679,5 +683,5 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
Temperature: temperature / 1000.0, Temperature: temperature / 1000.0,
}) })
} }
return temperatures, nil return temperatures, warns.Reference()
} }

25
host/types.go Normal file
View File

@ -0,0 +1,25 @@
package host
import (
"fmt"
)
type Warnings struct {
List []error
}
func (w *Warnings) Add(err error) {
w.List = append(w.List, err)
}
func (w *Warnings) Reference() error {
if len(w.List) > 0 {
return w
} else {
return nil
}
}
func (w *Warnings) Error() string {
return fmt.Sprintf("Number of warnings: %v", len(w.List))
}