Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Repository class refactoring #95

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
47 changes: 30 additions & 17 deletions include/cloysterhpc/services/repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,27 @@ 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)
{
}
{}

virtual void read() { }
virtual void write() { }
virtual void read() {}
virtual void write() {}
};



struct ELRepo {
dhilst marked this conversation as resolved.
Show resolved Hide resolved
std::string group;
std::string name;
Expand All @@ -87,23 +90,33 @@ struct ELRepo {

// (P/ todos os repositórios)
// std::string release;

};


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

std::vector<ELRepo> parseData();
void unparseData(const std::vector<ELRepo>&);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the variable name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name is not needed in the declaration and the context is obvious, but I will fixed because of conventions

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

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

void read() override;
void write() override;
std::vector<ELRepo> parse();
std::vector<ELRepo> parse(const std::stringstream& ss);
void unparse( const std::vector<ELRepo>& repositories );
void unparse( const std::vector<ELRepo>& repositories, std::stringstream& ss );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void unparse( const std::vector<ELRepo>& repositories );
void unparse( const std::vector<ELRepo>& repositories, std::stringstream& ss );
void unparse(const std::vector<ELRepo>& repositories);
void unparse(const std::vector<ELRepo>& repositories, std::stringstream& ss);

};

#endif // CLOYSTERHPC_REPO_H_
96 changes: 58 additions & 38 deletions src/services/repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,46 +57,63 @@ 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 {} ({})",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt::format.

m_path.string(), e.what()));
}
}

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

std::vector<ELRepo> ELRepoFile::parse()
{
this->read();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need "this"?


return this->parseData();
}

std::vector<ELRepo> ELRepoFile::parse(const std::stringstream& ss)
{
m_file = Glib::KeyFile::create();
m_file->load_from_data(ss.str().c_str());
return this->parseData();
}
Copy link
Collaborator

@dhilst dhilst Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, now

  • Add m_repositories member as a vector of ELRepo with a getter
  • The getter should load the data into the member if the member is empty and return at the end

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inside of ELRepo?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inside ELRepoFile



std::vector<ELRepo> ELRepoFile::parseData()
{
printf("reading\n");
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;
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why this captures this, anyway, move it to functions.cpp


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 {}",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more than a DEBUG message for the developer than to the user. For the user it should be something more friendly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest?

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 +124,32 @@ 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::unparseData(const std::vector<ELRepo>& repositories)
{
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);
}
}

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);
}
this->unparseData(repositories);
this->write();
}

file->save_to_file(m_path.string());

void ELRepoFile::unparse(const std::vector<ELRepo>& repositories, std::stringstream& ss)
{
this->unparseData(repositories);
ss.seekp(0);
ss << m_file->to_data();
}
Loading