From 3e3341ac1a9c86c5522f692c5815c67fd66c6334 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sat, 18 Oct 2025 15:01:57 -0400 Subject: [PATCH] Restore inner-loop error handling; optimize variable detection --- nut.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/nut.go b/nut.go index a8f605a..9a41766 100644 --- a/nut.go +++ b/nut.go @@ -36,6 +36,7 @@ func (c *Client) Close() error { return c.conn.Close() } +// GetListVar updates NutKeyValMap with the current status of all variables from . func (c *Client) GetListVar(upsID string) error { cmd := "LIST VAR \"" + upsID + "\"" if err := c.write(cmd); err != nil { @@ -50,13 +51,19 @@ func (c *Client) GetListVar(upsID string) error { return fmt.Errorf("pre-loop error: expected %q, got %q", expected, l) } +outer: for { - l, _ := c.read() - if !strings.HasPrefix(l, "VAR \""+upsID+"\" ") { - break + l, err := c.read() + if err != nil { + return fmt.Errorf("in-loop error: %w", err) } lSplit := strings.Split(l, " ") - NutKeyValMap[strings.Trim(lSplit[2], "\"")] = strings.Trim(strings.Join(lSplit[3:], " "), "\"") + switch lSplit[0] { + case "VAR": + NutKeyValMap[strings.Trim(lSplit[2], "\"")] = strings.Trim(strings.Join(lSplit[3:], " "), "\"") + default: + break outer + } } return nil }