-
Notifications
You must be signed in to change notification settings - Fork 3
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
arthurmco
wants to merge
10
commits into
master
Choose a base branch
from
repo-class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a01df8c
WIP: Repository class refactoring
arthurmco 6fe6003
Add parse() and unparse() methods, fix more things
arthurmco 99f79ce
Add stringstream read specialization, as suggested by @dhilst
arthurmco 3f0f219
Move some common functions to functions.h
arthurmco 37f8b60
Remove this->
arthurmco ab4ba11
Fix repo class
arthurmco 25df8ed
Fix const ref and declaration name
arthurmco de966e5
Fix naming
arthurmco 4895e2e
Rename required files
arthurmco a834f94
fixup! Rename required files
arthurmco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2025 Arthur Mendes <[email protected]> | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef CLOYSTERHPC_FILE_H_ | ||
#define CLOYSTERHPC_FILE_H_ | ||
|
||
#include <filesystem> | ||
|
||
/** | ||
* Generic file class | ||
* | ||
* This class should only read and write, | ||
*/ | ||
class GenericFile { | ||
protected: | ||
std::filesystem::path m_path; | ||
|
||
public: | ||
explicit GenericFile(const std::filesystem::path& path) | ||
: m_path(path) | ||
{ | ||
} | ||
|
||
virtual void read() { } | ||
virtual void write() { } | ||
|
||
virtual ~GenericFile() = default; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2025 Arthur Mendes <[email protected]> | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef CLOYSTERHPC_REPOFILE_H_ | ||
#define CLOYSTERHPC_REPOFILE_H_ | ||
|
||
#include <vector> | ||
|
||
#include <glibmm/fileutils.h> | ||
#include <glibmm/keyfile.h> | ||
|
||
#include <cloysterhpc/file.h> | ||
#include <cloysterhpc/services/repo.h> | ||
|
||
struct ELCloneRepo { | ||
std::string group; | ||
std::string name; | ||
std::optional<std::string> base_url; | ||
std::optional<std::string> metalink; | ||
bool enabled; | ||
bool gpgcheck; | ||
std::string gpgkey; | ||
|
||
// (P/ todos os repositórios) | ||
// std::string release; | ||
}; | ||
|
||
/** | ||
* Repository file class | ||
* | ||
* This class should parse the repository data | ||
*/ | ||
class ELRepoFile : GenericFile { | ||
private: | ||
Glib::RefPtr<Glib::KeyFile> m_file; | ||
|
||
std::vector<ELCloneRepo> parseData(); | ||
void unparseData(const std::vector<ELCloneRepo>& data); | ||
|
||
std::vector<ELCloneRepo> m_repositories; | ||
|
||
public: | ||
explicit ELRepoFile(const std::filesystem::path& path) | ||
: GenericFile(path) | ||
{ | ||
} | ||
|
||
virtual void read() override; | ||
virtual void write() override; | ||
|
||
void parse(); | ||
void parse(const std::stringstream& ss); | ||
void unparse(); | ||
void unparse(std::stringstream& ss); | ||
|
||
[[nodiscard]] std::vector<ELCloneRepo>& getRepositories(); | ||
[[nodiscard]] const std::vector<ELCloneRepo>& getRepositoriesConst() const; | ||
|
||
~ELRepoFile() override = default; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <cloysterhpc/functions.h> | ||
#include <cloysterhpc/services/repofile.h> | ||
|
||
void ELRepoFile::read() | ||
{ | ||
try { | ||
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::write() { m_file->save_to_file(m_path.string()); } | ||
|
||
void ELRepoFile::parse() | ||
{ | ||
read(); | ||
|
||
m_repositories = parseData(); | ||
} | ||
|
||
void ELRepoFile::parse(const std::stringstream& ss) | ||
{ | ||
m_file = Glib::KeyFile::create(); | ||
m_file->load_from_data(ss.str().c_str()); | ||
m_repositories = parseData(); | ||
} | ||
|
||
std::vector<ELCloneRepo> ELRepoFile::parseData() | ||
{ | ||
auto reponames = m_file->get_groups(); | ||
|
||
std::vector<ELCloneRepo> repositories; | ||
|
||
for (const auto& repogroup : reponames) { | ||
auto name = m_file->get_string(repogroup, "name"); | ||
|
||
if (name.empty()) { | ||
throw repository_exception(std::format( | ||
"Could not load repo name from repo '{}' at m_file {}", | ||
repogroup.raw(), m_path.string())); | ||
} | ||
|
||
auto metalink = cloyster::readKeyfileString( | ||
m_file, std::string_view { repogroup.c_str() }, "metalink"); | ||
auto baseurl = cloyster::readKeyfileString( | ||
m_file, std::string_view { repogroup.c_str() }, "baseurl"); | ||
|
||
auto enabled = m_file->get_boolean(repogroup, "enabled"); | ||
auto gpgcheck = m_file->get_boolean(repogroup, "gpgcheck"); | ||
auto gpgkey = m_file->get_string(repogroup, "gpgkey"); | ||
|
||
ELCloneRepo repo; | ||
repo.group = repogroup.raw(); | ||
repo.name = name.raw(); | ||
repo.metalink | ||
= metalink.transform([](const auto& v) { return v.raw(); }); | ||
repo.base_url | ||
= baseurl.transform([](const auto& v) { return v.raw(); }); | ||
repo.enabled = enabled; | ||
repo.gpgcheck = gpgcheck; | ||
repo.gpgkey = gpgkey; | ||
repositories.push_back(std::move(repo)); | ||
} | ||
|
||
return repositories; | ||
} | ||
|
||
void ELRepoFile::unparseData(const std::vector<ELCloneRepo>& 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() | ||
{ | ||
unparseData(m_repositories); | ||
write(); | ||
} | ||
|
||
void ELRepoFile::unparse(std::stringstream& ss) | ||
{ | ||
unparseData(m_repositories); | ||
ss.seekp(0); | ||
ss << m_file->to_data(); | ||
} | ||
|
||
[[nodiscard]] std::vector<ELCloneRepo>& ELRepoFile::getRepositories() | ||
{ | ||
return m_repositories; | ||
} | ||
|
||
[[nodiscard]] const std::vector<ELCloneRepo>& | ||
ELRepoFile::getRepositoriesConst() const | ||
{ | ||
return m_repositories; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@viniciusferrao opinions on this? if we'll not be handling exceptions is there any point in creating our own exceptions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is just a WIP. But not sure. But it is a good practice to specialize the throw: https://isocpp.org/wiki/faq/exceptions#what-to-throw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a WIP