Integrate in agent.

This commit is contained in:
Your Name
2025-04-25 20:06:47 +12:00
parent 513431b7a3
commit 9409adf4f6
10 changed files with 306 additions and 130 deletions

View File

@@ -39,7 +39,6 @@ namespace dropshell {
std::string get_remote_service_backups_path(const std::string &server_name, const std::string &service_name);
std::string get_remote_service_env_file(const std::string &server_name, const std::string &service_name);
std::string get_remote_service_env_file_parent(const std::string &server_name, const std::string &service_name);
} // namespace dropshell
#endif

View File

@@ -1,4 +1,5 @@
#include "envmanager.hpp"
#include "utils/utils.hpp"
#include <fstream>
#include <sstream>
#include <algorithm>
@@ -97,20 +98,6 @@ void envmanager::clear_variables() {
m_variables.clear();
}
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);
}));
// Trim trailing whitespace
str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), str.end());
return str;
}
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_]+))");
@@ -132,62 +119,4 @@ 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

@@ -45,7 +45,8 @@ class envmanager {
};
// utility functions
std::string trim(std::string str);
std::string trim(std::string str);
std::string dequote(std::string str);
std::string multi2string(std::vector<std::string> values);
std::vector<std::string> string2multi(std::string values);

View File

@@ -3,7 +3,7 @@
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
namespace dropshell {
void maketitle(const std::string& title) {
@@ -42,4 +42,92 @@ bool replace_line_in_file(const std::string& file_path, const std::string& searc
return true;
}
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);
}));
// Trim trailing whitespace
str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), str.end());
return str;
}
std::string dequote(std::string str)
{
if (str.length() < 2)
return str;
if (str.front() == '"' && str.back() == '"') {
return str.substr(1, str.length() - 2);
}
return str;
}
std::string quote(std::string str)
{
return "\""+str+"\"";
}
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

@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <vector>
namespace dropshell {
@@ -13,4 +14,13 @@ void maketitle(const std::string& title);
bool replace_line_in_file(const std::string& file_path, const std::string& search_string, const std::string& replacement_line);
// utility functions
std::string trim(std::string str);
std::string dequote(std::string str);
std::string quote(std::string str);
std::string multi2string(std::vector<std::string> values);
std::vector<std::string> string2multi(std::string values);
} // namespace dropshell