diff options
| author | ruki <[email protected]> | 2022-04-05 22:56:30 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-05 22:56:30 +0800 |
| commit | 217f0009ace71f566384a775890c64b8d337c1c3 (patch) | |
| tree | d11edc167e115b8b824594cc51bf55a44059cade | |
| parent | 5ec3a71b945421da1e6a0df7b371990952cc6ead (diff) | |
improve config file for service
| -rw-r--r-- | xmake/modules/private/service/config.lua | 54 | ||||
| -rw-r--r-- | xmake/plugins/service/main.lua | 2 | ||||
| -rw-r--r-- | xmake/plugins/service/xmake.lua | 2 |
3 files changed, 32 insertions, 26 deletions
diff --git a/xmake/modules/private/service/config.lua b/xmake/modules/private/service/config.lua index 239759225..18e1d3028 100644 --- a/xmake/modules/private/service/config.lua +++ b/xmake/modules/private/service/config.lua @@ -19,36 +19,40 @@ -- -- imports -import("core.base.object") +import("core.base.global") --- define module -local config = config or object() - --- init config -function config:init() -end - --- get the listen address -function config:addr() - return "127.0.0.1" -end - --- get the listen port -function config:port() - return 90091 +-- generate a default config file +function _generate_configfile() + local filepath = configfile() + assert(not _g.configs and not os.isfile(filepath)) + local configs = { + logfile = path.join(global.directory(), "service", "logs.txt"), + remote_build = { + server = { + listen = "127.0.0.1:90091" + } + } + } + io.save(filepath, configs, {orderkeys = true}) end --- get class -function config:class() - return config +-- get config file path +function configfile() + return path.join(global.directory(), "service.conf") end -function config:__tostring() - return "<config>" +-- get all configs +function configs() + return _g.configs end -function main() - local instance = config() - instance:init() - return instance +-- load config +function load() + assert(not _g.configs, "config has been loaded!") + local filepath = configfile() + if not os.isfile(filepath) then + _generate_configfile() + end + assert(os.isfile(filepath), "%s not found!", filepath) + _g.configs = io.load(filepath) end diff --git a/xmake/plugins/service/main.lua b/xmake/plugins/service/main.lua index acf0540a4..660903412 100644 --- a/xmake/plugins/service/main.lua +++ b/xmake/plugins/service/main.lua @@ -20,6 +20,7 @@ -- imports import("core.base.option") +import("private.service.config") import("private.service.start_service") import("private.service.restart_service") import("private.service.stop_service") @@ -32,6 +33,7 @@ import("private.service.show_status") function main() cprint("${color.warning}It's experimental feature, still in development!") + config.load() if option.get("start") then start_service({daemon = true}) elseif option.get("restart") then diff --git a/xmake/plugins/service/xmake.lua b/xmake/plugins/service/xmake.lua index a84286c02..b819c7157 100644 --- a/xmake/plugins/service/xmake.lua +++ b/xmake/plugins/service/xmake.lua @@ -31,7 +31,7 @@ task("service") {nil, "connect" , "k", nil, "Connect the remote daemon service." }, {nil, "reconnect", "k", nil, "Reconnect the remote daemon service." }, {nil, "disconnect", "k", nil, "Disconnect the remote daemon service." }, - {nil, "config", "kv", nil, "Import the configuration file. (default: ~/.xmake/service/config.lua)", + {nil, "config", "kv", nil, "Import the configuration file. (default: ~/.xmake/service.conf)", "e.g.", " - xmake service --config=/tmp/config.lua" }, {nil, "logs", "k", nil, "Show service logs if the daemon service has been started." }, |
