summaryrefslogtreecommitdiffstats
path: root/.config/yay/hooks/install_log.lua
blob: e07183e54997418096545fbcfff3a8235f098e83 (plain) (blame)
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
-- Copypasted from https://github.com/Jguer/yay/blob/next/doc/examples/install_log.lua


local log_path = (os.getenv("XDG_STATE_HOME") or (os.getenv("HOME") .. "/.local/state")) .. "/yay/install.log"
local log_dir  = log_path:match("^(.+)/[^/]+$")

yay.create_autocmd("PostInstall", {
  desc = "append every installed/upgraded package to a persistent log",
  callback = function(event)
    yay.log.info("install_log: writing to ", log_path)   
    os.execute("mkdir -p " .. log_dir)
    local f, err = io.open(log_path, "a")
    if not f then
      yay.log.warn("install_log: cannot open log file: ", err)
      return
    end

    local ts = os.date("%Y-%m-%dT%H:%M:%S")

    for _, pkg in ipairs(event.data.packages) do
      local upgrade = pkg.local_version ~= ""
      local action = upgrade and "upgrade" or "install"
      local version_change = upgrade and (pkg.local_version .. " -> " .. pkg.version) or pkg.version
      f:write(string.format("%s  %-9s %-7s %-14s %-12s %s\n",
        ts, action, pkg.source, pkg.reason, pkg.name, version_change))
    end

    f:close()
  end,
})