summaryrefslogtreecommitdiffhomepage
path: root/libs
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2010-02-28 23:38:22 +0000
committerJo-Philipp Wich <jow@openwrt.org>2010-02-28 23:38:22 +0000
commit8338e5e8f5984b99add8bd30d0060e44bcdb915f (patch)
treed7e919e74a0e954fbdb1c671c7d5e85016ef613c /libs
parent979fa033eab95320eda0b18ad6e4af0c67397dd6 (diff)
libs/ipkg: add callback based list_all() and list_installed()
Diffstat (limited to 'libs')
-rw-r--r--libs/ipkg/luasrc/model/ipkg.lua49
1 files changed, 47 insertions, 2 deletions
diff --git a/libs/ipkg/luasrc/model/ipkg.lua b/libs/ipkg/luasrc/model/ipkg.lua
index 382fc8f8f..607c3638e 100644
--- a/libs/ipkg/luasrc/model/ipkg.lua
+++ b/libs/ipkg/luasrc/model/ipkg.lua
@@ -20,6 +20,7 @@ local util = require "luci.util"
local type = type
local pairs = pairs
local error = error
+local table = table
local ipkg = "opkg"
@@ -30,8 +31,7 @@ module "luci.model.ipkg"
-- Internal action function
local function _action(cmd, ...)
local pkg = ""
- arg.n = nil
- for k, v in pairs(arg) do
+ for k, v in pairs({...}) do
pkg = pkg .. " '" .. v:gsub("'", "") .. "'"
end
@@ -147,3 +147,48 @@ end
function upgrade()
return _action("upgrade")
end
+
+-- List helper
+function _list(action, pat, cb)
+ local fd = io.popen(ipkg .. " " .. action .. (pat and " '*" .. pat:gsub("'", "") .. "*'" or ""))
+ if fd then
+ local name, version, desc
+ while true do
+ local line = fd:read("*l")
+ if not line then break end
+
+ if line:sub(1,1) ~= " " then
+ name, version, desc = line:match("^(.-) %- (.-) %- (.+)")
+
+ if not name then
+ name, version = line:match("^(.-) %- (.+)")
+ desc = ""
+ end
+
+ cb(name, version, desc)
+
+ name = nil
+ version = nil
+ desc = nil
+ end
+ end
+
+ fd:close()
+ end
+end
+
+--- List all packages known to opkg.
+-- @param pat Only find packages matching this pattern, nil lists all packages
+-- @param cb Callback function invoked for each package, receives name, version and description as arguments
+-- @return nothing
+function list_all(pat, cb)
+ _list("list", pat, cb)
+end
+
+--- List installed packages.
+-- @param pat Only find packages matching this pattern, nil lists all packages
+-- @param cb Callback function invoked for each package, receives name, version and description as arguments
+-- @return nothing
+function list_installed(pat, cb)
+ _list("list_installed", pat, cb)
+end