diff options
| author | babenko <[email protected]> | 2026-05-01 14:47:58 +0300 |
|---|---|---|
| committer | babenko <[email protected]> | 2026-05-01 15:04:18 +0300 |
| commit | 4a9f78903be5ce431a47241af5126c08fb38c4f9 (patch) | |
| tree | b8da36c0039da6fed2e0dfabad9000024743a8b6 /library/cpp/yt | |
| parent | 81191b7842925cb6f52174bf3a875749d2f71b8f (diff) | |
Extract environment variables helpers to library/cpp/yt/system
commit_hash:5a11a7288f325d6fa0a9bec9a1ce0b7afa1f4984
Diffstat (limited to 'library/cpp/yt')
| -rw-r--r-- | library/cpp/yt/system/env.cpp | 22 | ||||
| -rw-r--r-- | library/cpp/yt/system/env.h | 3 | ||||
| -rw-r--r-- | library/cpp/yt/system/ya.make | 1 |
3 files changed, 26 insertions, 0 deletions
diff --git a/library/cpp/yt/system/env.cpp b/library/cpp/yt/system/env.cpp index ff5eaeb8545..1bbc91d8274 100644 --- a/library/cpp/yt/system/env.cpp +++ b/library/cpp/yt/system/env.cpp @@ -1,6 +1,13 @@ #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> @@ -37,6 +44,21 @@ std::pair<TStringBuf, std::optional<TStringBuf>> ParseEnvironNameValuePair(TStri } } +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 NYT::NThreading diff --git a/library/cpp/yt/system/env.h b/library/cpp/yt/system/env.h index 77bc933a8ec..e599d8a23df 100644 --- a/library/cpp/yt/system/env.h +++ b/library/cpp/yt/system/env.h @@ -17,6 +17,9 @@ std::vector<std::string> GetEnvironNameValuePairs(); std::pair<TStringBuf, std::optional<TStringBuf>> ParseEnvironNameValuePair(TStringBuf pair); +std::optional<std::string> TryGetEnvValue(TStringBuf name); +std::string GetEnvValueOrThrow(TStringBuf name); + //////////////////////////////////////////////////////////////////////////////// } // namespace NYT diff --git a/library/cpp/yt/system/ya.make b/library/cpp/yt/system/ya.make index eb8509ce4f4..3f767f78f49 100644 --- a/library/cpp/yt/system/ya.make +++ b/library/cpp/yt/system/ya.make @@ -9,6 +9,7 @@ SRCS( ) PEERDIR( + library/cpp/yt/exception library/cpp/yt/misc ) |
