FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
env.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <ranges>
12#include <filesystem>
13#include <format>
14
15#include "../std/expected.hpp"
16#include "../lib/env.hpp"
17#include "../reserved/env.hpp"
18#include "db.hpp"
19
29namespace ns_db::ns_env
30{
31
32namespace
33{
34
35namespace fs = std::filesystem;
36
43[[nodiscard]] inline std::unordered_map<std::string,std::string> map(std::vector<std::string> const& entries)
44{
45 return entries
46 | std::views::filter([](auto&& e){ return e.find('=') != std::string::npos; })
47 | std::views::transform([](auto&& e)
48 {
49 auto it = e.find('=');
50 return std::make_pair(e.substr(0, it), e.substr(it+1, std::string::npos));
51 })
52 | std::ranges::to<std::unordered_map<std::string,std::string>>();
53}
54
63[[nodiscard]] inline Value<void> validate(std::vector<std::string> const& entries)
64{
65 for (auto&& entry : entries)
66 {
67 if (std::ranges::count_if(entry, [](char c){ return c == '='; }) == 0)
68 {
69 return Error("C::Variable assignment '{}' is invalid", entry);
70 }
71 }
72 return {};
73}
74
75} // namespace
76
83[[nodiscard]] inline Value<void> del(fs::path const& path_file_binary, std::vector<std::string> const& entries)
84{
85 ns_db::Db db = ns_db::from_string(Pop(ns_reserved::ns_env::read(path_file_binary))).value_or(ns_db::Db());
86 std::ranges::for_each(entries, [&](auto const& entry)
87 {
88 if( db.erase(entry) )
89 {
90 logger("I::Erase key '{}'", entry);
91 }
92 else
93 {
94 logger("I::Key '{}' not found for deletion", entry);
95 }
96 });
97 Pop(ns_reserved::ns_env::write(path_file_binary, Pop(db.dump())));
98 return {};
99}
100
108[[nodiscard]] inline Value<void> add(fs::path const& path_file_binary, std::vector<std::string> const& entries)
109{
110 // Validate entries
111 Pop(validate(entries));
112 // Insert environment variables in the database
113 ns_db::Db db = ns_db::from_string(Pop(ns_reserved::ns_env::read(path_file_binary))).value_or(ns_db::Db());
114 for (auto&& [key,value] : map(entries))
115 {
116 db(key) = value;
117 logger("I::Included variable '{}' with value '{}'", key, value);
118 }
119 // Write to the database
120 Pop(ns_reserved::ns_env::write(path_file_binary, Pop(db.dump())));
121 return {};
122}
123
131[[nodiscard]] inline Value<void> set(fs::path const& path_file_binary, std::vector<std::string> const& entries)
132{
133 Pop(ns_reserved::ns_env::write(path_file_binary, Pop(ns_db::Db().dump())));
134 Pop(add(path_file_binary, entries));
135 return {};
136}
137
144[[nodiscard]] inline Value<std::vector<std::string>> get(fs::path const& path_file_binary)
145{
146 // Get environment
147 ns_db::Db db = ns_db::from_string(Pop(ns_reserved::ns_env::read(path_file_binary))).value_or(ns_db::Db());
148 // Merge variables with values
149 std::vector<std::string> environment;
150 for (auto&& [key,value] : db.items())
151 {
152 environment.push_back(std::format("{}={}", key, Pop(value.template value<std::string>())));
153 }
154 // Expand variables
155 for(auto& variable : environment)
156 {
157 variable = ::ns_env::expand(variable).value_or(variable);
158 }
159 return environment;
160}
161
162} // namespace ns_db::ns_env
163
164
165/* 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
std::vector< std::pair< std::string, Db > > items() const noexcept
Retrieves key-value pairs as Db objects from the current JSON.
Definition db.hpp:216
bool erase(T &&t)
Removes a key from the JSON structure.
Definition db.hpp:343
Value< std::string > dump()
Serializes the current JSON data to a formatted string.
Definition db.hpp:277
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.
Definition log.hpp:682
Value< void > validate(std::vector< std::string > const &entries)
Validates environment variable entries.
Definition env.hpp:63
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.
Definition env.hpp:43
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.
Definition env.hpp:83
Value< std::vector< std::string > > get(fs::path const &path_file_binary)
Get existing variables from the database.
Definition env.hpp:144
Value< void > add(fs::path const &path_file_binary, std::vector< std::string > const &entries)
Adds a new environment variable to the database.
Definition env.hpp:108
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.
Definition env.hpp:131
Value< Db > from_string(S &&s)
Parses a JSON string and creates a Db object.
Definition db.hpp:496
Value< std::string > expand(ns_concept::StringRepresentable auto &&var)
Performs variable expansion analogous to a POSIX shell.
Definition env.hpp:96
Value< void > write(fs::path const &path_file_binary, std::string_view const &json)
Writes the environment json string to the target binary.
Definition env.hpp:45
Value< std::string > read(fs::path const &path_file_binary)
Reads the environment json string from the target binary.
Definition env.hpp:65
Manages the environment reserved space.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44