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
5 changes: 5 additions & 0 deletions include/cloysterhpc/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
#include <boost/process/child.hpp>
#include <boost/process/pipe.hpp>
#include <filesystem>
#include <glibmm/ustring.h>
#include <list>
#include <optional>
#include <string>
#include <vector>

#include <boost/asio.hpp>
#include <glibmm/keyfile.h>

namespace cloyster {
// Globals
Expand Down Expand Up @@ -164,6 +166,9 @@ std::string findAndReplace(const std::string_view& source,
*/
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);
Copy link
Owner

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.

Copy link
Collaborator Author

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


} /* namespace cloyster */

#endif // CLOYSTERHPC_FUNCTIONS_H_
29 changes: 14 additions & 15 deletions include/cloysterhpc/services/repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,65 +58,64 @@ class repository_exception : public std::runtime_error {
}
};



/**
* Generic file class
*
* This class should only read and write,
* This class should only read and write,
*/
class GenericFile {
protected:
std::filesystem::path m_path;

public:
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;
std::optional<std::string> baseURL;
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:
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)
: GenericFile(path)
{}
{
}

virtual void read() override;
virtual 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 );
void unparse(const std::vector<ELRepo>& repositories);
void unparse(
const std::vector<ELRepo>& repositories, std::stringstream& ss);
};

#endif // CLOYSTERHPC_REPO_H_
8 changes: 8 additions & 0 deletions src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

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

No need to be const ref.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

{
return file->has_key(group.data(), key.data())
? std::make_optional(file->get_string(group.data(), key.data()))
: std::nullopt;
}

} // namespace cloyster
42 changes: 19 additions & 23 deletions src/services/repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -73,32 +74,24 @@ void ELRepoFile::write() { m_file->save_to_file(m_path.string()); }

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

return this->parseData();
return 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();
return parseData();
}


std::vector<ELRepo> ELRepoFile::parseData()
std::vector<ELRepo> ELRepoFile::parseData()
{
auto reponames = m_file->get_groups();

std::vector<ELRepo> repositories;

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 = m_file->get_string(repogroup, "name");

Expand All @@ -108,8 +101,10 @@ std::vector<ELRepo> ELRepoFile::parseData()
repogroup.raw(), m_path.string()));
}

auto metalink = get_optional_string(repogroup, "metalink");
auto baseurl = get_optional_string(repogroup, "baseurl");
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");
Expand All @@ -120,7 +115,8 @@ std::vector<ELRepo> ELRepoFile::parseData()
repo.name = name.raw();
repo.metalink
= metalink.transform([](const auto& v) { return v.raw(); });
repo.baseURL = baseurl.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;
Expand All @@ -131,25 +127,25 @@ std::vector<ELRepo> ELRepoFile::parseData()
}

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

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