Move template list cache to ~/.dropshell/temp_files/ out of git repos
This commit is contained in:
@@ -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<std::string>{}(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<std::string> 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<std::string> template_manager::parse_list_file(const std::string& local_path) const
|
||||
std::vector<std::string> template_manager::parse_list_file(const std::string& list_file_path) const
|
||||
{
|
||||
std::vector<std::string> 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<std::string>& template_dirs) const
|
||||
void template_manager::write_list_file(const std::string& list_file_path, const std::vector<std::string>& 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;
|
||||
|
||||
@@ -72,13 +72,13 @@ class template_manager {
|
||||
|
||||
// Template discovery and list file management
|
||||
std::vector<std::string> discover_templates(const std::string& local_path) const;
|
||||
void write_list_file(const std::string& local_path, const std::vector<std::string>& template_dirs) const;
|
||||
void write_list_file(const std::string& list_file_path, const std::vector<std::string>& 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<std::string> parse_list_file(const std::string& local_path) const;
|
||||
std::vector<std::string> parse_list_file(const std::string& list_file_path) const;
|
||||
bool git_pull_source(const TemplateSource& source);
|
||||
bool git_clone_source(const TemplateSource& source);
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ std::vector<OverrideChange> 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);
|
||||
|
||||
Reference in New Issue
Block a user