@@ -37,106 +37,55 @@ def default_env
37
37
def create_erb ( content )
38
38
ERB . new ( content , trim_mode : '-' )
39
39
end
40
-
41
40
module_function :create_erb
42
41
43
- # @param name [String] The name of the environment variable to retrieve
44
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
45
- # @return [String] Value of the specified environment variable. nil if it does not exist
42
+ # @deprecated Use ENV instead
46
43
# @api private
47
44
def get_env ( name , mode = default_env )
48
- if mode == :windows
49
- Puppet ::Util ::Windows ::Process . get_environment_strings . each do |key , value |
50
- if name . casecmp ( key ) == 0 then
51
- return value
52
- end
53
- end
54
- return nil
55
- else
56
- ENV [ name ]
57
- end
45
+ ENV [ name ]
58
46
end
59
47
module_function :get_env
60
48
61
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
62
- # @return [Hash] A hashtable of all environment variables
49
+ # @deprecated Use ENV instead
63
50
# @api private
64
51
def get_environment ( mode = default_env )
65
- case mode
66
- when :posix
67
- ENV . to_hash
68
- when :windows
69
- Puppet ::Util ::Windows ::Process . get_environment_strings
70
- else
71
- raise _ ( "Unable to retrieve the environment for mode %{mode}" ) % { mode : mode }
72
- end
52
+ ENV . to_hash
73
53
end
74
54
module_function :get_environment
75
55
76
- # Removes all environment variables
77
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
56
+ # @deprecated Use ENV instead
78
57
# @api private
79
58
def clear_environment ( mode = default_env )
80
- case mode
81
- when :posix
82
- ENV . clear
83
- when :windows
84
- Puppet ::Util ::Windows ::Process . get_environment_strings . each do |key , _ |
85
- Puppet ::Util ::Windows ::Process . set_environment_variable ( key , nil )
86
- end
87
- else
88
- raise _ ( "Unable to clear the environment for mode %{mode}" ) % { mode : mode }
89
- end
59
+ ENV . clear
90
60
end
91
61
module_function :clear_environment
92
62
93
- # @param name [String] The name of the environment variable to set
94
- # @param value [String] The value to set the variable to. nil deletes the environment variable
95
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
63
+ # @deprecated Use ENV instead
96
64
# @api private
97
65
def set_env ( name , value = nil , mode = default_env )
98
- case mode
99
- when :posix
100
- ENV [ name ] = value
101
- when :windows
102
- Puppet ::Util ::Windows ::Process . set_environment_variable ( name , value )
103
- else
104
- raise _ ( "Unable to set the environment variable %{name} for mode %{mode}" ) % { name : name , mode : mode }
105
- end
66
+ ENV [ name ] = value
106
67
end
107
68
module_function :set_env
108
69
109
- # @param name [Hash] Environment variables to merge into the existing environment. nil values will remove the variable
110
- # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
70
+ # @deprecated Use ENV instead
111
71
# @api private
112
72
def merge_environment ( env_hash , mode = default_env )
113
- case mode
114
- when :posix
115
- env_hash . each { |name , val | ENV [ name . to_s ] = val }
116
- when :windows
117
- env_hash . each do |name , val |
118
- Puppet ::Util ::Windows ::Process . set_environment_variable ( name . to_s , val )
119
- end
120
- else
121
- raise _ ( "Unable to merge given values into the current environment for mode %{mode}" ) % { mode : mode }
122
- end
73
+ ENV . merge! ( hash . transform_keys ( &:to_s ) )
123
74
end
124
75
module_function :merge_environment
125
76
126
77
# Run some code with a specific environment. Resets the environment back to
127
78
# what it was at the end of the code.
128
- # Windows can store Unicode chars in the environment as keys or values, but
129
- # Ruby's ENV tries to roundtrip them through the local codepage, which can
130
- # cause encoding problems - underlying helpers use Windows APIs on Windows
131
- # 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
132
82
def withenv ( hash , mode = :posix )
133
- saved = get_environment ( mode )
134
- merge_environment ( hash , mode )
135
- yield
136
- ensure
137
- if saved
138
- clear_environment ( mode )
139
- 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 )
140
89
end
141
90
end
142
91
module_function :withenv
@@ -258,16 +207,16 @@ def which(bin)
258
207
if absolute_path? ( bin )
259
208
return bin if FileTest . file? bin and FileTest . executable? bin
260
209
else
261
- exts = Puppet :: Util . get_env ( 'PATHEXT' )
210
+ exts = ENV [ 'PATHEXT' ]
262
211
exts = exts ? exts . split ( File ::PATH_SEPARATOR ) : %w[ .COM .EXE .BAT .CMD ]
263
- Puppet :: Util . get_env ( 'PATH' ) . split ( File ::PATH_SEPARATOR ) . each do |dir |
212
+ ENV [ 'PATH' ] . split ( File ::PATH_SEPARATOR ) . each do |dir |
264
213
begin
265
214
dest = File . expand_path ( File . join ( dir , bin ) )
266
215
rescue ArgumentError => e
267
216
# if the user's PATH contains a literal tilde (~) character and HOME is not set, we may get
268
217
# an ArgumentError here. Let's check to see if that is the case; if not, re-raise whatever error
269
218
# was thrown.
270
- if e . to_s =~ /HOME/ and ( Puppet :: Util . get_env ( 'HOME' ) . nil? || Puppet :: Util . get_env ( 'HOME' ) == "" )
219
+ if e . to_s =~ /HOME/ and ( ENV [ 'HOME' ] . nil? || ENV [ 'HOME' ] == "" )
271
220
# if we get here they have a tilde in their PATH. We'll issue a single warning about this and then
272
221
# ignore this path element and carry on with our lives.
273
222
#TRANSLATORS PATH and HOME are environment variables and should not be translated
0 commit comments