Skip to content

Commit

Permalink
Add parse() and unparse() methods, fix more things
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurmco committed Jan 30, 2025
1 parent a01df8c commit 6fe6003
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
24 changes: 15 additions & 9 deletions include/cloysterhpc/services/repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ class repository_exception : public std::runtime_error {
};

/**
* Repository file class
* Generic file class
*
* This class should only read and write,
*/
template <class Repo> class RepoFile {
class GenericFile {
protected:
std::filesystem::path m_path;
std::vector<Repo> m_repositories;

public:
RepoFile(const std::filesystem::path& path)
GenericFile(const std::filesystem::path& path)
: m_path(path)
{
}
Expand All @@ -91,19 +92,24 @@ struct ELRepo {

/**
* Repository file class
*
* This class should parse the repository data
*/
class ELRepoFile : public RepoFile<ELRepo> {
class ELRepoFile : GenericFile {
private:
Glib::RefPtr<Glib::KeyFile> loadFile();
Glib::RefPtr<Glib::KeyFile> m_file;

public:
ELRepoFile(const std::filesystem::path& path)
: RepoFile(path)
: GenericFile(path)
{
}

void read() override;
void write() override;
virtual void read() override;
virtual void write() override;

std::vector<ELRepo> parse();
void unparse(const std::vector<ELRepo>& repositories);
};

#endif // CLOYSTERHPC_REPO_H_
68 changes: 32 additions & 36 deletions src/services/repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,46 +57,50 @@ void repo::load_repository(std::filesystem::path path)
}
}

Glib::RefPtr<Glib::KeyFile> ELRepoFile::loadFile()
void ELRepoFile::read()
{
try {
auto file = Glib::KeyFile::create();
file->load_from_file(m_path.string());
return file;
m_file = Glib::KeyFile::create();
m_file->load_from_file(m_path.string());
} catch (Glib::FileError& e) {
throw repository_exception(
std::format("Could not load repository file {} ({})",
m_path.string(), e.what()));
}
}

void ELRepoFile::read()
void ELRepoFile::write() { m_file->save_to_file(m_path.string()); }

std::vector<ELRepo> ELRepoFile::parse()
{
printf("reading\n");
this->read();

auto reponames = m_file->get_groups();

auto file = loadFile();
std::vector<ELRepo> repositories;

auto reponames = file->get_groups();
auto get_optional_string
= [this](auto group, auto key) -> std::optional<Glib::ustring> {
return m_file->has_key(group, key)
? std::make_optional(m_file->get_string(group, key))
: std::nullopt;
};

for (const auto& repogroup : reponames) {
auto name = file->get_string(repogroup, "name");
auto name = m_file->get_string(repogroup, "name");

if (name.empty()) {
throw repository_exception(std::format(
"Could not load repo name from repo '{}' at file {}",
"Could not load repo name from repo '{}' at m_file {}",
repogroup.raw(), m_path.string()));
}

auto metalink = (file->has_key(repogroup, "metalink"))
? std::make_optional(file->get_string(repogroup, "metalink"))
: std::nullopt;
auto baseurl = (file->has_key(repogroup, "baseurl"))
? std::make_optional(file->get_string(repogroup, "baseurl"))
: std::nullopt;
auto metalink = get_optional_string(repogroup, "metalink");
auto baseurl = get_optional_string(repogroup, "baseurl");

auto enabled = file->get_boolean(repogroup, "enabled");
auto gpgcheck = file->get_boolean(repogroup, "gpgcheck");
auto gpgkey = file->get_string(repogroup, "gpgkey");
auto enabled = m_file->get_boolean(repogroup, "enabled");
auto gpgcheck = m_file->get_boolean(repogroup, "gpgcheck");
auto gpgkey = m_file->get_string(repogroup, "gpgkey");

ELRepo repo;
repo.group = repogroup.raw();
Expand All @@ -107,29 +111,21 @@ void ELRepoFile::read()
repo.enabled = enabled;
repo.gpgcheck = gpgcheck;
repo.gpgkey = gpgkey;
m_repositories.push_back(std::move(repo));
repositories.push_back(std::move(repo));
}

for (const auto& r : m_repositories) {
std::print("repo found '{}'\n", r.group);
std::print("\tname: '{}'\n", r.name);
std::print("\tbaseurl: '{}'\n", r.baseURL.value_or("null"));
std::print("\tmetalink: '{}'\n", r.metalink.value_or("null"));
std::print("\tenabled: {}\n", r.enabled);
std::print("\tgpg: (check: {}, key: {})\n", r.gpgcheck, r.gpgkey);
}
return repositories;
}

void ELRepoFile::write()
void ELRepoFile::unparse(const std::vector<ELRepo>& repositories)
{
auto file = loadFile();

for (const auto& repo : m_repositories) {
file->set_string(repo.group, "name", repo.name);
file->set_boolean(repo.group, "enabled", repo.enabled);
file->set_boolean(repo.group, "gpgcheck", repo.gpgcheck);
file->set_string(repo.group, "gpgkey", repo.gpgkey);
for (const auto& repo : repositories) {
m_file->set_string(repo.group, "name", repo.name);
m_file->set_boolean(repo.group, "enabled", repo.enabled);
m_file->set_boolean(repo.group, "gpgcheck", repo.gpgcheck);
m_file->set_string(repo.group, "gpgkey", repo.gpgkey);
}

file->save_to_file(m_path.string());
this->write();
}

0 comments on commit 6fe6003

Please sign in to comment.