#include "env.h" #include #include #include #include #include #ifdef _darwin_ #include #define environ (*_NSGetEnviron()) #endif #ifdef _linux_ #include #endif namespace NYT { //////////////////////////////////////////////////////////////////////////////// #if defined(_linux_) || defined(_darwin_) std::vector GetEnvironNameValuePairs() { std::vector result; for (char** envIt = environ; *envIt; ++envIt) { result.emplace_back(*envIt); } return result; } #endif std::pair> 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 TryGetEnvValue(TStringBuf name) { auto result = TryGetEnv(TString(name)); return result ? std::optional(*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