35namespace fs = std::filesystem;
43[[nodiscard]]
inline std::unordered_map<std::string,std::string>
map(std::vector<std::string>
const& entries)
46 | std::views::filter([](
auto&& e){
return e.find(
'=') != std::string::npos; })
47 | std::views::transform([](
auto&& e)
49 auto it = e.find(
'=');
50 return std::make_pair(e.substr(0, it), e.substr(it+1, std::string::npos));
52 | std::ranges::to<std::unordered_map<std::string,std::string>>();
65 for (
auto&& entry : entries)
67 if (std::ranges::count_if(entry, [](
char c){
return c ==
'='; }) == 0)
69 return Error(
"C::Variable assignment '{}' is invalid", entry);
83[[nodiscard]]
inline Value<void> del(fs::path
const& path_file_binary, std::vector<std::string>
const& entries)
86 std::ranges::for_each(entries, [&](
auto const& entry)
90 logger(
"I::Erase key '{}'", entry);
94 logger(
"I::Key '{}' not found for deletion", entry);
108[[nodiscard]]
inline Value<void> add(fs::path
const& path_file_binary, std::vector<std::string>
const& entries)
111 Pop(validate(entries));
114 for (
auto&& [key,value] : map(entries))
117 logger(
"I::Included variable '{}' with value '{}'", key, value);
131[[nodiscard]]
inline Value<void> set(fs::path
const& path_file_binary, std::vector<std::string>
const& entries)
134 Pop(
add(path_file_binary, entries));
149 std::vector<std::string> environment;
150 for (
auto&& [key,value] : db.
items())
152 environment.push_back(std::format(
"{}={}", key, Pop(value.template value<std::string>())));
155 for(
auto& variable : environment)
A type-safe wrapper around nlohmann::json for database operations.
std::vector< std::pair< std::string, Db > > items() const noexcept
Retrieves key-value pairs as Db objects from the current JSON.
bool erase(T &&t)
Removes a key from the JSON structure.
Value< std::string > dump()
Serializes the current JSON data to a formatted string.
A database that interfaces with nlohmann json.
Enhanced error handling framework built on std::expected.
A library for manipulating environment variables.
#define logger(fmt,...)
Compile-time log level dispatch macro with automatic location capture.
Value< void > validate(std::vector< std::string > const &entries)
Validates environment variable entries.
std::unordered_map< std::string, std::string > map(std::vector< std::string > const &entries)
Given a string with an environment variable entry, splits the variable into key and value.
Environment variable database management.
Value< void > del(fs::path const &path_file_binary, std::vector< std::string > const &entries)
Deletes a list of environment variables from the database.
Value< std::vector< std::string > > get(fs::path const &path_file_binary)
Get existing variables from the database.
Value< void > add(fs::path const &path_file_binary, std::vector< std::string > const &entries)
Adds a new environment variable to the database.
Value< void > set(fs::path const &path_file_binary, std::vector< std::string > const &entries)
Resets all defined environment variables to the ones passed as an argument.
Value< Db > from_string(S &&s)
Parses a JSON string and creates a Db object.
Value< std::string > expand(ns_concept::StringRepresentable auto &&var)
Performs variable expansion analogous to a POSIX shell.
Value< void > write(fs::path const &path_file_binary, std::string_view const &json)
Writes the environment json string to the target binary.
Value< std::string > read(fs::path const &path_file_binary)
Reads the environment json string from the target binary.
Manages the environment reserved space.
Enhanced expected type with integrated logging capabilities.