Multiple directories

This commit is contained in:
Your Name
2025-04-25 17:42:02 +12:00
parent 4d3523a346
commit 72e757ebd6
14 changed files with 344 additions and 113 deletions

View File

@@ -28,35 +28,41 @@ std::string get_local_system_templates_path()
return "/opt/dropshell/templates";
}
std::string get_local_config_path()
int getNumConfigDirectories()
{
config *cfg = get_global_config();
std::string user_dir;
if (!cfg->get_local_config_directory(user_dir)) {
return std::string();
}
return user_dir;
std::vector<std::string> local_config_directories = cfg->get_local_config_directories();
return local_config_directories.size();
}
std::string get_local_config_templates_path()
std::string get_local_config_path(int index)
{
std::string config_path = get_local_config_path();
config *cfg = get_global_config();
std::vector<std::string> local_config_directories = cfg->get_local_config_directories();
if (index < 0 || index >= local_config_directories.size())
return std::string();
return local_config_directories[index];
}
std::string get_local_config_templates_path(int index)
{
std::string config_path = get_local_config_path(index);
if (config_path.empty())
return std::string();
return config_path + "/templates";
}
std::string get_local_config_servers_path()
std::string get_local_config_servers_path(int index)
{
std::string config_path = get_local_config_path();
std::string config_path = get_local_config_path(index);
if (config_path.empty())
return std::string();
return config_path + "/servers";
}
std::string get_local_config_backups_path()
std::string get_local_config_backups_path(int index)
{
std::string config_path = get_local_config_path();
std::string config_path = get_local_config_path(index);
if (config_path.empty())
return std::string();
return config_path + "/backups";
@@ -64,12 +70,15 @@ std::string get_local_config_backups_path()
std::string get_local_server_path(const std::string &server_name)
{
if (server_name.empty())
return std::string();
std::string config_path = get_local_config_path();
if (config_path.empty())
return std::string();
return config_path + "/servers/" + server_name;
config *cfg = get_global_config();
std::vector<std::string> local_config_directories = cfg->get_local_config_directories();
for (auto &dir : local_config_directories) {
std::string server_path = dir + "/servers/" + server_name;
if (fs::exists(server_path)) {
return server_path;
}
}
return std::string();
}
std::string get_local_server_env_path(const std::string &server_name)

View File

@@ -8,10 +8,12 @@ namespace dropshell {
// local paths - return empty string on failure
std::string get_local_dropshell_config_path();
std::string get_local_system_templates_path();
std::string get_local_config_path();
std::string get_local_config_templates_path();
std::string get_local_config_servers_path();
std::string get_local_config_backups_path();
int getNumConfigDirectories();
std::string get_local_config_path(int index);
std::string get_local_config_templates_path(int index);
std::string get_local_config_servers_path(int index);
std::string get_local_config_backups_path(int index);
std::string get_local_server_path(const std::string &server_name);
std::string get_local_server_env_path(const std::string &server_name);

View File

@@ -97,7 +97,7 @@ void envmanager::clear_variables() {
m_variables.clear();
}
std::string envmanager::trim(std::string str) const {
std::string trim(std::string str) {
// Trim leading whitespace
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](unsigned char ch) {
return !std::isspace(ch);
@@ -132,4 +132,62 @@ std::string envmanager::expand_patterns(std::string str) const {
return result;
}
std::string multi2string(std::vector<std::string> values)
{
std::string result;
for (const auto& value : values) {
// remove any " contained in the string value, if present
std::string quoteless_value = value;
quoteless_value.erase(std::remove(quoteless_value.begin(), quoteless_value.end(), '"'), quoteless_value.end());
result += "\"" + trim(quoteless_value) + "\",";
}
if (!result.empty())
result.pop_back(); // Remove the last comma
return result;
}
std::vector<std::string> string2multi(std::string values)
{
std::vector<std::string> result;
// Return values separated by commas, but ignore commas within quotes
bool inside_quotes = false;
std::string current_item;
for (char c : values) {
if (c == '"') {
inside_quotes = !inside_quotes;
} else if (c == ',' && !inside_quotes) {
if (!current_item.empty()) {
// Remove quotes if present
if (current_item.front() == '"' && current_item.back() == '"') {
current_item = current_item.substr(1, current_item.length() - 2);
}
std::string final = trim(current_item);
if (!final.empty()) {
result.push_back(final);
}
current_item.clear();
}
} else {
current_item += c;
}
}
// Add the last item if not empty
if (!current_item.empty()) {
// Remove quotes if present
if (current_item.front() == '"' && current_item.back() == '"') {
current_item = current_item.substr(1, current_item.length() - 2);
}
std::string final = trim(current_item);
if (!final.empty()) {
result.push_back(final);
}
}
return result;
}
} // namespace dropshell

View File

@@ -3,6 +3,7 @@
#include <string>
#include <map>
#include <vector>
namespace dropshell {
// envmanager is a class that manages the environment files for the application.
@@ -36,7 +37,6 @@ class envmanager {
void clear_variables();
private:
std::string trim(std::string str) const;
std::string expand_patterns(std::string str) const;
private:
@@ -44,6 +44,11 @@ class envmanager {
std::map<std::string, std::string> m_variables;
};
// utility functions
std::string trim(std::string str);
std::string multi2string(std::vector<std::string> values);
std::vector<std::string> string2multi(std::string values);
} // namespace dropshell
#endif