31 [[nodiscard]]
const std::string &
var_name()
const {
return var_name_; }
34 std::string var_name_;
54 static std::string
Expand(
const std::string &value,
bool strict =
true) {
56 result.reserve(value.size());
59 while (i < value.size()) {
61 if (i + 2 < value.size() && value[i] ==
'$' &&
62 value[i + 1] ==
'$' && value[i + 2] ==
'{') {
69 if (i + 1 < value.size() && value[i] ==
'$' &&
70 value[i + 1] ==
'{') {
71 std::size_t start = i + 2;
72 std::size_t end = value.find(
'}', start);
73 if (end == std::string::npos) {
80 std::string content = value.substr(start, end - start);
82 std::string default_value;
83 bool has_default =
false;
86 std::size_t colon_pos = content.find(
':');
87 if (colon_pos != std::string::npos) {
88 var_name = content.substr(0, colon_pos);
89 default_value = content.substr(colon_pos + 1);
95 auto env_value =
GetEnv(var_name);
96 if (env_value.has_value()) {
97 result += env_value.value();
98 }
else if (has_default) {
99 result += default_value;
123 while ((pos = value.find(
"${", pos)) != std::string::npos) {
124 std::size_t end = value.find(
'}', pos + 2);
125 if (end != std::string::npos) {
144 YAML::Node expanded = ExpandNodeRecursive(node.
Raw());
173 std::string expanded_path =
Expand(path,
true);
182 std::string expanded_path =
Expand(path_with_vars,
true);
196 static std::optional<std::string>
GetEnv(
const std::string &name) {
197 const char *value = std::getenv(name.c_str());
198 if (value ==
nullptr) {
201 return std::string(value);
207 static std::string
GetEnv(
const std::string &name,
208 const std::string &default_value) {
209 return GetEnv(name).value_or(default_value);
214 static YAML::Node ExpandNodeRecursive(
const YAML::Node &node) {
215 if (node.IsScalar()) {
216 std::string value = node.as<std::string>();
218 return YAML::Node(
Expand(value,
true));
223 if (node.IsSequence()) {
225 for (std::size_t i = 0; i < node.size(); ++i) {
226 result.push_back(ExpandNodeRecursive(node[i]));
233 for (
const auto &pair : node) {
234 std::string key = pair.first.as<std::string>();
239 result[key] = ExpandNodeRecursive(pair.second);
YAML file utilities for includes, merging, and discovery.
Type-safe YAML wrapper with path-aware error messages.
Exception for undefined environment variables.
Definition YamlEnv.hpp:25
EnvVarError(const std::string &var_name)
Definition YamlEnv.hpp:27
const std::string & var_name() const
Definition YamlEnv.hpp:31
Environment variable expansion utilities.
Definition YamlEnv.hpp:40
static YamlNode LoadFromEnvPath(const std::string &path_with_vars)
Expand envvars in path, then load the file.
Definition YamlEnv.hpp:181
static std::string Expand(const std::string &value, bool strict=true)
Expand environment variables in a string.
Definition YamlEnv.hpp:54
static YamlNode LoadFileWithEnv(const std::string &path)
Load YAML file with environment variable expansion.
Definition YamlEnv.hpp:159
static std::optional< std::string > GetEnv(const std::string &name)
Get environment variable value.
Definition YamlEnv.hpp:196
static bool ContainsEnvVars(const std::string &value)
Check if string contains environment variable references.
Definition YamlEnv.hpp:120
static YamlNode LoadWithIncludesAndEnv(const std::string &path)
Load with includes AND environment expansion.
Definition YamlEnv.hpp:172
static YamlNode ExpandAll(const YamlNode &node)
Recursively expand all string values in a YAML tree.
Definition YamlEnv.hpp:143
static std::string GetEnv(const std::string &name, const std::string &default_value)
Get environment variable with default.
Definition YamlEnv.hpp:207
YamlError(const std::string &path, const std::string &msg)
Definition YamlNode.hpp:29
static YamlNode LoadWithIncludes(const std::string &path)
Load YAML from file with !include directive resolution.
Definition YamlFile.hpp:37
Wrapper around yaml-cpp node with ergonomic accessors.
Definition YamlNode.hpp:47
static YamlNode LoadFile(const std::string &path)
Load from file.
Definition YamlNode.hpp:54
const std::string & Path() const
Get current path (for error messages).
Definition YamlNode.hpp:114
const YAML::Node & Raw() const
Get underlying yaml-cpp node.
Definition YamlNode.hpp:200
Definition CSVExport.hpp:20