This commit is contained in:
John 2025-04-27 13:03:48 +12:00
parent 8614d29b06
commit 192cc48d42
4 changed files with 195 additions and 191 deletions

View File

@ -244,8 +244,6 @@ int main(int argc, char* argv[]) {
std::cerr << command << " ";
}
std::cerr << std::endl;
// print help
dropshell::print_help();
return 1;
} catch (const std::exception& e) {

View File

@ -95,7 +95,7 @@ ServiceInfo get_service_info(const std::string &server_name, const std::string &
// find the template path
service.local_template_path = tinfo.local_template_path;
service.local_template_default_env_path = tinfo.local_template_default_env_path;
service.local_template_default_env_path = tinfo.local_template_path + "/_default.env";
return service;
}

View File

@ -18,61 +18,51 @@ bool get_templates(std::vector<template_info>& templates) {
// Helper function to add templates from a directory
auto add_templates_from_dir = [&templates](const std::string& dir_path) {
if (!std::filesystem::exists(dir_path)) {
if (!std::filesystem::exists(dir_path))
return;
}
for (const auto& entry : std::filesystem::directory_iterator(dir_path)) {
for (const auto& entry : std::filesystem::directory_iterator(dir_path))
if (entry.is_directory()) {
template_info info;
info.template_name = entry.path().filename().string();
info.local_template_path = entry.path().string();
info.local_template_default_env_path = entry.path() / "_default.env";
template_info info(entry.path().filename().string(), entry.path().string());
// Check if template with same name already exists
bool duplicate = false;
auto it = std::find_if(templates.begin(), templates.end(),
[&info](const template_info& t) { return t.template_name == info.template_name; });
duplicate = (it!=templates.end());
//duplicate |= (info.template_name=="example"); // don't include the example template!
if (!duplicate) {
if (!duplicate)
templates.push_back(info);
}
}
}
};
// add templates from the local config directories
for (int i = 0; i < getNumConfigDirectories(); i++) {
std::string path = get_local_config_templates_path(i);
if (path.empty()) {
std::cerr << "Error: Templates directory not found: " << path << std::endl;
return false;
}
std::vector<std::string> template_config_directories;
get_all_template_config_directories(template_config_directories);
for (const auto& path : template_config_directories) {
add_templates_from_dir(path);
}
// add templates from the system templates directory
add_templates_from_dir(get_local_system_templates_path());
return true;
}
bool get_template_info(const std::string& template_name, template_info& info) {
std::vector<template_info> templates;
if (!get_templates(templates)) {
return false;
}
// add templates from the local config directories
std::vector<std::string> paths_to_search;
get_all_template_config_directories(paths_to_search);
auto it = std::find_if(templates.begin(), templates.end(),
[&template_name](const template_info& t) { return t.template_name == template_name; });
if (it != templates.end()) {
info = *it;
for (const auto& path : paths_to_search) {
std::filesystem::path full_path = path + "/" + template_name;
if (std::filesystem::exists(full_path))
{
info.template_name = template_name;
info.local_template_path = full_path.string();
return true;
}
}
std::cout << "Warning: Template '" << template_name << "' not found" << std::endl;
return false;
}
@ -185,4 +175,19 @@ void create_template(const std::string& template_name) {
std::cout << "Template '" << template_name << "' created at " << new_template_path << std::endl;
}
bool get_all_template_config_directories(std::vector<std::string> &template_config_directories)
{
template_config_directories.clear();
for (int i = 0; i < getNumConfigDirectories(); i++) {
std::string config_templates_path = get_local_config_templates_path(i);
if (config_templates_path.empty()) {
std::cerr << "Error: Templates directory not found: " << config_templates_path << std::endl;
return false;
}
template_config_directories.push_back(config_templates_path);
}
template_config_directories.push_back(get_local_system_templates_path());
return true;
}
} // namespace dropshell

View File

@ -1,23 +1,22 @@
#include <string>
#include <vector>
#include <filesystem>
namespace dropshell {
class template_info {
public:
template_info() {}
template_info(std::string n, std::string p) : template_name(n), local_template_path(p) {}
std::string template_name;
std::string local_template_path;
std::string local_template_default_env_path;
};
// templates are stored in two locations:
// templates are stored in multiple locations:
// 1. /opt/dropshell/templates
// 2. CONFIG_DIR/usertemplates (if it exists)
// we aggregate the templates from both locations and return them in the order of priority.
// if a template exists in both locations, the one in the user directory takes precedence.
// the template name is just the subfolder name in the templates directory.
// the template path is the path of that subfolder.
// 2. CONFIG_DIR/templates
bool get_templates(std::vector<template_info>& templates);
@ -32,4 +31,6 @@ void list_templates();
// 3. print out the README.txt file in the new template directory, and the path to the new template
void create_template(const std::string& template_name);
bool get_all_template_config_directories(std::vector<std::string>& template_config_directories);
} // namespace dropshell