• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/config/load_config.h"
16 
17 #include <grpc/support/port_platform.h>
18 #include <stdio.h>
19 
20 #include "absl/flags/marshalling.h"
21 #include "absl/log/check.h"
22 #include "absl/strings/numbers.h"
23 #include "absl/strings/str_join.h"
24 #include "absl/types/optional.h"
25 #include "src/core/util/env.h"
26 
27 namespace grpc_core {
28 
29 namespace {
LoadEnv(absl::string_view environment_variable)30 absl::optional<std::string> LoadEnv(absl::string_view environment_variable) {
31   return GetEnv(std::string(environment_variable).c_str());
32 }
33 }  // namespace
34 
LoadConfigFromEnv(absl::string_view environment_variable,const char * default_value)35 std::string LoadConfigFromEnv(absl::string_view environment_variable,
36                               const char* default_value) {
37   CHECK(!environment_variable.empty());
38   return LoadEnv(environment_variable).value_or(default_value);
39 }
40 
LoadConfigFromEnv(absl::string_view environment_variable,int32_t default_value)41 int32_t LoadConfigFromEnv(absl::string_view environment_variable,
42                           int32_t default_value) {
43   auto env = LoadEnv(environment_variable);
44   if (env.has_value()) {
45     int32_t out;
46     if (absl::SimpleAtoi(*env, &out)) return out;
47     fprintf(stderr, "Error reading int from %s: '%s' is not a number",
48             std::string(environment_variable).c_str(), env->c_str());
49   }
50   return default_value;
51 }
52 
LoadConfigFromEnv(absl::string_view environment_variable,bool default_value)53 bool LoadConfigFromEnv(absl::string_view environment_variable,
54                        bool default_value) {
55   auto env = LoadEnv(environment_variable);
56   if (env.has_value()) {
57     bool out;
58     std::string error;
59     if (absl::ParseFlag(env->c_str(), &out, &error)) return out;
60     fprintf(stderr, "Error reading bool from %s: '%s' is not a bool: %s",
61             std::string(environment_variable).c_str(), env->c_str(),
62             error.c_str());
63   }
64   return default_value;
65 }
66 
LoadConfig(const absl::Flag<std::vector<std::string>> & flag,absl::string_view environment_variable,const absl::optional<std::string> & override,const char * default_value)67 std::string LoadConfig(const absl::Flag<std::vector<std::string>>& flag,
68                        absl::string_view environment_variable,
69                        const absl::optional<std::string>& override,
70                        const char* default_value) {
71   if (override.has_value()) return *override;
72   auto from_flag = absl::GetFlag(flag);
73   if (!from_flag.empty()) return absl::StrJoin(from_flag, ",");
74   return LoadConfigFromEnv(environment_variable, default_value);
75 }
76 
77 }  // namespace grpc_core
78