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 <concepts>
12#include <cstdlib>
13#include <wordexp.h>
14#include <ranges>
15
16#include "../std/expected.hpp"
17#include "../std/string.hpp"
18#include "../common.hpp"
19#include "../macro.hpp"
20
28namespace ns_env
29{
30
31namespace
32{
33namespace fs = std::filesystem;
34}
35
36enum class Replace
37{
38 Y,
39 N,
40};
41
51template<ns_concept::StringRepresentable T, ns_concept::StringRepresentable U>
52void set(T&& name, U&& value, Replace replace)
53{
54 setenv(ns_string::to_string(name).c_str(), ns_string::to_string(value).c_str(), (replace == Replace::Y));
55}
56
64template<ns_string::static_string S = "E">
65inline Value<std::string> get_expected(std::string_view name)
66{
67 static constexpr ns_string::static_string suffix("::Could not read variable '{}'");
68 static constexpr ns_string::static_string fmt = S.template join<suffix>();
69 const char * var = std::getenv(name.data());
70 return (var != nullptr)?
71 Value<std::string>(std::string{var})
72 : Error(fmt.data, name);
73}
74
82inline bool exists(std::string_view name, std::string_view value)
83{
84 const char* value_real = getenv(name.data());
85 return_if(not value_real, false);
86 return std::string_view{value_real} == value;
87}
88
97{
98 std::string expanded = ns_string::to_string(var);
99
100 // Perform word expansion
101 wordexp_t data;
102 if (int ret = wordexp(expanded.c_str(), &data, 0); ret == 0)
103 {
104 if (data.we_wordc > 0)
105 {
106 expanded = data.we_wordv[0];
107 } // if
108 wordfree(&data);
109 } // if
110 else
111 {
112 std::string error;
113 switch(ret)
114 {
115 case WRDE_BADCHAR: error = "WRDE_BADCHAR"; break;
116 case WRDE_BADVAL: error = "WRDE_BADVAL"; break;
117 case WRDE_CMDSUB: error = "WRDE_CMDSUB"; break;
118 case WRDE_NOSPACE: error = "WRDE_NOSPACE"; break;
119 case WRDE_SYNTAX: error = "WRDE_SYNTAX"; break;
120 default: error = "unknown";
121 } // switch
122 return Error("E::{}", error);
123 } // else
124
125 return expanded;
126}
127
134template<typename T = std::string>
135inline Value<T> xdg_data_home() noexcept
136{
137 const char* var = std::getenv("XDG_DATA_HOME");
138 return_if(var, var);
139 const char* home = std::getenv("HOME");
140 return_if(not home, Error("E::HOME is undefined"));
141 return std::string{home} + "/.local/share";
142}
143
150[[nodiscard]] inline Value<fs::path> search_path(std::string const& query)
151{
152 std::string env_path = Pop(ns_env::get_expected("PATH"));
153 // Query should be a file name
154 if ( fs::path{query}.is_absolute() )
155 {
156 return Error("E::Query should be a file name, not an absolute path");
157 }
158 // Search directories in PATH
159 for(fs::path directory : env_path
160 | std::views::split(':')
161 | std::ranges::to<std::vector<std::string>>())
162 {
163 fs::path path_full = directory / query;
164 return_if(fs::exists(path_full), path_full);
165 }
166 return Error("E::File not found in PATH");
167}
168
169} // namespace ns_env
170
171/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
Common utility functions and helpers used throughout FlatImage.
Concept for types that can be represented as a string.
Definition concept.hpp:442
Enhanced error handling framework built on std::expected.
Simplified macros for common control flow patterns with optional logging.
Environment variable management utilities.
Value< std::string > expand(ns_concept::StringRepresentable auto &&var)
Performs variable expansion analogous to a POSIX shell.
Definition env.hpp:96
Value< std::string > get_expected(std::string_view name)
Get the value of an environment variable.
Definition env.hpp:65
Value< fs::path > search_path(std::string const &query)
Search the directories in the PATH variable for the given input file name.
Definition env.hpp:150
bool exists(std::string_view name, std::string_view value)
Checks if variable exists and equals value.
Definition env.hpp:82
Value< T > xdg_data_home() noexcept
Returns or computes the value of XDG_DATA_HOME.
Definition env.hpp:135
void set(T &&name, U &&value, Replace replace)
Sets an environment variable.
Definition env.hpp:52
std::string to_string(T &&t) noexcept
Converts a type to a string.
Definition string.hpp:97
String helpers.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44
Compile-time string container with fixed size.
Definition string.hpp:35