From a165efeaf58dc7336e40d812359b0fc2ce0e7a83 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 1 Sep 2023 07:29:41 -0400 Subject: add `--write-config` to zenserver options (#382) - Feature: `zen up` command has two new command line options - `--config ` tells zenserver to start with a specific config file - `--owner-pid ` tells zenserver to start with a owning process id - Feature: `zen attach` command to add additional owning processes to a running zenserver instance - `--owner-pid ` adds pid to running zenserver instance list of owning processes - Feature: `--write-config` command line option for zenserver - `--write-config ` path to a file which will contain a lua config file for zenserver combining all command line options and optional lua config files - Improvement: `zen up` command will check if zenserver is currently running before starting up a new instance --- src/zenserver/config.cpp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'src/zenserver/config.cpp') diff --git a/src/zenserver/config.cpp b/src/zenserver/config.cpp index 2d286b02a..1f5a3eb21 100644 --- a/src/zenserver/config.cpp +++ b/src/zenserver/config.cpp @@ -10,6 +10,7 @@ #include #include #include +#include ZEN_THIRD_PARTY_INCLUDES_START #include @@ -136,15 +137,15 @@ ParseBucketConfigs(std::span Buckets) } static std::string -MakeSafePath(const std::string& Path) +MakeSafePath(const std::string_view Path) { #if ZEN_PLATFORM_WINDOWS if (Path.empty()) { - return Path; + return std::string(Path); } - std::string FixedPath = Path; + std::string FixedPath(Path); std::replace(FixedPath.begin(), FixedPath.end(), '/', '\\'); if (!FixedPath.starts_with("\\\\?\\")) { @@ -152,7 +153,7 @@ MakeSafePath(const std::string& Path) } return FixedPath; #else - return Path; + return std::string(Path); #endif }; @@ -768,7 +769,10 @@ private: } // namespace LuaConfig void -ParseConfigFile(const std::filesystem::path& Path, ZenServerOptions& ServerOptions, const cxxopts::ParseResult& CmdLineResult) +ParseConfigFile(const std::filesystem::path& Path, + ZenServerOptions& ServerOptions, + const cxxopts::ParseResult& CmdLineResult, + std::string_view OutputConfigFile) { using namespace std::literals; @@ -923,6 +927,16 @@ ParseConfigFile(const std::filesystem::path& Path, ZenServerOptions& ServerOptio { LuaOptions.Touch("server.objectstore.buckets"sv); } + + if (!OutputConfigFile.empty()) + { + std::filesystem::path WritePath(MakeSafePath(OutputConfigFile)); + zen::ExtendableStringBuilder<512> ConfigStringBuilder; + LuaOptions.Print(ConfigStringBuilder, CmdLineResult); + zen::BasicFile Output; + Output.Open(WritePath, zen::BasicFile::Mode::kTruncate); + Output.Write(ConfigStringBuilder.Data(), ConfigStringBuilder.Size(), 0); + } } void @@ -943,6 +957,7 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) std::string ContentDir; std::string AbsLogFile; std::string ConfigFile; + std::string OutputConfigFile; cxxopts::Options options("zenserver", "Zen Server"); options.add_options()("dedicated", @@ -956,6 +971,7 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) options.add_options()("content-dir", "Frontend content directory", cxxopts::value(ContentDir)); options.add_options()("abslog", "Path to log file", cxxopts::value(AbsLogFile)); options.add_options()("config", "Path to Lua config file", cxxopts::value(ConfigFile)); + options.add_options()("write-config", "Path to output Lua config file", cxxopts::value(OutputConfigFile)); options.add_options()("no-sentry", "Disable Sentry crash handler", cxxopts::value(ServerOptions.NoSentry)->default_value("false")); @@ -1380,11 +1396,11 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) if (!ServerOptions.ConfigFile.empty()) { - ParseConfigFile(ServerOptions.ConfigFile, ServerOptions, result); + ParseConfigFile(ServerOptions.ConfigFile, ServerOptions, result, OutputConfigFile); } else { - ParseConfigFile(ServerOptions.DataDir / "zen_cfg.lua", ServerOptions, result); + ParseConfigFile(ServerOptions.DataDir / "zen_cfg.lua", ServerOptions, result, OutputConfigFile); } ValidateOptions(ServerOptions); -- cgit v1.2.3