Add template manager for remote templates.
This commit is contained in:
@@ -22,9 +22,9 @@ namespace localfile {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string server_env(const std::string &server_name) {
|
||||
std::string server_json(const std::string &server_name) {
|
||||
std::string serverpath = localpath::server(server_name);
|
||||
return (serverpath.empty() ? "" : (fs::path(serverpath) / "server.env").string());
|
||||
return (serverpath.empty() ? "" : (fs::path(serverpath) / "server.json").string());
|
||||
}
|
||||
|
||||
std::string service_env(const std::string &server_name, const std::string &service_name) {
|
||||
|
@@ -14,7 +14,7 @@ namespace dropshell {
|
||||
|
||||
// server_definition_path
|
||||
// |-- <server_name>
|
||||
// |-- server.env
|
||||
// |-- server.json
|
||||
// |-- services
|
||||
// |-- <service_name>
|
||||
// |-- service.env
|
||||
@@ -42,7 +42,7 @@ namespace dropshell {
|
||||
namespace localfile {
|
||||
// ~/.config/dropshell/dropshell.json
|
||||
std::string dropshell_json();
|
||||
std::string server_env(const std::string &server_name);
|
||||
std::string server_json(const std::string &server_name);
|
||||
std::string service_env(const std::string &server_name, const std::string &service_name);
|
||||
std::string template_info_env(const std::string &server_name, const std::string &service_name);
|
||||
} // namespace localfile
|
||||
|
@@ -72,18 +72,6 @@ void envmanager::get_all_variables(std::map<std::string, std::string>& variables
|
||||
variables = m_variables;
|
||||
}
|
||||
|
||||
std::string envmanager::get_variable_substituted(std::string key) const {
|
||||
std::string value = get_variable(key);
|
||||
return expand_patterns(value);
|
||||
}
|
||||
|
||||
void envmanager::get_all_variables_substituted(std::map<std::string, std::string>& variables) const {
|
||||
variables.clear();
|
||||
for (const auto& pair : m_variables) {
|
||||
variables[pair.first] = expand_patterns(pair.second);
|
||||
}
|
||||
}
|
||||
|
||||
void envmanager::add_variables(std::map<std::string, std::string> variables) {
|
||||
for (auto& pair : variables) {
|
||||
set_variable(pair.first, pair.second);
|
||||
@@ -98,26 +86,4 @@ void envmanager::clear_variables() {
|
||||
m_variables.clear();
|
||||
}
|
||||
|
||||
std::string envmanager::expand_patterns(std::string str) const {
|
||||
// Combined regex pattern for both ${var} and $var formats
|
||||
std::regex var_pattern("\\$(?:\\{([^}]+)\\}|([a-zA-Z0-9_]+))");
|
||||
std::string result = str;
|
||||
std::smatch match;
|
||||
|
||||
while (std::regex_search(result, match, var_pattern)) {
|
||||
// match[1] will contain capture from ${var} format
|
||||
// match[2] will contain capture from $var format
|
||||
std::string var_name = match[1].matched ? match[1].str() : match[2].str();
|
||||
|
||||
// Get value from system environment variables
|
||||
const char* env_value = std::getenv(var_name.c_str());
|
||||
std::string value = env_value ? env_value : "";
|
||||
|
||||
result = result.replace(match.position(), match.length(), value);
|
||||
}
|
||||
|
||||
// dequote the result
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
||||
|
@@ -25,20 +25,12 @@ class envmanager {
|
||||
std::string get_variable(std::string key) const;
|
||||
void get_all_variables(std::map<std::string, std::string>& variables) const;
|
||||
|
||||
// get variables, but replace patterns ${var} and $var with the actual environment variable in the returned string.
|
||||
// trim whitespace from the values.
|
||||
std::string get_variable_substituted(std::string key) const;
|
||||
void get_all_variables_substituted(std::map<std::string, std::string>& variables) const;
|
||||
|
||||
// add variables to the environment files.
|
||||
// trim whitespace from the values.
|
||||
void add_variables(std::map<std::string, std::string> variables);
|
||||
void set_variable(std::string key, std::string value);
|
||||
void clear_variables();
|
||||
|
||||
private:
|
||||
std::string expand_patterns(std::string str) const;
|
||||
|
||||
private:
|
||||
std::string m_path;
|
||||
std::map<std::string, std::string> m_variables;
|
||||
|
@@ -5,6 +5,7 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <regex>
|
||||
namespace dropshell {
|
||||
|
||||
void maketitle(const std::string& title) {
|
||||
@@ -268,4 +269,32 @@ std::vector<std::string> split(const std::string& str, const std::string& delimi
|
||||
return tokens;
|
||||
}
|
||||
|
||||
|
||||
std::string replace_with_environment_variables_like_bash(std::string str) {
|
||||
// Combined regex pattern for both ${var} and $var formats
|
||||
std::regex var_pattern("\\$(?:\\{([^}]+)\\}|([a-zA-Z0-9_]+))");
|
||||
std::string result = str;
|
||||
std::smatch match;
|
||||
|
||||
while (std::regex_search(result, match, var_pattern)) {
|
||||
// match[1] will contain capture from ${var} format
|
||||
// match[2] will contain capture from $var format
|
||||
std::string var_name = match[1].matched ? match[1].str() : match[2].str();
|
||||
|
||||
// Get value from system environment variables
|
||||
const char* env_value = std::getenv(var_name.c_str());
|
||||
std::string value = env_value ? env_value : "";
|
||||
|
||||
result = result.replace(match.position(), match.length(), value);
|
||||
}
|
||||
|
||||
// dequote the result
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
std::string requote(std::string str) {
|
||||
return quote(trim(dequote(trim(str))));
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
@@ -20,6 +20,8 @@ std::string trim(std::string str);
|
||||
std::string dequote(std::string str);
|
||||
std::string quote(std::string str);
|
||||
std::string halfquote(std::string str);
|
||||
std::string requote(std::string str);
|
||||
|
||||
std::string multi2string(std::vector<std::string> values);
|
||||
std::vector<std::string> string2multi(std::string values);
|
||||
std::vector<std::string> split(const std::string& str, const std::string& delimiter);
|
||||
@@ -34,4 +36,6 @@ void ensure_directories_exist(std::vector<std::string> directories);
|
||||
std::vector<int> search(const std::string &pat, const std::string &txt);
|
||||
int count_substring(const std::string &substring, const std::string &text);
|
||||
|
||||
std::string replace_with_environment_variables_like_bash(std::string str);
|
||||
|
||||
} // namespace dropshell
|
Reference in New Issue
Block a user