-
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
a01df8c
6fe6003
99f79ce
3f0f219
37f8b60
ab4ba11
25df8ed
de966e5
4895e2e
a834f94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
#include <string_view> | ||
#include <vector> | ||
|
||
#include <glibmm/fileutils.h> | ||
#include <glibmm/keyfile.h> | ||
|
||
/** | ||
* @brief This class represents an EL .repo file as it's found in | ||
* /etc/yum.repos.d/ | ||
|
@@ -47,4 +50,80 @@ class repo { | |
static void load_repository(std::filesystem::path path); | ||
}; | ||
|
||
class repository_exception : public std::runtime_error { | ||
public: | ||
explicit repository_exception(const std::string& msg) | ||
: std::runtime_error(msg) | ||
{ | ||
} | ||
}; | ||
Comment on lines
+53
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. This is just a WIP |
||
|
||
/** | ||
* 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; | ||
}; | ||
|
||
struct ELRepo { | ||
dhilst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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<ELRepo> parseData(); | ||
void unparseData(const std::vector<ELRepo>&); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing the variable name. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
std::vector<ELRepo> 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<ELRepo>& getRepositories(); | ||
[[nodiscard]] const std::vector<ELRepo>& getRepositoriesConst() const; | ||
|
||
~ELRepoFile() override = default; | ||
}; | ||
|
||
#endif // CLOYSTERHPC_REPO_H_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,4 +351,12 @@ void copyFile(std::filesystem::path source, std::filesystem::path destination) | |
} | ||
} | ||
|
||
std::optional<Glib::ustring> readKeyfileString(Glib::RefPtr<Glib::KeyFile> file, | ||
const std::string_view& group, const std::string_view& key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to be const ref. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See this comment |
||
{ | ||
return file->has_key(group.data(), key.data()) | ||
? std::make_optional(file->get_string(group.data(), key.data())) | ||
: std::nullopt; | ||
} | ||
|
||
} // namespace cloyster |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <cloysterhpc/functions.h> | ||
#include <cloysterhpc/services/repo.h> | ||
#include <fmt/printf.h> | ||
#include <glibmm/fileutils.h> | ||
|
@@ -56,3 +57,105 @@ void repo::load_repository(std::filesystem::path path) | |
fmt::print("KeyFile Error: {}\n", ex.what()); | ||
} | ||
} | ||
|
||
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 {} ({})", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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<ELRepo> ELRepoFile::parseData() | ||
{ | ||
auto reponames = m_file->get_groups(); | ||
|
||
std::vector<ELRepo> 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 {}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you suggest? |
||
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"); | ||
|
||
ELRepo 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<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() | ||
{ | ||
unparseData(m_repositories); | ||
write(); | ||
} | ||
|
||
void ELRepoFile::unparse(std::stringstream& ss) | ||
{ | ||
unparseData(m_repositories); | ||
ss.seekp(0); | ||
ss << m_file->to_data(); | ||
} | ||
|
||
[[nodiscard]] std::vector<ELRepo>& ELRepoFile::getRepositories() | ||
{ | ||
return m_repositories; | ||
} | ||
|
||
[[nodiscard]] const std::vector<ELRepo>& | ||
ELRepoFile::getRepositoriesConst() const | ||
{ | ||
return m_repositories; | ||
} |
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.
std:string_view
is already a pointer to a string. It should not be passed as const ref.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 didn't know that. I thought it was pointer+length. It will be fixed