summaryrefslogtreecommitdiffstats
path: root/lbu-model.lua
blob: 8a362cec2a1af02c1d8b62a93c2844f3bbfcaf15 (plain)
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
module (..., package.seeall)

require "fs"
local configfile = "/etc/lbu/lbu.conf"

-- ################################################################################
-- LOCAL FUNCTIONS

local function get_version ()
	local f,error = io.popen("/sbin/lbu 2>&1")
	local programversion = f:read("*l")
	f:close()
	return programversion
end

local function getLbuStatus()
	local ret = {}
	local f = io.popen("/sbin/lbu status -v", "r")
	if not (f) then return ret end
	for line in f:lines() do
		if (string.match(line, "^Include files")) then break end
		if (string.match(line, "^Exclude files")) then break end
		local status, name = string.match(line, "^(%S+)%s+(.+)$")
		if (status) and (name) then
			ret[string.gsub('/' .. name, "/+", "/")] = status
		end
	end
	f:close()
	return ret
end


-- ################################################################################
-- PUBLIC FUNCTIONS

function getstatus ()
	local status = {}
	local statustxt = nil
	local lbustatus = list()
	if (#lbustatus == 0) then
		statustxt = "OK! (There is no uncommited files)"
	else
		statustxt = "WARNING! (Until you commit, you will loose your changes at next reboot/shutdown!)"
	end
	status["version"] = get_version()
	status["status"] = statustxt
	return status
end

function list(self)
	local ret = {}
	local lbuStatus = getLbuStatus()
	for k,v in pairs(lbuStatus) do
		ret[#ret + 1] = { name=k, status=v }
	end
	table.sort(ret, function(a,b) return (a.name < b.name) end)
	return ret
end

-- ################################################################################
-- OLD FUNCTIONS

--[[
function commit(self)
    local f = io.popen("/sbin/lbu commit", 'r')
    if not f then return false, "cannot run lbu" end
    local ret = f:read("*a")
    f:close()
    return true, ret
end
--]]