blob: 78a8b5887e38f23fe8b31a3741604a327b2bfd87 (
plain) (
blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#include "env.h"
#include <library/cpp/yt/exception/exception.h>
#include <util/string/printf.h>
#include <util/system/platform.h>
#include <util/system/env.h>
#include <util/generic/maybe.h>
#ifdef _darwin_
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#endif
#ifdef _linux_
#include <unistd.h>
#endif
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
#if defined(_linux_) || defined(_darwin_)
std::vector<std::string> GetEnvironNameValuePairs()
{
std::vector<std::string> result;
for (char** envIt = environ; *envIt; ++envIt) {
result.emplace_back(*envIt);
}
return result;
}
#endif
std::pair<TStringBuf, std::optional<TStringBuf>> ParseEnvironNameValuePair(TStringBuf pair)
{
if (auto pos = pair.find('='); pos != std::string::npos) {
return {pair.substr(0, pos), pair.substr(pos + 1)};
} else {
return {pair, std::nullopt};
}
}
std::optional<std::string> TryGetEnvValue(TStringBuf name)
{
auto result = TryGetEnv(TString(name));
return result ? std::optional<std::string>(*result) : std::nullopt;
}
std::string GetEnvValueOrThrow(TStringBuf name)
{
auto value = TryGetEnvValue(name);
if (!value) {
throw TSimpleException(Sprintf("Environment variable \"%s\" is not set", name.data()));
}
return *value;
}
namespace NDetail {
void ThrowFailedToParseEnvValueError(TStringBuf name, TStringBuf value)
{
throw TSimpleException(Sprintf(
"Failed to parse value \"%s\" of environment variable \"%s\"",
TString(value).c_str(),
TString(name).c_str()));
}
} // namespace NDetail
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|