@@ -39,103 +39,53 @@ def create_erb(content)
39
39
end
40
40
module_function :create_erb
41
41
42
- # @param name [String] The name of the environment variable to retrieve
43
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
44
- # @return [String] Value of the specified environment variable. nil if it does not exist
42
+ # @deprecated Use ENV instead
45
43
# @api private
46
44
def get_env ( name , mode = default_env )
47
- if mode == :windows
48
- Puppet ::Util ::Windows ::Process . get_environment_strings . each do |key , value |
49
- if name . casecmp ( key ) == 0 then
50
- return value
51
- end
52
- end
53
- return nil
54
- else
55
- ENV [ name ]
56
- end
45
+ ENV [ name ]
57
46
end
58
47
module_function :get_env
59
48
60
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
61
- # @return [Hash] A hashtable of all environment variables
49
+ # @deprecated Use ENV instead
62
50
# @api private
63
51
def get_environment ( mode = default_env )
64
- case mode
65
- when :posix
66
- ENV . to_hash
67
- when :windows
68
- Puppet ::Util ::Windows ::Process . get_environment_strings
69
- else
70
- raise _ ( "Unable to retrieve the environment for mode %{mode}" ) % { mode : mode }
71
- end
52
+ ENV . to_hash
72
53
end
73
54
module_function :get_environment
74
55
75
- # Removes all environment variables
76
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
56
+ # @deprecated Use ENV instead
77
57
# @api private
78
58
def clear_environment ( mode = default_env )
79
- case mode
80
- when :posix
81
- ENV . clear
82
- when :windows
83
- Puppet ::Util ::Windows ::Process . get_environment_strings . each do |key , _ |
84
- Puppet ::Util ::Windows ::Process . set_environment_variable ( key , nil )
85
- end
86
- else
87
- raise _ ( "Unable to clear the environment for mode %{mode}" ) % { mode : mode }
88
- end
59
+ ENV . clear
89
60
end
90
61
module_function :clear_environment
91
62
92
- # @param name [String] The name of the environment variable to set
93
- # @param value [String] The value to set the variable to. nil deletes the environment variable
94
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
63
+ # @deprecated Use ENV instead
95
64
# @api private
96
65
def set_env ( name , value = nil , mode = default_env )
97
- case mode
98
- when :posix
99
- ENV [ name ] = value
100
- when :windows
101
- Puppet ::Util ::Windows ::Process . set_environment_variable ( name , value )
102
- else
103
- raise _ ( "Unable to set the environment variable %{name} for mode %{mode}" ) % { name : name , mode : mode }
104
- end
66
+ ENV [ name ] = value
105
67
end
106
68
module_function :set_env
107
69
108
- # @param name [Hash] Environment variables to merge into the existing environment. nil values will remove the variable
109
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
70
+ # @deprecated Use ENV instead
110
71
# @api private
111
72
def merge_environment ( env_hash , mode = default_env )
112
- case mode
113
- when :posix
114
- env_hash . each { |name , val | ENV [ name . to_s ] = val }
115
- when :windows
116
- env_hash . each do |name , val |
117
- Puppet ::Util ::Windows ::Process . set_environment_variable ( name . to_s , val )
118
- end
119
- else
120
- raise _ ( "Unable to merge given values into the current environment for mode %{mode}" ) % { mode : mode }
121
- end
73
+ ENV . merge! ( hash . transform_keys ( &:to_s ) )
122
74
end
123
75
module_function :merge_environment
124
76
125
77
# Run some code with a specific environment. Resets the environment back to
126
78
# what it was at the end of the code.
127
- # Windows can store Unicode chars in the environment as keys or values, but
128
- # Ruby's ENV tries to roundtrip them through the local codepage, which can
129
- # cause encoding problems - underlying helpers use Windows APIs on Windows
130
- # see https://bugs.ruby-lang.org/issues/8822
79
+ #
80
+ # @param hash [Hash{String,Symbol => String}] Environment variables to override the current environment.
81
+ # @param mode [Symbol] ignored
131
82
def withenv ( hash , mode = :posix )
132
- saved = get_environment ( mode )
133
- merge_environment ( hash , mode )
134
- yield
135
- ensure
136
- if saved
137
- clear_environment ( mode )
138
- merge_environment ( saved , mode )
83
+ saved = ENV . to_hash
84
+ begin
85
+ ENV . merge! ( hash . transform_keys ( &:to_s ) )
86
+ yield
87
+ ensure
88
+ ENV . replace ( saved )
139
89
end
140
90
end
141
91
module_function :withenv
0 commit comments