aboutsummaryrefslogtreecommitdiffstats
path: root/lib/data.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/data.lua')
-rw-r--r--lib/data.lua25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/data.lua b/lib/data.lua
index 98c6898..4803f5b 100644
--- a/lib/data.lua
+++ b/lib/data.lua
@@ -378,4 +378,29 @@ function M.board_name(vendor, name)
return name or vendor
end
+-- The IPv4 address from `ip -4 addr show <iface>` output.
+--
+-- Split from the command that produces it so it can be tested against a
+-- fixture: every other parser in this file follows the same split, and an
+-- address is exactly the kind of value that must not be hardcoded in a test.
+function M.iface_addr_parse(text)
+ if type(text) ~= 'string' then return nil end
+ return text:match('inet%s+(%d+%.%d+%.%d+%.%d+)')
+end
+
+-- The interface's IPv4 address, or nil when it has none.
+--
+-- This shells out, which the draw hook otherwise never does, so the caller
+-- memoizes it: an address does not change without an event this dashboard
+-- does not watch. Retried while nil, because a bridge may not be up when
+-- conky starts.
+function M.iface_addr(iface)
+ if type(iface) ~= 'string' then return nil end
+ local p = io.popen('ip -4 addr show ' .. ("%q"):format(iface) .. ' 2>/dev/null')
+ if not p then return nil end
+ local out = p:read('*a')
+ p:close()
+ return M.iface_addr_parse(out)
+end
+
return M