1#ifndef YNPUT_ENV_VAR_HELPER
2#define YNPUT_ENV_VAR_HELPER
4#include "../../../../NameSpaceDef/namespaces.hpp"
5#include "../../lib/logging/AyonLogger.hpp"
22 static const std::string kLogKeyName =
"getEnvKey";
23 static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
26 auto logKey = logger.key(kLogKeyName);
27 const char* charEnvKey = std::getenv(envKey.c_str());
28 if (charEnvKey !=
nullptr) {
29 std::string strEnvKey(charEnvKey);
30 logger.info(logKey,
"Loaded environment key '{}'", envKey);
33 logger.warn(logKey,
"Environment key '{}' was not found", envKey);
46 static const std::string kLogKeyName =
"cleanEnvKey";
47 static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
50 auto logKey = logger.key(kLogKeyName);
51 auto start = dirtyKey.find_first_not_of(
" \t\n\r\f\v");
52 if (start == std::string::npos) {
53 logger.warn(logKey,
"Environment key value was empty after cleanup");
56 auto end = dirtyKey.find_last_not_of(
" \t\n\r\f\v");
58 logger.info(logKey,
"Cleaned environment key value");
59 return dirtyKey.substr(start, end - start + 1);
70std::vector<std::string>
71split(
const std::string &str,
char delimiter) {
73 static const std::string kLogKeyName =
"splitEnvValue";
74 static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
77 auto logKey = logger.key(kLogKeyName);
78 std::vector<std::string> tokens;
79 std::stringstream ss(str);
82 while (std::getline(ss, token, delimiter)) {
83 tokens.push_back(token);
86 logger.info(logKey,
"Split environment value into {} token(s)", tokens.size());
96std::vector<std::string>
99 static const std::string kLogKeyName =
"getEnvArray";
100 static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
103 auto logKey = logger.key(kLogKeyName);
104 std::string envKeyVal =
getEnvKey(envKey);
105 std::vector<std::string> arrayItems;
106 if (envKeyVal.empty()) {
107 logger.warn(logKey,
"Environment array key '{}' was empty", envKey);
110 arrayItems =
split(envKeyVal,
',');
112 for (std::string &dirtyItem: arrayItems) {
115 logger.info(logKey,
"Loaded environment array '{}' with {} item(s)", envKey, arrayItems.size());
126std::map<std::string, std::string>
129 static const std::string kLogKeyName =
"getEnvMap";
130 static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
133 auto logKey = logger.key(kLogKeyName);
134 std::string envKeyVal =
getEnvKey(envKey);
135 std::map<std::string, std::string> envMap;
136 if (envKeyVal.empty()) {
137 logger.warn(logKey,
"Environment map key '{}' was empty", envKey);
140 std::vector<std::string> dirtyArrayItems =
split(envKeyVal,
',');
142 for (std::string &dirtyItem: dirtyArrayItems) {
144 auto eqPos = dirtyItem.find(
'=');
145 if (eqPos != std::string::npos) {
146 std::string key = dirtyItem.substr(0, eqPos);
147 std::string val = dirtyItem.substr(eqPos + 1);
151 logger.info(logKey,
"Loaded environment map '{}' with {} item(s)", envKey, envMap.size());
static AyonLogger & getInstance()
Definition: AyonLogger.hpp:33
std::vector< std::string > getEnvArray(const std::string &envKey)
split an env key into an std vector.
Definition: envVarHelpers.hpp:97
std::vector< std::string > split(const std::string &str, char delimiter)
splits a given string string via a given delimiter.
Definition: envVarHelpers.hpp:71
YNPUT_CORE_IOSTD_NAMESPACE_OPEN std::string getEnvKey(const std::string &envKey)
get the value of an environment key in string format
Definition: envVarHelpers.hpp:20
std::string cleanEnvKey(std::string &dirtyKey)
cleans an given environment key from any hidden characters line brakes and similar
Definition: envVarHelpers.hpp:44
std::map< std::string, std::string > getEnvMap(const std::string &envKey)
split an environment key into a std::map delimiter = , key value definition = {key}={value}
Definition: envVarHelpers.hpp:127
#define YNPUT_CORE_IOSTD_NAMESPACE_OPEN
Definition: namespaces.hpp:62
#define YNPUT_CORE_IOSTD_NAMESPACE_CLOSE
Definition: namespaces.hpp:63