Loading...
Searching...
No Matches
EnvHelpers.cc
Go to the documentation of this file.
1#include "eon/EnvHelpers.hpp"
3#include "eon/EonLogger.h"
4
5namespace eonc::helpers {
6
7std::string get_value_from_env_or_param(const char *env_variable,
8 const std::string &param_value,
9 const std::string &default_value,
10 const std::string &warning_message,
11 const bool is_mandatory) {
12 const char *env_value = std::getenv(env_variable);
13 if (env_value != nullptr) {
14 return std::string(env_value);
15 }
16
17 if (!param_value.empty()) {
18 return param_value;
19 }
20
21 if (is_mandatory) {
22 throw std::runtime_error(
23 "Environment variable " + std::string(env_variable) +
24 " is not set and no parameter value provided. Please set it in the "
25 "configuration or as an environment variable.\n");
26 }
27
28 if (!default_value.empty() && !warning_message.empty()) {
29 EONC_LOG_WARNING("{}", warning_message);
30 }
31
32 return default_value;
33}
34
35} // namespace eonc::helpers
#define EONC_LOG_WARNING(...)
Definition EonLogger.h:256
std::string get_value_from_env_or_param(const char *env_variable, const std::string &param_value, const std::string &default_value, const std::string &warning_message, const bool is_mandatory)
Definition EnvHelpers.cc:7