bliss

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

commit 1c136e28f373ff2763626f7ced06e2cebf64761e
Author: aabacchus <ben@bvnf.space>
Date:   Fri, 21 Apr 2023 14:01:39 +0100

init

Diffstat:
AREADME | 29+++++++++++++++++++++++++++++
Akiss.lua | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest.lua | 4++++
Autils.lua | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 202 insertions(+), 0 deletions(-)

diff --git a/README b/README @@ -0,0 +1,29 @@ +An implementation of the kiss package manager in Lua. + +[ ] alternatives +[ ] build +[ ] hooks +[ ] checksum +[ ] download +[ ] install +[ ] list +[ ] remove +[ ] search +[ ] update +[ ] upgrade +[x] version +[ ] ext + +Why Lua? +-------- + +Lua ... + - shares many goals with KISS, such as simplicity and efficiency. + - offers advantages over shell as a "proper" programming language. + - can easily be extended by code written in C (etc). + - is relatively fast. + +Dependencies +------------ + +lua-posix library (https://github.com/luaposix/luaposix) diff --git a/kiss.lua b/kiss.lua @@ -0,0 +1,59 @@ +#!/usr/bin/env lua +local utils = require 'utils' +--local t = {__index = utils} +--setmetatable(_G, t) + +local alternatives, build, checksum, download, help_ext, install, list, remove, update, upgrade, version + +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") + + 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, + v = version, + } + + if #arg < 1 then usage() end + + local char = string.sub(arg[1], 1, 1) + if arg[1] == "upgrade" then char = 'U' end + + local f = args_map[char] + if f then + f() + else + -- TODO: ext + usage() + end +end + +local env = utils.setup() +args(arg) diff --git a/test.lua b/test.lua @@ -0,0 +1,4 @@ +local utils = require "utils" + +local env = utils.setup() +for k,v in pairs(env) do print(k,v) end diff --git a/utils.lua b/utils.lua @@ -0,0 +1,110 @@ +-- 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