From a5a12e3689e02af12f68e4868359ee5e65f9a056 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 30 Mar 2026 15:50:09 +1300 Subject: [PATCH] Move template list cache to ~/.dropshell/temp_files/ out of git repos --- source/src/templates.cpp | 50 +++++++++++++++------- source/src/templates.hpp | 4 +- source/src/utils/directories.hpp | 2 +- source/src/utils/service_env_overrides.cpp | 2 +- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/source/src/templates.cpp b/source/src/templates.cpp index 84f6d0c..0159265 100644 --- a/source/src/templates.cpp +++ b/source/src/templates.cpp @@ -18,6 +18,13 @@ namespace dropshell { + // Return path in ~/.dropshell/temp_files/ for caching template lists (keeps cache out of git repos) + static std::filesystem::path get_list_cache_path(const std::string& local_path) { + size_t h = std::hash{}(local_path); + std::string filename = "templates_" + std::to_string(h) + ".list"; + return std::filesystem::path(localpath::temp_files()) / filename; + } + // ------------------------------------------------------------------------------------------------ // template_manager — loading and resolution // ------------------------------------------------------------------------------------------------ @@ -94,18 +101,18 @@ return; } - // Try to read dropshell-templates.list - std::filesystem::path list_file = std::filesystem::path(src.local_path) / filenames::dropshell_templates_list; + // Try to read cached template list from ~/.dropshell/temp_files/ + std::filesystem::path list_file = get_list_cache_path(src.local_path); std::vector template_dirs; if (std::filesystem::exists(list_file)) { - template_dirs = parse_list_file(src.local_path); + template_dirs = parse_list_file(list_file.string()); } else { // Discover templates by searching for template_info.env template_dirs = discover_templates(src.local_path); // Write the discovered list so next time is fast if (!template_dirs.empty()) - write_list_file(src.local_path, template_dirs); + write_list_file(list_file.string(), template_dirs); } // Remove stale entries for this source from mTemplateMap before re-adding @@ -130,11 +137,10 @@ } } - std::vector template_manager::parse_list_file(const std::string& local_path) const + std::vector template_manager::parse_list_file(const std::string& list_file_path) const { std::vector result; - std::filesystem::path list_file = std::filesystem::path(local_path) / filenames::dropshell_templates_list; - std::ifstream f(list_file); + std::ifstream f(list_file_path); if (!f.is_open()) return result; @@ -181,18 +187,16 @@ return result; } - void template_manager::write_list_file(const std::string& local_path, const std::vector& template_dirs) const + void template_manager::write_list_file(const std::string& list_file_path, const std::vector& template_dirs) const { - std::filesystem::path list_file = std::filesystem::path(local_path) / filenames::dropshell_templates_list; - std::ofstream f(list_file); + std::ofstream f(list_file_path); if (!f.is_open()) { - debug << "Could not write " << list_file << std::endl; + debug << "Could not write " << list_file_path << std::endl; return; } - f << "# Auto-generated by dropshell — template directories relative to this file" << std::endl; + f << "# Auto-generated by dropshell — template directories (cached)" << std::endl; for (const auto& dir : template_dirs) f << dir << std::endl; - info << "Wrote " << list_file << " (" << template_dirs.size() << " templates)" << std::endl; } // ------------------------------------------------------------------------------------------------ @@ -270,6 +274,13 @@ // else: local-only, no .git — skip } + // Delete cached list files so templates are re-discovered from fresh state + for (const auto& src : mSources) { + std::filesystem::path list_file = get_list_cache_path(src.local_path); + if (std::filesystem::exists(list_file)) + std::filesystem::remove(list_file); + } + // Re-resolve all templates after pulling resolve_templates(); return true; @@ -288,7 +299,14 @@ if (!std::filesystem::exists(std::filesystem::path(src.local_path) / ".git")) return true; // no .git dir, nothing to pull - git_pull_source(src); + if (!git_pull_source(src)) + warning << "Failed to pull latest template source for '" << template_name << "' from " << src.local_path << std::endl; + + // Delete cached list file so templates are re-discovered from fresh state + std::filesystem::path list_file = get_list_cache_path(src.local_path); + if (std::filesystem::exists(list_file)) + std::filesystem::remove(list_file); + // Re-resolve this source's templates resolve_source(it->second.source_index); return true; @@ -518,8 +536,8 @@ docker logs "$CONTAINER_NAME" "$@" )BASH"; if (!write_template_file(new_template_path + "/logs.sh", logs_sh, true)) return false; - // 5. Update dropshell-templates.list - std::filesystem::path list_file = std::filesystem::path(base_path) / filenames::dropshell_templates_list; + // 5. Update cached template list + std::filesystem::path list_file = get_list_cache_path(base_path); std::ofstream lf(list_file, std::ios::app); if (lf.is_open()) { lf << template_name << std::endl; diff --git a/source/src/templates.hpp b/source/src/templates.hpp index b9e474f..c897a6e 100644 --- a/source/src/templates.hpp +++ b/source/src/templates.hpp @@ -72,13 +72,13 @@ class template_manager { // Template discovery and list file management std::vector discover_templates(const std::string& local_path) const; - void write_list_file(const std::string& local_path, const std::vector& template_dirs) const; + void write_list_file(const std::string& list_file_path, const std::vector& template_dirs) const; private: static bool required_file(std::string path, std::string template_name); void resolve_templates(); void resolve_source(size_t source_index); - std::vector parse_list_file(const std::string& local_path) const; + std::vector parse_list_file(const std::string& list_file_path) const; bool git_pull_source(const TemplateSource& source); bool git_clone_source(const TemplateSource& source); diff --git a/source/src/utils/directories.hpp b/source/src/utils/directories.hpp index cfa4b91..52f0ae0 100644 --- a/source/src/utils/directories.hpp +++ b/source/src/utils/directories.hpp @@ -42,7 +42,7 @@ namespace dropshell { static const std::string dropshell_json = "dropshell.json"; static const std::string ds_run = "ds_run.sh"; static const std::string template_paths_json = "template_paths.json"; - static const std::string dropshell_templates_list = "dropshell-templates.list"; + static const std::string overrides_env = "overrides.env"; } // namespace filenames. diff --git a/source/src/utils/service_env_overrides.cpp b/source/src/utils/service_env_overrides.cpp index 552cf84..b2ffde9 100644 --- a/source/src/utils/service_env_overrides.cpp +++ b/source/src/utils/service_env_overrides.cpp @@ -73,7 +73,7 @@ std::vector apply_overrides( // Apply each override, skipping variables not defined in the template for (const auto& [key, new_val] : override_vars) { if (!allowed_vars.empty() && allowed_vars.find(key) == allowed_vars.end()) { - warning << "Override skipped: " << key << " is not defined in the template for " << service_name << std::endl; + debug << "Override skipped: " << key << " is not defined in the template for " << service_name << std::endl; continue; } std::string current_val = service_env.get_variable(key);