1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <grpc/support/port_platform.h>
16
17 #include "src/core/lib/config/load_config.h"
18
19 #include <stdio.h>
20
21 #include "absl/flags/marshalling.h"
22 #include "absl/strings/numbers.h"
23 #include "absl/strings/str_join.h"
24 #include "absl/types/optional.h"
25
26 #include "src/core/lib/gpr/log_internal.h"
27 #include "src/core/lib/gprpp/env.h"
28
29 namespace grpc_core {
30
31 namespace {
LoadEnv(absl::string_view environment_variable)32 absl::optional<std::string> LoadEnv(absl::string_view environment_variable) {
33 return GetEnv(std::string(environment_variable).c_str());
34 }
35 } // namespace
36
LoadConfigFromEnv(absl::string_view environment_variable,const char * default_value)37 std::string LoadConfigFromEnv(absl::string_view environment_variable,
38 const char* default_value) {
39 GPR_ASSERT_INTERNAL(!environment_variable.empty());
40 return LoadEnv(environment_variable).value_or(default_value);
41 }
42
LoadConfigFromEnv(absl::string_view environment_variable,int32_t default_value)43 int32_t LoadConfigFromEnv(absl::string_view environment_variable,
44 int32_t default_value) {
45 auto env = LoadEnv(environment_variable);
46 if (env.has_value()) {
47 int32_t out;
48 if (absl::SimpleAtoi(*env, &out)) return out;
49 fprintf(stderr, "Error reading int from %s: '%s' is not a number",
50 std::string(environment_variable).c_str(), env->c_str());
51 }
52 return default_value;
53 }
54
LoadConfigFromEnv(absl::string_view environment_variable,bool default_value)55 bool LoadConfigFromEnv(absl::string_view environment_variable,
56 bool default_value) {
57 auto env = LoadEnv(environment_variable);
58 if (env.has_value()) {
59 bool out;
60 std::string error;
61 if (absl::ParseFlag(env->c_str(), &out, &error)) return out;
62 fprintf(stderr, "Error reading bool from %s: '%s' is not a bool: %s",
63 std::string(environment_variable).c_str(), env->c_str(),
64 error.c_str());
65 }
66 return default_value;
67 }
68
LoadConfig(const absl::Flag<std::vector<std::string>> & flag,absl::string_view environment_variable,const absl::optional<std::string> & override,const char * default_value)69 std::string LoadConfig(const absl::Flag<std::vector<std::string>>& flag,
70 absl::string_view environment_variable,
71 const absl::optional<std::string>& override,
72 const char* default_value) {
73 if (override.has_value()) return *override;
74 auto from_flag = absl::GetFlag(flag);
75 if (!from_flag.empty()) return absl::StrJoin(from_flag, ",");
76 return LoadConfigFromEnv(environment_variable, default_value);
77 }
78
79 } // namespace grpc_core
80