summaryrefslogtreecommitdiffstats
path: root/app/cfgfile-model.lua
blob: 281ee2c455c3eb13850e538623fa41f6faa98743 (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
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
module (..., package.seeall)

require "fs"


--TODO this should be somehow figureoutable from acf info
local cfgdir = "/usr/share/acf/app/cfgfile"

--TODO, in fs actually, find should use coroutines instead of reading
--all in table

local files = nil

--TODO might on demand load only part of config that is needed for this app
--but then TODO need to make persistent item ids
local function loadCfg()
    if files ~= nil then return end
    files = {}
    for fname in fs.find(".*%.cfg", cfgdir) do
        f = io.open(fname, 'r')
        if f then
	    s = f:read("*a")
	    f:close()
	    if s then
                c = loadstring("return ({\n" .. s .. "\n})")
	        if c then
		    for i,v in ipairs(c()) do
                        files[#files + 1] = v
		    end
		end
	    end
	end
    end
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
	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

--TODO this 'filter' breaks model isolation by requring caller to know
--keys in files[] items. But this way the breakage is at least very obvious,
--unlike passing these values as an argument to this function.
function list(self, filter)
    loadCfg()
    local ret = {}
    local lbuStatus = getLbuStatus()
    for k,v in pairs(files) do
        if not filter or filter(v) then
            ret[#ret+1] = {
                id=k,
                app=v.app,
                section=v.section,
                name=v.name,
                descr=v.descr,
		status=lbuStatus[v.filename]
            }
	end
    end
    return ret
end

function get(self, id)
    loadCfg()
    local item = files[id]
    if not item then return false end
    local f = io.open(item.filename, "r")
    local n = ""
    if f then
        n = f:read("*a")
        f:close()
    end
    return true, {
        id=cfe{ value=tostring(id) },
        content=cfe{ value=n, type="longtext" },
        name=cfe{ value=item.name }
    }
end


function set(self, id, data)
    loadCfg()
    local item = files[id]
    if not item then return false, date end
    local f = io.open(item.filename, "w")
    if f then
        f:write(data.content.value)
        f:close()
    end
    -- TODO update processing
    return get(self, id)
end