From e5f5e54eec3e767766e884f012e3a82f2d971829 Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Sat, 23 Jul 2022 19:54:59 -0400 Subject: [PATCH] ENH make interface finder more robust --- src/sys.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sys.lua b/src/sys.lua index 5d3cec0..575572a 100644 --- a/src/sys.lua +++ b/src/sys.lua @@ -151,20 +151,20 @@ end -- ASSUME realpath exists (part of coreutils) +local NET_DIR = '/sys/class/net' + local get_interfaces = function() - local s = i_o.execute_cmd('realpath /sys/class/net/* | grep -v virtual') - local interfaces = {} - for iface in __string_gmatch(s, '/([^/\n]+)\n') do - interfaces[#interfaces + 1] = iface - end - return interfaces + local cmd = string.format('realpath %s/* | grep -v virtual', NET_DIR) + local s = i_o.execute_cmd(cmd) + local f = pure.partial(pure.gmatch_table, '/([^/\n]+)\n') + return pure.maybe({}, f, s) end M.get_net_interface_paths = function() local is = get_interfaces() return pure.map( function(s) - local dir = string.format('/sys/class/net/%s/statistics/', s) + local dir = string.format('%s/%s/statistics/', NET_DIR, s) return {rx = dir..'rx_bytes', tx = dir..'tx_bytes'} end, is