bliss

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

main.lua (2932B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
#!/usr/bin/env lua
--- Executable interface to the bliss library.
-- @script bliss
local bliss = require "bliss"

local function version()
    print(bliss.version)
end

local function usage()
    --- @usage
    local usage = [[bliss [a|b|c|d|i|l|r|s|u|U|v] [pkg]...
    alternatives List and swap alternatives
    build        Build packages
    checksum     Generate checksums
    download     Download sources
    install      Install packages
    list         List installed packages
    remove       Remove packages
    search       Search for packages
    update       Update the repositories
    upgrade      Update the system
    version      Package manager version]]
    print(usage)
    os.exit(true)
end

local function args(env, arg)
    local args_map = {
        a = bliss.alternatives,
        b = bliss.build,
        c = bliss.checksum,
        d = bliss.download,
        H = bliss.help_ext,
        i = bliss.install,
        l = bliss.list,
        r = bliss.remove,
        s = bliss.search,
        u = bliss.update,
        U = bliss.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

    if char == "i" or char == "a" or char == "r" then
        local user = bliss.am_not_owner(env.ROOT .. "/")
        if user then
            local newarg = {
                "env",
                "LOGNAME="..user,
                "HOME="..os.getenv("HOME"),
                "XDG_CACHE_HOME="..(os.getenv("XDG_CACHE_HOME") or ""),
                "KISS_COMPRESS="..env.COMPRESS,
                "KISS_PATH="..table.concat(env.PATH, ":"),
                "KISS_FORCE="..env.FORCE,
                "KISS_ROOT="..env.ROOT,
                "KISS_CHOICE="..env.CHOICE,
                "KISS_COLOR="..env.COLOR,
                "KISS_TMPDIR="..env.TMPDIR,
                "KISS_PID="..env.PID,
                "_KISS_LVL="..env._LVL,
            }
            table.move(arg, 0, #arg, #newarg+1, newarg)

            bliss.trap_off(env)
            bliss.as_user(env, user, newarg)
            bliss.trap_on(env)
            return
        end
    end

    -- shift
    table.remove(arg, 1)

    -- TODO: pkg_order
    -- TODO: prepend PWD to PATH if no args and action ~= list

    local f = args_map[char]
    if f then
        -- Run in protected mode. This is to catch bugs rather than user-facing
        -- error handling. None of the action functions return a value; we're
        -- only interested in if they fail or succeed.
        if not xpcall(f, function (msg)
            print(msg)
            print(debug.traceback())
        end, env, arg)
        then
            bliss.log("An exception was caught, not removing temporary files")
            bliss.trap_off(env)
            os.exit(1)
        end
    else
        -- TODO: ext
        usage()
    end
end

local env = bliss.setup()
args(env, arg)