From a10034c38281127a505798341a4c3b69a96bd2ed Mon Sep 17 00:00:00 2001 From: Peter Bakkum Date: Fri, 4 Nov 2022 15:42:12 -0700 Subject: [PATCH] Locale-safe ps output parsing --- top.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/top.go b/top.go index 354de39..36b66bb 100644 --- a/top.go +++ b/top.go @@ -76,6 +76,12 @@ type PsProcess struct { Command string } +func parsePerc(field string) (float64, error) { + // In some locales the ps output uses a `,` rather than a `.` + cleanField := strings.Replace(field, ",", ".", 64) + return strconv.ParseFloat(cleanField, 64) +} + func GetPsProcesses(ctx context.Context) ([]*PsProcess, error) { args := []string{"auxc"} out, err := commandWithContext(ctx, "ps", args...) @@ -98,12 +104,12 @@ func GetPsProcesses(ctx context.Context) ([]*PsProcess, error) { return nil, err } - cpuPerc, err := strconv.ParseFloat(fields[2], 64) + cpuPerc, err := parsePerc(fields[2]) if err != nil { return nil, err } - memPerc, err := strconv.ParseFloat(fields[3], 64) + memPerc, err := parsePerc(fields[3]) if err != nil { return nil, err }