From e15cb96069212ebe8a86c529822b7b4afb9834f7 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sat, 18 Oct 2025 16:27:30 -0400 Subject: [PATCH] Add "GetVar" function for checking the value of a specific variable (statelessly) --- nut.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/nut.go b/nut.go index 9a41766..6de6715 100644 --- a/nut.go +++ b/nut.go @@ -36,8 +36,8 @@ 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 { +// ListVar updates NutKeyValMap with the current status of all variables from . +func (c *Client) ListVar(upsID string) error { cmd := "LIST VAR \"" + upsID + "\"" if err := c.write(cmd); err != nil { return err @@ -68,6 +68,20 @@ outer: return nil } +// GetVar returns the value of the specified variable for . +func (c *Client) GetVar(upsID, varName string) (string, error) { + cmd := "GET VAR \"" + upsID + "\" \"" + varName + "\"" + if err := c.write(cmd); err != nil { + return "", err + } + l, err := c.read() + if err != nil { + return "", err + } + value := strings.Trim(strings.Join(strings.Split(l, " ")[3:], " "), "\"") + return value, nil +} + // newClient wraps an existing net.Conn. func newClient(conn net.Conn) *Client { return &Client{conn, bufio.NewReader(conn)}