Ajoute le namespace oki

This commit is contained in:
Colin FRIZOT
2022-10-10 11:06:29 +02:00
parent 73dbf64e7f
commit b9bfcc18bd
14 changed files with 176 additions and 145 deletions

View File

@@ -3,17 +3,18 @@
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
void invalidUsage(std::ostream &os) { namespace oki{
void invalidUsage(std::ostream &os) {
std::cerr << "Invalid usage. Hint: "; std::cerr << "Invalid usage. Hint: ";
} }
void help(std::ostream &os) { void help(std::ostream &os) {
os << "help: Show this help\n"; os << "help: Show this help\n";
os << "list: List available packages\n"; os << "list: List available packages\n";
os << "install: Install a new package\n"; os << "install: Install a new package\n";
} }
CliAction parseArguments(int argc, char *argv[]) { CliAction parseArguments(int argc, char *argv[]) {
if (argc < 2) { if (argc < 2) {
std::cerr << "See " << argv[0] << " help for all available actions.\n"; std::cerr << "See " << argv[0] << " help for all available actions.\n";
exit(0); exit(0);
@@ -32,4 +33,6 @@ CliAction parseArguments(int argc, char *argv[]) {
} else { } else {
exit(1); exit(1);
} }
}
} }

View File

@@ -3,12 +3,16 @@
#include <string_view> #include <string_view>
#include <variant> #include <variant>
struct ListAction { namespace oki{
}; struct ListAction {
struct InstallAction { };
struct InstallAction {
std::string_view packageName; std::string_view packageName;
explicit InstallAction(const char *packageName) : packageName{packageName} {} explicit InstallAction(const char *packageName) : packageName{packageName} {}
}; };
using CliAction = std::variant<ListAction, InstallAction>; using CliAction = std::variant<ListAction, InstallAction>;
CliAction parseArguments(int argc, char *argv[]);
}
CliAction parseArguments(int argc, char *argv[]);

View File

@@ -4,11 +4,13 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
fs::path getDefaultLocalRepository() { namespace oki{
fs::path getDefaultLocalRepository() {
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
const char *xdgData = std::getenv("XDG_DATA_HOME"); const char *xdgData = std::getenv("XDG_DATA_HOME");
if (xdgData == nullptr) { if (xdgData == nullptr) {
return std::getenv("HOME") / fs::path{".local/share/oki"}; return std::getenv("HOME") / fs::path{".local/share/oki"};
} }
return xdgData / fs::path{"oki"}; return xdgData / fs::path{"oki"};
}
} }

View File

@@ -2,4 +2,6 @@
#include <filesystem> #include <filesystem>
std::filesystem::path getDefaultLocalRepository(); namespace oki{
std::filesystem::path getDefaultLocalRepository();
}

View File

@@ -2,20 +2,21 @@
#include <curl/curl.h> #include <curl/curl.h>
static std::size_t writeCallback(char *in, std::size_t size, std::size_t nmemb, std::string *out) { namespace oki{
static std::size_t writeCallback(char *in, std::size_t size, std::size_t nmemb, std::string *out) {
std::size_t totalSize = size * nmemb; std::size_t totalSize = size * nmemb;
if (totalSize) { if (totalSize) {
out->append(in, totalSize); out->append(in, totalSize);
return totalSize; return totalSize;
} }
return 0; return 0;
} }
HttpRequest::HttpRequest(std::string_view url) : curl{curl_easy_init()}, url{url} { HttpRequest::HttpRequest(std::string_view url) : curl{curl_easy_init()}, url{url} {
curl_easy_setopt(curl, CURLOPT_URL, this->url.c_str()); curl_easy_setopt(curl, CURLOPT_URL, this->url.c_str());
} }
std::string HttpRequest::get() { std::string HttpRequest::get() {
std::string buffer; std::string buffer;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
@@ -24,14 +25,15 @@ std::string HttpRequest::get() {
throw RequestException{static_cast<int>(res)}; throw RequestException{static_cast<int>(res)};
} }
return buffer; return buffer;
} }
HttpRequest::~HttpRequest() { HttpRequest::~HttpRequest() {
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
RequestException::RequestException(int code) : code{code} {} RequestException::RequestException(int code) : code{code} {}
const char *RequestException::what() const noexcept { const char *RequestException::what() const noexcept {
return curl_easy_strerror(static_cast<CURLcode>(code)); return curl_easy_strerror(static_cast<CURLcode>(code));
}
} }

View File

@@ -2,20 +2,22 @@
#include <string> #include <string>
class HttpRequest { namespace oki{
private: class HttpRequest {
private:
void *curl; void *curl;
std::string url; std::string url;
public: public:
explicit HttpRequest(std::string_view url); explicit HttpRequest(std::string_view url);
std::string get(); std::string get();
~HttpRequest(); ~HttpRequest();
}; };
class RequestException : public std::exception { class RequestException : public std::exception {
private: private:
int code; int code;
public: public:
explicit RequestException(int code); explicit RequestException(int code);
const char *what() const noexcept override; const char *what() const noexcept override;
}; };
}

View File

@@ -4,6 +4,8 @@
#include "cli/options.h" #include "cli/options.h"
#include "repository/RemoteRepository.h" #include "repository/RemoteRepository.h"
using namespace oki;
std::size_t writeCallback(char *in, size_t size, size_t nmemb, std::string *out){ std::size_t writeCallback(char *in, size_t size, size_t nmemb, std::string *out){
std::size_t total_size = size * nmemb; std::size_t total_size = size * nmemb;
if (total_size){ if (total_size){

View File

@@ -1,11 +1,13 @@
#include "Package.h" #include "Package.h"
Package::Package(std::string_view shortName, std::string_view longName) : shortName{shortName}, longName{longName} {} namespace oki{
Package::Package(std::string_view shortName, std::string_view longName) : shortName{shortName}, longName{longName} {}
const std::string& Package::getShortName() const { const std::string& Package::getShortName() const {
return shortName; return shortName;
} }
const std::string& Package::getLongName() const { const std::string& Package::getLongName() const {
return longName; return longName;
}
} }

View File

@@ -2,12 +2,14 @@
#include <string> #include <string>
class Package { namespace oki{
private: class Package {
private:
std::string shortName; std::string shortName;
std::string longName; std::string longName;
public: public:
Package(std::string_view shortName, std::string_view longName); Package(std::string_view shortName, std::string_view longName);
const std::string& getShortName() const; const std::string& getShortName() const;
const std::string& getLongName() const; const std::string& getLongName() const;
}; };
}

View File

@@ -4,20 +4,22 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
LocalRepository::LocalRepository(fs::path root) : root{std::move(root)} {} namespace oki{
LocalRepository::LocalRepository(fs::path root) : root{std::move(root)} {}
void LocalRepository::createIfNotExists() { void LocalRepository::createIfNotExists() {
fs::create_directories(root); fs::create_directories(root);
} }
std::vector<Package> LocalRepository::listPackages() { std::vector<Package> LocalRepository::listPackages() {
std::vector<Package> packages; std::vector<Package> packages;
for (const auto& file : fs::directory_iterator(root)) { for (const auto& file : fs::directory_iterator(root)) {
packages.emplace_back(file.path().filename().string(), ""); packages.emplace_back(file.path().filename().string(), "");
} }
return packages; return packages;
} }
void LocalRepository::download(std::string_view packageName, const fs::path& destination) { void LocalRepository::download(std::string_view packageName, const fs::path& destination) {
std::cerr << "TODO : downloading " << packageName << "\n"; std::cerr << "TODO : downloading " << packageName << "\n";
}
} }

View File

@@ -2,12 +2,14 @@
#include "Repository.h" #include "Repository.h"
class LocalRepository : public Repository { namespace oki{
private: class LocalRepository : public Repository {
private:
std::filesystem::path root; std::filesystem::path root;
public: public:
explicit LocalRepository(std::filesystem::path root); explicit LocalRepository(std::filesystem::path root);
void createIfNotExists(); void createIfNotExists();
std::vector<Package> listPackages() override; std::vector<Package> listPackages() override;
void download(std::string_view packageName, const std::filesystem::path& destination) override; void download(std::string_view packageName, const std::filesystem::path& destination) override;
}; };
}

View File

@@ -5,9 +5,10 @@
using json = nlohmann::json; using json = nlohmann::json;
RemoteRepository::RemoteRepository(std::string_view apiUrl) : apiUrl{apiUrl} {} namespace oki{
RemoteRepository::RemoteRepository(std::string_view apiUrl) : apiUrl{apiUrl} {}
std::vector<Package> RemoteRepository::listPackages() { std::vector<Package> RemoteRepository::listPackages() {
HttpRequest request{apiUrl + "?api=list"}; HttpRequest request{apiUrl + "?api=list"};
json data = json::parse(request.get()); json data = json::parse(request.get());
std::vector<Package> packages; std::vector<Package> packages;
@@ -15,8 +16,9 @@ std::vector<Package> RemoteRepository::listPackages() {
packages.emplace_back(item.at("short_name").get<std::string>(), item.at("long_name").get<std::string>()); packages.emplace_back(item.at("short_name").get<std::string>(), item.at("long_name").get<std::string>());
} }
return packages; return packages;
} }
void RemoteRepository::download(std::string_view packageName, const std::filesystem::path &destination) { void RemoteRepository::download(std::string_view packageName, const std::filesystem::path &destination) {
}
} }

View File

@@ -2,11 +2,13 @@
#include "Repository.h" #include "Repository.h"
class RemoteRepository : public Repository { namespace oki{
private: class RemoteRepository : public Repository {
private:
std::string apiUrl; std::string apiUrl;
public: public:
explicit RemoteRepository(std::string_view apiUrl); explicit RemoteRepository(std::string_view apiUrl);
std::vector<Package> listPackages() override; std::vector<Package> listPackages() override;
void download(std::string_view packageName, const std::filesystem::path& destination) override; void download(std::string_view packageName, const std::filesystem::path& destination) override;
}; };
}

View File

@@ -6,9 +6,11 @@
#include "../package/Package.h" #include "../package/Package.h"
class Repository { namespace oki{
public: class Repository {
public:
virtual std::vector<Package> listPackages() = 0; virtual std::vector<Package> listPackages() = 0;
virtual void download(std::string_view packageName, const std::filesystem::path& destination) = 0; virtual void download(std::string_view packageName, const std::filesystem::path& destination) = 0;
virtual ~Repository() = default; virtual ~Repository() = default;
}; };
}