summaryrefslogtreecommitdiffstats
path: root/lib/apk.lua
blob: 774b7f0ec6667ddca3cdb1e57393c1e57778c54a (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
-- apk library
module (..., package.seeall)

local repo = nil
local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "
local install_cache = false

local reload_installed = function()
	if repo then
		-- clear out installed info
		for name,value in pairs(repo) do
			if value then
				value.installed = nil
				value.comment = nil
			end
		end
		-- read in which are installed
		local f = io.popen(path.."/sbin/apk_info 2>/dev/null")
		local line
		for line in f:lines() do
			local name, ver, comment = string.match(line, "(%S+)%-(%d+%S*)%s+(.*)")
			if not repo[name] then
				repo[name] = {}
			end
			repo[name].installed = ver
			repo[name].comment = comment
		end
		install_cache = true
	end
	return repo
end


repository = function()
	if not repo then
		-- read in all of the packages
		local f = io.popen(path.."/sbin/apk_fetch -lvq 2>/dev/null")
		repo = {}
		install_cache = false
		for line in f:lines() do
			local name, ver = string.match(line, "(.*)%-(%d+.*)")
			if name then
				repo[name] = {}
				repo[name].version = ver
			end
		end
		f:close()
	end
	if not install_cache then
		reload_installed()
	end
	return repo
end

get_all = function()
	repo = repository()
	-- read in all of the available packages
	local all = {}
	for name,value in pairs(repo) do
		if value.version then
			local temp = {}
			temp.name = name
			temp.version = value.version
			all[#all + 1] = temp
		end
	end
	table.sort(all, function(a,b) return (a.name < b.name) end)
	return all
end

get_loaded = function()
	repo = repository()
	-- read in the loaded packages
	local loaded = {}
	for name,value in pairs(repo) do
		if value.installed then 
			local temp = {}
			temp.name = name
			temp.version = value.installed
			temp.description = value.comment
			loaded[#loaded+1] = temp
		end
	end
	table.sort(loaded, function(a,b) return (a.name < b.name) end)
	return loaded
end

get_available = function()
	repo = repository()
	-- available are all except same version installed
	local available = {}
	for name,value in pairs(repo) do
		if value.version ~= value.installed then
			local temp = {}
			temp.name = name
			temp.version = value.version
			available[#available + 1] = temp
		end
	end
	table.sort(available, function(a,b) return (a.name < b.name) end)
	return available
end

delete = function(package)
	repo = repository()
	local success = false
	local cmdresult = "Delete failed - Invalid package"
	if package and repo[package] then
		success = true
		local cmd = path .. "apk_delete " .. package .. " 2>&1"
		local f = io.popen( cmd )
		cmdresult = f:read("*a") or ""
		f:close()
		install_cache = false
	end
	return success, cmdresult
end

install = function(package)
	repo = repository()
	local success = false
	local cmdresult = "Install failed - Invalid package"
	if package and repo[package] then
		success = true
		local cmd = path .. "apk_add " .. package .. " 2>&1"
		local f = io.popen( cmd )
		cmdresult = f:read("*a")
		f:close()
		install_cache = false
	end
	return success, cmdresult
end

is_installed = function(package)
	repo = repository()
	return package and repo[package] and repo[package].installed
end