-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.lua
109 lines (94 loc) · 2.41 KB
/
config.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
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
local pipe = require "pipe"
local config = {
argv = nil,
argc = 0,
name = "dzecnt-container-host",
mount = nil,
uid = nil,
gid = nil,
-- 1GB
mem = 1024 * 1024 * 1024 * 1024
}
local opt_handlers = {
["-f"] = function(config, argv, i)
-- TODO: parse file for settings
end,
["-n"] = function(config, argv, i)
assert(argv[i + 1])
config.name = argv[i + 1]
end,
["-m"] = function(config, argv, i)
assert(argv[i + 1])
config.mount = argv[i + 1]
end,
["-u"] = function(config, argv, i)
assert(argv[i + 1])
config.uid = argv[i + 1]
end,
["-g"] = function(config, argv, i)
assert(argv[i + 1])
config.gid = argv[i + 1]
end,
["-c"] = nil -- Just to make sure.
}
function config:new()
self.__index = self
local obj = {
pipe = pipe:new()
}
return setmetatable(obj, self)
end
function config:parse(argv)
local argc = #argv
local name = argv[0]
argv[0] = nil
if not argv[1] then
self:usage(name)
os.exit()
end
local i = 1
while i <= argc do
if argv[i] == "-h" then
self:usage(name)
os.exit()
elseif opt_handlers[argv[i]] then
opt_handlers[argv[i]](self, argv, i)
i = i + 1 -- Skip next.
else
if argv[i] == "-c" or (i == 1 and argv[i]:sub(1,1) ~= '-') then
if argv[i] == "-c" then
i = i + 1
end
self.argv = {}
-- Copy the remainder as argv of the
-- executed command.
local j = 1
while i <= argc do
self.argv[j] = argv[i]
i = i + 1
j = j + 1
end
self.argc = j - 1
return self
end
-- TODO: Log error.
end
i = i + 1
end
return self
end
function config:usage(name)
print(string.format([[
Usage: %s [OPTIONS]... -c COMMAND [ARGS]...
Options:
-c COMMAND specifies command to run,
can be omitted if no other
options are used
-f FILE config file to read
-h print this message
-m PATH mount point path
-n NAME hostname
-u MAP uid map
]], name))
end
return config