code tidying

This commit is contained in:
Your Name
2025-04-30 20:46:18 +12:00
parent 32da3d5fbc
commit 194bde1a0d
10 changed files with 88 additions and 84 deletions

View File

@ -97,11 +97,13 @@ void printversion() {
}
#define HAPPYEXIT(CMD, RUNCMD) {if (safearg(argc,argv,1) == CMD) {RUNCMD; return 0;}}
#define BOOLEXIT(CMD, RUNCMD) {if (safearg(argc,argv,1) == CMD) {return (RUNCMD) ? 0 : 1;}}
int main(int argc, char* argv[]) {
HAPPYEXIT("hash", hash_demo_raw(safearg(argc,argv,2)))
HAPPYEXIT("makesafecmd", std::cout<<makesafecmd(safearg(argc,argv,2))<<std::endl)
HAPPYEXIT("version", printversion())
BOOLEXIT("test-template", test_template(safearg(argc,argv,2)))
ASSERT_MSG(safearg(argc,argv,1) != "assert", "Hello! Here is an assert.");
try {

View File

@ -209,30 +209,28 @@ void get_all_service_env_vars(const std::string &server_name, const std::string
all_env_vars["SERVER"] = server_name;
all_env_vars["SERVICE"] = service_name;
std::vector<std::string> env_files = {
localfile::service_env(server_name,service_name),
localfile::template_info_env(server_name,service_name)
};
{ // load service.env from the service on this machine.
std::map<std::string, std::string> env_vars;
envmanager env_manager(localfile::service_env(server_name,service_name));
env_manager.load();
env_manager.get_all_variables(env_vars);
all_env_vars.merge(env_vars);
}
{ // load .template_info.env from the service on this machine.
std::map<std::string, std::string> env_vars;
envmanager env_manager(localfile::template_info_env(server_name,service_name));
env_manager.load();
env_manager.get_all_variables(env_vars);
all_env_vars.merge(env_vars);
for (const auto& file : env_files) {
// load service.env from the service on this machine.
std::map<std::string, std::string> env_vars;
envmanager env_manager(file);
env_manager.load();
env_manager.get_all_variables(env_vars);
all_env_vars.merge(env_vars);
}
// determine template name.
auto it = all_env_vars.find("TEMPLATE");
if (it == all_env_vars.end()) {
std::cerr << std::endl << std::endl;
std::cerr << "Error: TEMPLATE variable not defined in service " << service_name << " on server " << server_name << std::endl;
std::cerr << "The TEMPLATE variable is required to determine the template name." << std::endl;
std::cerr << "Please check the service.env file and the .template_info.env file in:" << std::endl;
std::cerr << " " << localpath::service(server_name, service_name) << std::endl;
std::cerr << " " << localpath::service(server_name, service_name) << std::endl << std::endl;
return;
}
std::string template_name = it->second;
@ -257,5 +255,4 @@ void get_all_service_env_vars(const std::string &server_name, const std::string
}
} // namespace dropshell

View File

@ -22,7 +22,6 @@ namespace dropshell {
// get all env vars for a given service
void get_all_service_env_vars(const std::string& server_name, const std::string& service_name, std::map<std::string, std::string> & all_env_vars);
// list all backups for a given service (across all servers)
std::set<std::string> list_backups(const std::string& server_name, const std::string& service_name);

View File

@ -1,7 +1,3 @@
#include "templates.hpp"
#include "config.hpp"
#include "utils/directories.hpp"
#include "utils/utils.hpp"
#include <filesystem>
#include <iostream>
#include <fstream>
@ -9,6 +5,13 @@
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include "utils/envmanager.hpp"
#include "utils/directories.hpp"
#include "utils/utils.hpp"
#include "templates.hpp"
#include "config.hpp"
namespace dropshell {
@ -189,4 +192,67 @@
return true;
}
bool required_file(std::string path, std::string template_name)
{
if (!std::filesystem::exists(path)) {
std::cerr << "Error: " << path << " file not found in template " << template_name << std::endl;
return false;
}
return true;
}
bool test_template(const std::string &template_path)
{
std::string template_name = std::filesystem::path(template_path).filename().string();
std::vector<std::string> required_files = {
"example/service.env",
"example/.template_info.env",
"_default.env",
"install.sh",
"uninstall.sh"
};
for (const auto& file : required_files) {
if (!required_file(template_path + "/" + file, template_name))
return false;
}
// ------------------------------------------------------------
// check TEMPLATE= line.
std::map<std::string, std::string> all_env_vars;
std::vector<std::string> env_files = {
"example/service.env",
"example/.template_info.env"
};
for (const auto& file : env_files) {
{ // load service.env from the service on this machine.
std::map<std::string, std::string> env_vars;
envmanager env_manager(template_path + "/" + file);
env_manager.load();
env_manager.get_all_variables(env_vars);
all_env_vars.merge(env_vars);
}
// determine template name.
auto it = all_env_vars.find("TEMPLATE");
if (it == all_env_vars.end()) {
std::cerr << "Error: TEMPLATE variable not found in " << file << std::endl;
return false;
}
std::string env_template_name = it->second;
if (env_template_name.empty()) {
std::cerr << "Error: TEMPLATE variable is empty in " << file << std::endl;
return false;
}
if (env_template_name != template_name) {
std::cerr << "Error: TEMPLATE variable is wrong in " << file << std::endl;
return false;
}
return true;
}
} // namespace dropshell

View File

@ -33,4 +33,7 @@ class template_info {
bool get_all_template_config_directories(std::vector<std::string>& template_config_directories);
bool test_template(const std::string& template_path);
} // namespace dropshell