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

View File

@@ -3,12 +3,16 @@
#include <string_view>
#include <variant>
struct ListAction {
};
struct InstallAction {
namespace oki{
struct ListAction {
};
struct InstallAction {
std::string_view 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;
fs::path getDefaultLocalRepository() {
namespace oki{
fs::path getDefaultLocalRepository() {
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
const char *xdgData = std::getenv("XDG_DATA_HOME");
if (xdgData == nullptr) {
return std::getenv("HOME") / fs::path{".local/share/oki"};
}
return xdgData / fs::path{"oki"};
}
}

View File

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

View File

@@ -2,20 +2,21 @@
#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;
if (totalSize) {
out->append(in, totalSize);
return totalSize;
}
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());
}
}
std::string HttpRequest::get() {
std::string HttpRequest::get() {
std::string buffer;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
@@ -24,14 +25,15 @@ std::string HttpRequest::get() {
throw RequestException{static_cast<int>(res)};
}
return buffer;
}
}
HttpRequest::~HttpRequest() {
HttpRequest::~HttpRequest() {
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));
}
}

View File

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

View File

@@ -4,6 +4,8 @@
#include "cli/options.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 total_size = size * nmemb;
if (total_size){

View File

@@ -1,11 +1,13 @@
#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;
}
}
const std::string& Package::getLongName() const {
const std::string& Package::getLongName() const {
return longName;
}
}

View File

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

View File

@@ -4,20 +4,22 @@
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);
}
}
std::vector<Package> LocalRepository::listPackages() {
std::vector<Package> LocalRepository::listPackages() {
std::vector<Package> packages;
for (const auto& file : fs::directory_iterator(root)) {
packages.emplace_back(file.path().filename().string(), "");
}
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";
}
}

View File

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

View File

@@ -5,9 +5,10 @@
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"};
json data = json::parse(request.get());
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>());
}
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"
class RemoteRepository : public Repository {
private:
namespace oki{
class RemoteRepository : public Repository {
private:
std::string apiUrl;
public:
public:
explicit RemoteRepository(std::string_view apiUrl);
std::vector<Package> listPackages() override;
void download(std::string_view packageName, const std::filesystem::path& destination) override;
};
};
}

View File

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