bliss

KISS in Lua
git clone git://bvnf.space/bliss.git
Log | Files | Refs | README | LICENSE

commit 904b6b3e11f2a1af162a14e1e5c9967596040ee6
parent 1c136e28f373ff2763626f7ced06e2cebf64761e
Author: aabacchus <ben@bvnf.space>
Date:   Mon, 12 Jun 2023 23:15:20 +0100

restructure

Diffstat:
Mkiss.lua | 60++++++++++++++++++++++++++++++------------------------------
Alibkiss/init.lua | 14++++++++++++++
Alibkiss/search.lua | 18++++++++++++++++++
Alibkiss/utils.lua | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest.lua | 14+++++++++++---
Dutils.lua | 110-------------------------------------------------------------------------------
6 files changed, 194 insertions(+), 143 deletions(-)

diff --git a/kiss.lua b/kiss.lua @@ -1,43 +1,40 @@ #!/usr/bin/env lua -local utils = require 'utils' ---local t = {__index = utils} ---setmetatable(_G, t) +local kiss = require 'libkiss' -local alternatives, build, checksum, download, help_ext, install, list, remove, update, upgrade, version - -function version() +local function version() print("0.0.0") end local function usage() - utils.log(arg[0] .. " [a|b|c|d|i|l|r|s|u|U|v] [pkg]...") - utils.log("alternatives List and swap alternatives") - utils.log("build Build packages") - utils.log("checksum Generate checksums") - utils.log("download Download sources") - utils.log("install Install packages") - utils.log("list List installed packages") - utils.log("remove Remove packages") - utils.log("search Search for packages") - utils.log("update Update the repositories") - utils.log("upgrade Update the system") - utils.log("version Package manager version") + kiss.log(arg[0] .. " [a|b|c|d|i|l|r|s|u|U|v] [pkg]...") + kiss.log("alternatives List and swap alternatives") + kiss.log("build Build packages") + kiss.log("checksum Generate checksums") + kiss.log("download Download sources") + kiss.log("install Install packages") + kiss.log("list List installed packages") + kiss.log("remove Remove packages") + kiss.log("search Search for packages") + kiss.log("update Update the repositories") + kiss.log("upgrade Update the system") + kiss.log("version Package manager version") os.exit(true) end local function args(arg) local args_map = { - a = alternatives, - b = build, - c = checksum, - d = download, - H = help_ext, - i = install, - l = list, - r = remove, - u = update, - U = upgrade, + a = kiss.alternatives, + b = kiss.build, + c = kiss.checksum, + d = kiss.download, + H = kiss.help_ext, + i = kiss.install, + l = kiss.list, + r = kiss.remove, + s = kiss.search, + u = kiss.update, + U = kiss.upgrade, v = version, } @@ -46,14 +43,17 @@ local function args(arg) local char = string.sub(arg[1], 1, 1) if arg[1] == "upgrade" then char = 'U' end + -- shift + table.remove(arg, 1) + local f = args_map[char] if f then - f() + f(env, arg) else -- TODO: ext usage() end end -local env = utils.setup() +env = kiss.setup() args(arg) diff --git a/libkiss/init.lua b/libkiss/init.lua @@ -0,0 +1,14 @@ +local cwd = (...):gsub('%.init$', '') + +local M = {} + +local names = {'utils', 'search'} +for i = 1, #names do + local name = names[i] + local t = require(cwd .. '.' .. name) + for k, v in pairs(t) do + M[k] = v + end +end + +return M diff --git a/libkiss/search.lua b/libkiss/search.lua @@ -0,0 +1,18 @@ +local cwd = (...):gsub('%.[^%.]+$', '') +local utils = require(cwd .. '.utils') + +local function search(env, arg) + + -- prepend pkg_db to search path + local path = utils.shallowcopy(env.PATH) + table.insert(path, 1, env.pkg_db) + + for _, repo in ipairs(path) do + print(repo) + end +end + +local M = { + search = search, +} +return M diff --git a/libkiss/utils.lua b/libkiss/utils.lua @@ -0,0 +1,121 @@ +-- Copyright 2023 phoebos + +local colors = {"", "", ""} +local setup, setup_colors, check_execute, get_available, split, log, warn, die, run, capture, shallowcopy + +function setup() + colors = setup_colors() + check_execute() + + local env = { + CHK = os.getenv("KISS_CHK") + or get_available("openssl", "sha256sum", "sha256", "shasum", "digest") + or warn("No sha256 utility found"), + COMPRESS= os.getenv("KISS_COMPRESS") or "gz", + DEBUG = os.getenv("KISS_DEBUG"), + FORCE = os.getenv("KISS_FORCE"), + GET = os.getenv("KISS_GET") + or get_available("aria2c", "axel", "curl", "wget", "wget2") + or warn("No download utility found (aria2c, axel, curl, wget, wget2"), + HOOK = split(os.getenv("KISS_HOOK"), ':'), + KEEPLOG = os.getenv("KISS_KEEPLOG"), + PATH = split(os.getenv("KISS_PATH"), ':'), + PID = os.getenv("KISS_PID") or nil, + PROMPT = os.getenv("KISS_PROMPT"), + ROOT = os.getenv("KISS_ROOT") or "", + SU = os.getenv("KISS_SU") or get_available("ssu", "sudo", "doas", "su"), + TMPDIR = os.getenv("KISS_TMPDIR"), + time = os.date("%Y-%m-%d-%H:%M"), + } + -- pkg_db depends on ROOT so must be set after env is constructed + env.pkg_db = env.ROOT .. "/var/db/kiss/installed" + return env +end + +function setup_colors() + local t = {} + if os.getenv("KISS_COLOR") ~= "0" then + t[1] = "\x1B[1;33m" + t[2] = "\x1B[1;34m" + t[3] = "\x1B[m" + end + return t +end + +function check_execute() + if not os.execute() then die("cannot execute shell commands") end +end + +function get_available(...) + local x, p, res + for i = 1, select('#', ...) do + x = select(i, ...) + res = capture("command -v " .. x) + if res[1] then return res[1] end + end + return nil +end + +function split(s, sep) + local c = {} + for a in s:gmatch("[^%s"..sep.."]+") do + table.insert(c, a) + end + return c +end + +function log(name, msg, category) + -- This is a direct translation of kiss's log(). Quite hacky. + io.stderr:write(string.format("%s%s %s%s%s %s\n", + colors[1], + category or "->", + colors[3] .. (msg and colors[2] or ''), + name, + colors[3], + msg or "")) +end + +function warn(name, msg) + log(name, msg, "WARNING") +end + +function die(name, msg) + log(name, msg, "ERROR") + os.exit(false) +end + +function run(cmd) + io.stderr:write(cmd.."\n") + local res, ty, code + -- faster to use fork + posix.unistd.execp? + res, ty, code = os.execute(cmd) + return res +end + +-- Returns an array of lines printed by cmd +function capture(cmd) + local p = io.popen(cmd, 'r') + local res = {} + for line in p:lines() do + table.insert(res, line) + end + return res +end + +function shallowcopy(t) + local u = {} + for k,v in pairs(t) do u[k] = v end + return t +end + +local M = { + setup = setup, + log = log, + warn = warn, + die = die, + run = run, + capture = capture, + shallowcopy = shallowcopy, +} + +return M diff --git a/test.lua b/test.lua @@ -1,4 +1,12 @@ -local utils = require "utils" +kiss = require "libkiss" -local env = utils.setup() -for k,v in pairs(env) do print(k,v) end +function tts(t) local s = "{ " local sep = "" for k,v in pairs(t) do s = s..sep..k.."="..v sep = ', ' end return s .. ' }' end + +env = kiss.setup() +for k,v in pairs(env) do + if "table" == type(v) then + print(k, tts(v)) + else + print(k,v) + end +end diff --git a/utils.lua b/utils.lua @@ -1,110 +0,0 @@ --- Copyright 2023 phoebos - --- vars -local colors --- funcs -local setup, setup_colors, check_execute, get_available, log, warn, die, run, capture - -function setup() - colors = setup_colors() - check_execute() - - local env = { - CHK = os.getenv("KISS_CHK") - or get_available("openssl", "sha256sum", "sha256", "shasum", "digest") - or warn("No sha256 utility found"), - COMPRESS= os.getenv("KISS_COMPRESS") or "gz", - DEBUG = os.getenv("KISS_DEBUG"), - FORCE = os.getenv("KISS_FORCE"), - GET = os.getenv("KISS_GET") - or get_available("aria2c", "axel", "curl", "wget", "wget2") - or warn("No download utility found (aria2c, axel, curl, wget, wget2"), - HOOK = os.getenv("KISS_HOOK"), - KEEPLOG = os.getenv("KISS_KEEPLOG"), - PATH = os.getenv("KISS_PATH"), - PID = os.getenv("KISS_PID") or nil, - PROMPT = os.getenv("KISS_PROMPT"), - ROOT = os.getenv("KISS_ROOT") or "", - SU = os.getenv("KISS_SU") or get_available("ssu", "sudo", "doas", "su"), - TMPDIR = os.getenv("KISS_TMPDIR"), - time = os.date("%Y-%m-%d-%H:%M"), - } - -- pkg_db depends on ROOT so must be set after env is constructed - env.pkg_db = env.ROOT .. "/var/db/kiss/installed" - return env -end - -function setup_colors() - local t = {"", "", ""} - if os.getenv("KISS_COLOR") ~= "0" then - t[1] = "\x1B[1;33m" - t[2] = "\x1B[1;34m" - t[3] = "\x1B[m" - end - return t -end - -function check_execute() - if not os.execute() then die("cannot execute shell commands") end -end - -function get_available(...) - local x, p, res - for i = 1, select('#', ...) do - x = select(i, ...) - res = capture("command -v " .. x) - if res[1] then return res[1] end - end - return nil -end - --- utilities - -function log(name, msg, category) - -- This is a direct translation of kiss's log(). Quite hacky. - io.stderr:write(string.format("%s%s %s%s%s %s\n", - colors[1], - category or "->", - colors[3] .. (msg and colors[2] or ''), - name, - colors[3], - msg or "")) -end - -function warn(name, msg) - log(name, msg, "WARNING") -end - -function die(name, msg) - log(name, msg, "ERROR") - os.exit(false) -end - -function run(cmd) - io.stderr:write(cmd.."\n") - local res, ty, code - -- faster to use fork + posix.unistd.execp? - res, ty, code = os.execute(cmd) - return res -end - --- Returns an array of lines printed by cmd -function capture(cmd) - local p = io.popen(cmd, 'r') - local res = {} - for line in p:lines() do - table.insert(res, line) - end - return res -end - -local P = { - setup = setup, - log = log, - warn = warn, - die = die, - run = run, - capture = capture, -} - -return P