forked from lunarmodules/lua_cliargs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_loader.lua
More file actions
139 lines (109 loc) · 2.85 KB
/
Copy pathconfig_loader.lua
File metadata and controls
139 lines (109 loc) · 2.85 KB
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
138
139
local trim = require 'cliargs.utils.trim'
local function read_file(filepath)
local f, err = io.open(filepath, "r")
if not f then
return nil, err
end
local contents = f:read('*all')
f:close()
return contents
end
return {
FORMAT_LOADERS = {
["lua"] = "from_lua",
["json"] = "from_json",
["yaml"] = "from_yaml",
["yml"] = "from_yaml",
["ini"] = "from_ini",
},
--- Load configuration from a Lua file that exports a table.
from_lua = function(filepath)
local file, err = loadfile(filepath)
if not file and err then
return nil, err
end
return file()
end,
--- Load configuration from a JSON file.
---
--- Requires the "dkjson"[1] module to be present on the system. Get it with:
---
--- luarocks install dkjson
---
--- [1] http://dkolf.de/src/dkjson-lua.fsl/home
from_json = function(filepath)
local src, config, _, err
local json = require 'dkjson'
src, err = read_file(filepath)
if not src and err then
return nil, err
end
config, _, err = json.decode(src)
if err then
return nil, err
end
return config
end,
--- Load configuration from an INI file.
---
--- Requires the "inifile"[1] module to be present on the system. Get it with:
---
--- luarocks install inifile
---
--- The INI file must contain a group that lists the default values. For
--- example:
---
--- [cli]
--- quiet = true
--- compress = lzma
---
--- The routine will automatically cast boolean values ("true" and "false")
--- into Lua booleans. You may opt out of this behavior by passing `false`
--- to `no_cast`.
---
--- [1] http://docs.bartbes.com/inifile
from_ini = function(filepath, group, no_cast)
local inifile = require 'inifile'
local config, err
group = group or 'cli'
assert(type(group) == 'string',
'You must provide an INI group to read from.'
)
config, err = inifile.parse(filepath)
if not config and err then
return nil, err
end
if not no_cast then
for k, src_value in pairs(config[group]) do
local v = trim(src_value)
if v == 'true' then
v = true
elseif v == 'false' then
v = false
end
config[group][k] = v
end
end
return config[group]
end,
--- Load configuration from a YAML file.
---
--- Requires the "lyaml"[1] module to be present on the system. Get it with:
---
--- luarocks install lyaml
---
--- [1] https://github.com/gvvaughan/lyaml
from_yaml = function(filepath)
local src, err
local lyaml = require 'lyaml'
src, err = read_file(filepath)
if not src and err then
return nil, err
end
local ok, config_or_err = pcall(lyaml.load, src)
if not ok then
return nil, config_or_err
end
return config_or_err
end
}