-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpand_env.rb
More file actions
27 lines (25 loc) · 798 Bytes
/
Copy pathexpand_env.rb
File metadata and controls
27 lines (25 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# expand environment variables in parameters
# and adjust any occurence of $HOME to the home on the guest
def expand_env(value)
guest_sub_indices = []
return value unless value.is_a?(String)
while (matches = value.match(/\$([A-Za-z0-9_]+)/))
env_name = matches[1]
match_index = matches.begin(1)
# guest substitution
if value[match_index-2] == '$'
value[match_index-2] = '?'
value[match_index-1] = ''
guest_sub_indices << match_index-2
next
end
break if env_name == ""
env_val = ENV[env_name]
return nil if env_val.nil?
value = value.sub("$#{env_name}", env_val)
end
guest_sub_indices.each do |i|
value[i] = '$'
end
return value
end