-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.cpp
More file actions
62 lines (53 loc) · 1.88 KB
/
Copy pathenv.cpp
File metadata and controls
62 lines (53 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <brewtils/env.h>
void brewtils::env::init(std::string path) noexcept(false) {
if (brewtils::os::dir::exists(path)) {
path = brewtils::os::joinPath(path, ".env");
}
if (!brewtils::os::file::exists(path)) {
logger::warning("No .env file found at " + path + ", skipping...");
return;
}
const std::string data = brewtils::os::file::read(path);
std::string key, value;
for (const std::string &line : brewtils::string::split(data, "\n")) {
std::size_t pos = line.rfind('=');
if (pos == std::string::npos) {
continue;
}
key = line.substr(0, pos);
key = brewtils::string::trim(key);
value = line.substr(pos + 1);
value = brewtils::string::split(value, "#")[0];
value = brewtils::string::trim(value);
brewtils::env::set(key, value);
}
return;
}
std::string brewtils::env::get(const std::string &key,
const std::string &defaultValue) noexcept(true) {
char *value = getenv(key.c_str());
if (value == nullptr) {
logger::warning("Environment variable '" + key +
"' not found, using default value: '" + defaultValue + "'");
return defaultValue;
}
return std::string(value);
}
void brewtils::env::set(const std::string &key,
const std::string &value) noexcept(false) {
if (value.empty()) {
logger::warning("Unsetting environment variable " + key);
if (unsetenv(key.c_str()) != 0) {
logger::error("Failed to unset environment variable " + key,
"void set(const std::string &key, const std::string "
"&value) noexcept(false)");
}
} else {
if (setenv(key.c_str(), value.c_str(), 1) != 0) {
logger::error("Failed to set environment variable " + key,
"void set(const std::string &key, const std::string "
"&value) noexcept(false)");
}
}
return;
}