From 8270b55e1c080e330676863c20adc3bf84227c6c Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 8 Dec 2024 18:50:18 +0300 Subject: [PATCH] feat: add way to use environment value for 'output' option --- src/config.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 375dc4cb..2ada69d1 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -123,20 +123,37 @@ void Config::mergeConfig(Json::Value &a_config_, Json::Value &b_config_) { } bool isValidOutput(const Json::Value &config, const std::string &name, const std::string &identifier) { + const auto isOutputMatches = [&](const std::string &output) -> bool { + if (output.substr(0, 1) == "$") { + auto *const environment_value = std::getenv(output.substr(1).c_str()); + if (environment_value != nullptr) { + const std::string output_from_env = environment_value; + return output_from_env == name || output_from_env == identifier; + } + + spdlog::warn("The environment value is unknown: {}", output); + } + + return output == name || output == identifier; + }; + if (config["output"].isArray()) { for (auto const &output_conf : config["output"]) { if (output_conf.isString()) { auto config_output = output_conf.asString(); + if (config_output.substr(0, 1) == "!") { - if (config_output.substr(1) == name || config_output.substr(1) == identifier) { + if (isOutputMatches(config_output.substr(1))) { return false; } continue; } - if (config_output == name || config_output == identifier) { + + if (isOutputMatches(config_output)) { return true; } + if (config_output.substr(0, 1) == "*") { return true; } @@ -147,11 +164,13 @@ bool isValidOutput(const Json::Value &config, const std::string &name, if (config["output"].isString()) { auto config_output = config["output"].asString(); + if (!config_output.empty()) { if (config_output.substr(0, 1) == "!") { - return config_output.substr(1) != name && config_output.substr(1) != identifier; + return !isOutputMatches(config_output.substr(1)); } - return config_output == name || config_output == identifier; + + return isOutputMatches(config_output); } }