From f6898d463721542f1eedd0c1025a07d4ee75676e Mon Sep 17 00:00:00 2001 From: Sven Rebhan Date: Wed, 1 Nov 2023 11:51:30 +0100 Subject: [PATCH] Condense code --- host/host_linux.go | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/host/host_linux.go b/host/host_linux.go index 25ea3353d..6ae816137 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -490,44 +490,24 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err } // Add discovered temperature sensor to the list + optional := optionalProperties(filepath.Join(directory, basename)) temperatures = append(temperatures, TemperatureStat{ SensorKey: name, Temperature: temperature / hostTemperatureScale, - High: optionalValueReadFromFile(basepath+"_max") / hostTemperatureScale, - Critical: optionalValueReadFromFile(basepath+"_crit") / hostTemperatureScale, - Optional: optionalProperties(filepath.Join(directory, basename)), + High: optional["max"], + Critical: optional["crit"], + Optional: optional, }) } return temperatures, warns.Reference() } -func optionalValueReadFromFile(filename string) float64 { - var raw []byte - var err error - var value float64 - - // Check if file exists - if _, err := os.Stat(filename); os.IsNotExist(err) { - return 0 - } - - if raw, err = os.ReadFile(filename); err != nil { - return 0 - } - - if value, err = strconv.ParseFloat(strings.TrimSpace(string(raw)), 64); err != nil { - return 0 - } - - return value -} - func optionalProperties(basename string) map[string]float64 { // Determine all files with the base-prefix matches, err := filepath.Glob(basename + "_*") if err != nil { - return nil + return map[string]float64{} } // Collect the information from all files that are not already handled