58 lines
1.6 KiB
Lua
58 lines
1.6 KiB
Lua
-- HorlogeSkynet's Lua script (for Conky) -- Config module
|
|
-- <https://git.forestier.app/HorlogeSkynet/SimpleConkyScript.git>
|
|
|
|
|
|
local json = require('cjson.safe')
|
|
|
|
|
|
local config = {}
|
|
|
|
|
|
local _config, _config_json
|
|
|
|
|
|
-- Open, decode and load the user configuration file.
|
|
-- This function returns `true` when the internal `_config` object has successfully changed.
|
|
function config.load_config(config_file_path)
|
|
local config_f, error_message = io.open(config_file_path .. 'config.json')
|
|
if not config_f then
|
|
io.stderr:write(
|
|
string.format("Couldn\'t open the configuration file : %s.\n", error_message))
|
|
return false
|
|
end
|
|
|
|
local raw_json = config_f:read('*all')
|
|
|
|
-- Check whether the user configuration has changed and should be updated.
|
|
if not _config_json or raw_json ~= _config_json then
|
|
_config, error_message = json.decode(raw_json)
|
|
if not _config then
|
|
io.stderr:write(
|
|
string.format("JSON parsing error : %s.\n", error_message))
|
|
return false
|
|
end
|
|
|
|
-- Save the loaded configuration for future comparisons.
|
|
_config_json = raw_json
|
|
|
|
return true
|
|
end
|
|
|
|
return false
|
|
end
|
|
|
|
|
|
-- Return a reference to the passed configuration section.
|
|
-- Please be sure to populate `_config` with `config.load_config` at first.
|
|
function config.get_entries(section)
|
|
return _config[section]
|
|
end
|
|
|
|
|
|
-- During module loading, make the `_config` object be automatically populated.
|
|
-- The configuration file path is passed as module parameter.
|
|
config.load_config(...)
|
|
|
|
|
|
return config
|