diff options
-rw-r--r-- | ensure.lua | 7 | ||||
-rw-r--r-- | ensure/engine.lua | 37 | ||||
-rw-r--r-- | ensure/engines/apt.lua | 15 | ||||
-rw-r--r-- | ensure/engines/pacman.lua | 15 |
4 files changed, 70 insertions, 4 deletions
@@ -1,10 +1,9 @@ #!/usr/bin/env lua local l = require "ensure.list" +local e = require "ensure.engine" local loud = { identifier = l.default.identifier, invalid = function (_, name) io.stderr:write(string.format("invalid package name: %q\n", name)) end, } -local p = l.all(arg[1] or os.getenv("ENSURE") or "/etc/ensure.d", loud) -for _, name in pairs(p) do - print(name) -end +local path = arg[1] or os.getenv("ENSURE") or "/etc/ensure.d" +e.install(e.check(l.all(path, loud))) diff --git a/ensure/engine.lua b/ensure/engine.lua new file mode 100644 index 0000000..ccaa18b --- /dev/null +++ b/ensure/engine.lua @@ -0,0 +1,37 @@ +local path = require "pl.path" +local engine = {} + + +function engine.detect () + local function detect (pathnames) + for name, pathname in pairs(pathnames) do + if path.isfile(pathname) then + return name + end + end + end + + local name = detect { + pacman="/usr/bin/pacman", -- TODO: Use which/PATH? + apt="/usr/bin/apt", + } + engine.loaded = require("ensure.engines." .. name) + return engine.loaded +end + + +local function loaded (target, name) + target[name] = function (...) + if not engine.loaded then + engine.detect() + end + return engine.loaded[name](...) + end +end + + +loaded(engine, "check") +loaded(engine, "install") + + +return engine diff --git a/ensure/engines/apt.lua b/ensure/engines/apt.lua new file mode 100644 index 0000000..82f7188 --- /dev/null +++ b/ensure/engines/apt.lua @@ -0,0 +1,15 @@ +local apt = {} +local unpack = table.unpack or unpack + + +function apt.check (packages) + return packages +end + + +function apt.install (packages) + print("apt", "install", unpack(packages)) +end + + +return apt diff --git a/ensure/engines/pacman.lua b/ensure/engines/pacman.lua new file mode 100644 index 0000000..21674f8 --- /dev/null +++ b/ensure/engines/pacman.lua @@ -0,0 +1,15 @@ +local pacman = {} +local unpack = table.unpack or unpack + + +function pacman.check (packages) + return packages +end + + +function pacman.install (packages) + print("pacman", "-S", unpack(packages)) +end + + +return pacman |