helpers: add sysctl helper for freebsd

This commit is contained in:
mutlusun 2017-01-25 17:47:33 +01:00 committed by Jörg Thalheim
parent 3baade7b06
commit 78e1242601
No known key found for this signature in database
GPG Key ID: CA4106B8D7CC79FA
1 changed files with 38 additions and 0 deletions

View File

@ -196,6 +196,44 @@ function helpers.scroll(text, maxlen, widget)
end
-- }}}
-- {{{ Return result from one sysctl variable as string
function helpers.sysctl(path)
local fd = io.popen("sysctl -n " .. helpers.shellquote(path))
if not fd then
return
end
local ret = fd:read()
fd:close()
return ret
end
-- }}}
-- {{{ Return result from multiple sysctl variables as table
function helpers.sysctl_table(syspath)
return setmetatable({ _path = syspath },
{ __index = function(table, index)
local path = "sysctl -n " .. helpers.shellquote(table._path .. "." .. index)
local f = io.popen(path)
if f then
local s = f:read("*all")
f:close()
if select(2, s:gsub("\n", "\n")) > 1 then
local o = { _path = path}
setmetatable(o, getmetatable(table))
return o
else
return s
end
end
end
})
end
-- }}}
return helpers
-- }}}