-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
47 lines (42 loc) · 1.38 KB
/
util.lua
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
-- SPDX-License-Identifier: GPL-2.0
-- Copyright 2022-2024 - Fábio Rodrigues Ribeiro and contributors
local function getoutput(cmd)
local handle = assert(io.popen(cmd))
if not handle then error(("Erro ao chamar o comando: %s."):format(cmd)) end
local result = handle:read "*l"
handle:close()
if not result or result == "" then return nil, "Command output is empty" end
return result
end
local function openfile_match(filename, string)
local file = assert(io.open(filename, "r"))
if not file then error(("Erro ao abrir o arquivo %s para leitura."):format(filename)) end
local result
for line in file:lines() do
result = line:match(string)
if result then break end
end
file:close()
-- if not result or result == "" then return nil, "File is empty" end
return result
end
local function xargs()
local opts = ""
for i = 2, #arg do opts = ("%s %s"):format(opts, arg[i]) end
return opts
end
local function sbversion() return getoutput "rpm -E %fedora" end
local function arch() return getoutput "uname -m" end
local function writecmd_x(cmd) io.write(cmd .. "\n") x(cmd) end
local function writemsg_x(cmd, msg) io.write(msg) x(cmd) end
local function x_writemsg(cmd, msg) x(cmd) io.write(msg) end
return {
sbversion = sbversion,
arch = arch,
writecmd_x = writecmd_x,
writemsg_x = writemsg_x,
x_writemsg = x_writemsg,
openfile_match = openfile_match,
xargs = xargs,
getoutput = getoutput
}