FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
remote.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <filesystem>
12#include <string>
13
14#include "../std/expected.hpp"
16#include "db.hpp"
17
27namespace ns_db::ns_remote
28{
29
30namespace
31{
32
33namespace fs = std::filesystem;
34
35}
36
44[[nodiscard]] inline Value<void> set(fs::path const& path_file_binary, std::string const& url)
45{
46 // Create database
47 ns_db::Db db;
48 // Insert URL
49 db("url") = url;
50 logger("I::Set remote URL to '{}'", url);
51 // Write to the database
52 Pop(ns_reserved::ns_remote::write(path_file_binary, Pop(db.dump())));
53 return {};
54}
55
62[[nodiscard]] inline Value<std::string> get(fs::path const& path_file_binary)
63{
64 // Read database
66 Pop(ns_reserved::ns_remote::read(path_file_binary))
67 ).value_or(ns_db::Db());
68 // Check if URL exists
69 if (db.empty() or not db.contains("url"))
70 {
71 return Error("E::No remote URL configured");
72 }
73 // Return URL
74 return Pop(db("url").value<std::string>(), "E::Could not read URL");
75}
76
83[[nodiscard]] inline Value<void> clear(fs::path const& path_file_binary)
84{
85 // Create empty database
86 ns_db::Db db;
87 // Write empty database
88 Pop(ns_reserved::ns_remote::write(path_file_binary, Pop(db.dump())));
89 logger("I::Cleared remote URL");
90 return {};
91}
92
93} // namespace ns_db::ns_remote
94
95/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
A type-safe wrapper around nlohmann::json for database operations.
Definition db.hpp:64
bool contains(T &&t) const noexcept
Checks if the JSON contains a specific key.
Definition db.hpp:309
Value< std::string > dump()
Serializes the current JSON data to a formatted string.
Definition db.hpp:277
bool empty() const noexcept
Checks whether the JSON data is empty.
Definition db.hpp:291
A database that interfaces with nlohmann json.
Enhanced error handling framework built on std::expected.
#define logger(fmt,...)
Compile-time log level dispatch macro with automatic location capture.
Definition log.hpp:682
Remote recipe repository URL management.
Value< std::string > get(fs::path const &path_file_binary)
Gets the remote URL from the database.
Definition remote.hpp:62
Value< void > clear(fs::path const &path_file_binary)
Clears the remote URL from the database.
Definition remote.hpp:83
Value< void > set(fs::path const &path_file_binary, std::string const &url)
Sets a remote URL in the database.
Definition remote.hpp:44
Value< Db > from_string(S &&s)
Parses a JSON string and creates a Db object.
Definition db.hpp:496
Value< std::string > read(fs::path const &path_file_binary)
Reads the remote URL string from the target binary.
Definition remote.hpp:65
Value< void > write(fs::path const &path_file_binary, std::string_view const &url)
Writes the remote URL string to the target binary.
Definition remote.hpp:45
Manages the remote URL reserved space.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44