userver: userver/engine/subprocess/environment_variables.hpp Source File
Loading...
Searching...
No Matches
environment_variables.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/subprocess/environment_variables.hpp
4/// @brief @copybrief engine::subprocess::EnvironmentVariables
5
6#include <string>
7#include <unordered_map>
8
9#include <userver/rcu/rcu.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace engine::subprocess {
14
15/// Iterable environment variables wrapper to update values.
17 public:
19
20 /// Constructs a wrapper from a map.
21 explicit EnvironmentVariablesUpdate(Map vars) : vars_(std::move(vars)) {}
22
23 /// Returns a non const iterator to the beginning.
24 auto begin() { return vars_.begin(); }
25
26 /// Returns a non const iterator to the ending.
27 auto end() { return vars_.end(); }
28
29 /// Returns a const iterator to the beginning.
30 auto begin() const { return vars_.begin(); }
31
32 /// Returns a const iterator to the ending.
33 auto end() const { return vars_.end(); }
34
35 private:
36 Map vars_;
37};
38
39/// @brief Environment variables representation.
40///
41/// Wrapper to save environment variables and pass them to ProcessStarter::Exec.
42/// Changing an instance does not change current environment
43/// variables of the process.
45 public:
47
48 /// @brief Constructs an instance from pairs: key, value taken from the map.
49 explicit EnvironmentVariables(Map vars);
50
51 /// Constructs copy of the instance.
53
54 /// @brief Updates variable.
55 /// @note If variable does not exist then it is added.
57
58 /// @brief Returns the value of the variable.
59 /// @warning Throws std::runtime_error if there is no variable.
60 const std::string& GetValue(const std::string& variable_name) const;
61
62 /// Returns the pointer to the value of the variable or
63 /// nullptr if there is no variable.
64 const std::string* GetValueOptional(const std::string& variable_name) const;
65
66 /// Sets the value of the variable.
67 void SetValue(std::string variable_name, std::string value);
68
69 /// Returns the reference to the value.
70 std::string& operator[](const std::string& variable_name);
71
72 /// Checks whether the container is empty.
73 auto empty() const { return vars_.empty(); }
74
75 /// Returns the number of elements.
76 auto size() const { return vars_.size(); }
77
79
80 /// Returns a const iterator to the beginning.
81 auto begin() const { return vars_.begin(); }
82
83 /// Returns a const iterator to the ending.
84 auto end() const { return vars_.end(); }
85
86 private:
87 Map vars_;
88};
89
90/// Returns copy of the environment variables of the current process.
92
93/// Returns thread-safe read only pointer
94/// to the environment variables of the current process.
96
97/// Fetches current environment variables for getting via
98/// GetCurrentEnvironmentVariables or GetCurrentEnvironmentVariablesPtr.
100
101/// Overwrite modes
102enum class Overwrite {
103 kAllowed, ///< Overwrites or creates the environment variable
104 kForbidden, ///< Creates new environment variable, else throws
105 ///< std::runtime_error
106 kIgnored ///< Does not overwrite the environment variable if the variable
107 ///< exists
108};
109
110/// @brief Sets the environment variable with the specified overwrite type.
111/// @warning Not thread-safe.
112void SetEnvironmentVariable(const std::string& variable_name,
113 const std::string& value,
114 Overwrite overwrite = Overwrite::kForbidden);
115
116/// @brief Unsets the environment variable.
117/// @warning Not thread-safe.
118void UnsetEnvironmentVariable(const std::string& variable_name);
119
120} // namespace engine::subprocess
121
122USERVER_NAMESPACE_END