Vaguely working

This commit is contained in:
Your Name 2025-05-04 20:34:46 +12:00
parent 8d2a66ee49
commit 3bfd6a3cba
3 changed files with 43 additions and 19 deletions

View File

@ -155,6 +155,9 @@ int main(int argc, char* argv[]) {
if (!gConfig().is_config_set())
return die("Please run 'dropshell edit' to set up the dropshell configuration.");
// load the template sources.
gTemplateManager().load_sources();
const std::vector<std::string> & server_definition_paths = gConfig().get_local_server_definition_paths();
if (server_definition_paths.size()>1) { // only show if there are multiple.
std::cout << "Server definition paths: ";

View File

@ -55,7 +55,7 @@
);
}
void template_manager::list_templates() {
void template_manager::list_templates() const {
auto templates = get_template_list();
if (templates.empty()) {
@ -76,7 +76,7 @@
std::cout << std::string(60, '-') << std::endl;
}
std::set<std::string> template_manager::get_template_list()
std::set<std::string> template_manager::get_template_list() const
{
std::set<std::string> templates;
for (const auto& source : mSources) {
@ -86,7 +86,7 @@
return templates;
}
bool template_manager::has_template(const std::string &template_name)
bool template_manager::has_template(const std::string &template_name) const
{
template_source_interface* source = get_source(template_name);
if (!source)
@ -94,7 +94,7 @@
return true;
}
template_info template_manager::get_template_info(const std::string &template_name)
template_info template_manager::get_template_info(const std::string &template_name) const
{
template_source_interface* source = get_source(template_name);
if (!source) {
@ -103,12 +103,17 @@
return source->get_template_info(template_name);
}
bool template_manager::template_command_exists(const std::string &template_name, const std::string &command)
bool template_manager::template_command_exists(const std::string &template_name, const std::string &command) const
{
return false;
template_source_interface* source = get_source(template_name);
if (!source) {
std::cerr << "Error: Template '" << template_name << "' not found" << std::endl;
return false;
}
return source->template_command_exists(template_name, command);
}
void template_manager::create_template(const std::string &template_name)
void template_manager::create_template(const std::string &template_name) const
{
// 1. Create a new directory in the user templates directory
std::vector<std::string> local_server_definition_paths = gConfig().get_local_server_definition_paths();
@ -192,6 +197,8 @@
void template_manager::load_sources()
{
ASSERT(mSources.empty());
ASSERT(gConfig().is_config_set());
ASSERT(!mLoaded);
auto local_template_paths = gConfig().get_template_local_paths();
if (local_template_paths.empty()) {
std::cerr << "Error: No local template paths found" << std::endl;
@ -206,6 +213,13 @@
for (const auto& url : registry_urls) {
mSources.push_back(std::make_unique<template_source_registry>(url));
}
std::cout << "Loaded " << mSources.size() << " template sources:" << std::endl;
for (const auto& source : mSources) {
std::cout << source->get_description() << std::endl;
}
mLoaded = true;
}
bool template_manager::required_file(std::string path, std::string template_name)
@ -217,7 +231,7 @@
return true;
}
template_source_interface *template_manager::get_source(const std::string &template_name)
template_source_interface *template_manager::get_source(const std::string &template_name) const
{
for (const auto& source : mSources) {
if (source->has_template(template_name)) {

View File

@ -37,6 +37,8 @@ class template_source_interface {
virtual bool has_template(const std::string& template_name) = 0;
virtual template_info get_template_info(const std::string& template_name) = 0;
virtual bool template_command_exists(const std::string& template_name,const std::string& command) = 0;
virtual std::string get_description() = 0;
};
class template_source_registry : public template_source_interface {
@ -49,6 +51,8 @@ class template_source_registry : public template_source_interface {
bool has_template(const std::string& template_name);
template_info get_template_info(const std::string& template_name);
bool template_command_exists(const std::string& template_name,const std::string& command);
std::string get_description() { return "Registry: " + mURL; }
private:
std::filesystem::path get_cache_dir();
private:
@ -64,6 +68,8 @@ class template_source_local : public template_source_interface {
bool has_template(const std::string& template_name);
template_info get_template_info(const std::string& template_name);
bool template_command_exists(const std::string& template_name,const std::string& command);
std::string get_description() { return "Local: " + mLocalPath.string(); }
private:
std::filesystem::path mLocalPath;
};
@ -73,24 +79,25 @@ class template_manager {
template_manager() : mLoaded(false) {}
~template_manager() {}
std::set<std::string> get_template_list();
bool has_template(const std::string& template_name);
template_info get_template_info(const std::string& template_name);
std::set<std::string> get_template_list() const;
bool has_template(const std::string& template_name) const;
template_info get_template_info(const std::string& template_name) const;
bool template_command_exists(const std::string& template_name,const std::string& command);
void create_template(const std::string& template_name);
bool test_template(const std::string& template_path);
bool template_command_exists(const std::string& template_name,const std::string& command) const;
void create_template(const std::string& template_name) const;
static bool test_template(const std::string& template_path);
void list_templates();
void list_templates() const;
void load_sources();
private:
void load_sources();
bool required_file(std::string path, std::string template_name);
template_source_interface* get_source(const std::string& template_name);
static bool required_file(std::string path, std::string template_name);
template_source_interface* get_source(const std::string& template_name) const;
private:
bool mLoaded;
std::vector<std::unique_ptr<template_source_interface>> mSources;
mutable std::vector<std::unique_ptr<template_source_interface>> mSources;
};
template_manager & gTemplateManager();