Update source/src/templates.cpp
This commit is contained in:
@@ -125,6 +125,14 @@
|
||||
|
||||
bool template_source_registry::has_template(const std::string& template_name)
|
||||
{
|
||||
// First check if we have it cached
|
||||
std::filesystem::path cache_dir = get_cache_dir();
|
||||
std::filesystem::path template_cache_dir = cache_dir / template_name;
|
||||
if (std::filesystem::exists(template_cache_dir)) {
|
||||
// We have it cached, so return true
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if template exists in registry
|
||||
std::string check_url = mRegistry.url + "/exists/" + template_name + ":latest";
|
||||
|
||||
@@ -143,10 +151,14 @@
|
||||
file >> json_response;
|
||||
file.close();
|
||||
}
|
||||
} catch (...) {
|
||||
// Failed to parse JSON
|
||||
} catch (const std::exception& e) {
|
||||
warning << "Failed to parse JSON response from " << check_url << ": " << e.what() << std::endl;
|
||||
}
|
||||
std::filesystem::remove(temp_file);
|
||||
} else {
|
||||
// curl failed - might be network issue or server down
|
||||
// Check cache as fallback
|
||||
return std::filesystem::exists(template_cache_dir);
|
||||
}
|
||||
} else {
|
||||
json_response = get_json_from_url(check_url);
|
||||
@@ -156,16 +168,12 @@
|
||||
return json_response["exists"].get<bool>();
|
||||
}
|
||||
|
||||
return false;
|
||||
// If registry check failed but we have cache, use cache
|
||||
return std::filesystem::exists(template_cache_dir);
|
||||
}
|
||||
|
||||
template_info template_source_registry::get_template_info(const std::string& template_name)
|
||||
{
|
||||
// Check if template exists in registry
|
||||
if (!has_template(template_name)) {
|
||||
return template_info();
|
||||
}
|
||||
|
||||
// Get cache directory
|
||||
std::filesystem::path cache_dir = get_cache_dir();
|
||||
std::filesystem::path template_cache_dir = cache_dir / template_name;
|
||||
@@ -176,6 +184,14 @@
|
||||
std::filesystem::create_directories(cache_dir);
|
||||
}
|
||||
|
||||
// If we have a cached version and can't reach the registry, use the cache
|
||||
bool have_cache = std::filesystem::exists(template_cache_dir) && std::filesystem::exists(template_json_file);
|
||||
|
||||
// Check if template exists (in cache or registry)
|
||||
if (!has_template(template_name)) {
|
||||
return template_info();
|
||||
}
|
||||
|
||||
// Get metadata from registry to check version
|
||||
std::string meta_url = mRegistry.url + "/meta/" + template_name + ":latest";
|
||||
|
||||
@@ -204,6 +220,15 @@
|
||||
}
|
||||
|
||||
if (registry_metadata.is_null()) {
|
||||
// If we can't get metadata from registry but have cache, use cache
|
||||
if (have_cache) {
|
||||
info << "Registry unavailable, using cached template: " << template_name << std::endl;
|
||||
return template_info(
|
||||
template_name,
|
||||
"Registry: " + mRegistry.name + " (cached)",
|
||||
template_cache_dir
|
||||
);
|
||||
}
|
||||
warning << "Failed to get metadata for template: " << template_name << std::endl;
|
||||
return template_info();
|
||||
}
|
||||
|
Reference in New Issue
Block a user