lib/hieracles/interpolate.rb in hieracles-0.1.5 vs lib/hieracles/interpolate.rb in hieracles-0.1.6
- old
+ new
@@ -1,23 +1,44 @@
module Hieracles
module Interpolate
def parse(data, values, interactive = false)
- data.gsub(/%\{(?:(scope|hiera|literal|alias) *)?([^\}]*)\}/) do |match|
+ data.gsub(/%\{(?:(scope|hiera|literal|alias)\(['"])?(?:::)?([^\}"']*)(?:["']\))?\}/) do |match|
if interactive && !values[$2.to_sym]
- puts
- puts "#{match} is not defined."
- puts "Is it missing in your ENC source?"
- puts "Maybe you should define a default value for that scope variable in your config file?"
- puts "Do you want to provide a temmporary value? [input value]"
- print "#{$2} = "
- val = $stdin.gets.chomp
- values[$2.to_sym] = val
- val
+ values[$2.to_sym] = ask_about($2)
+ values[$2.to_sym]
else
values[$2.to_sym]
end
end
+ end
+
+ def ask_about(var)
+ @@output.puts
+ @@output.puts "'#{var}' is not defined."
+ @@output.puts "Is it missing in your ENC source?"
+ @@output.puts "Maybe you should define a default value for that scope variable in your config file?"
+ @@output.puts "Do you want to provide a temmporary value? [input value]"
+ @@output.print "#{var} = "
+ @@input.gets.chomp
+ end
+
+ # makes possible to set input and output
+ def setio(input, output)
+ @@input = input
+ @@output = output
+ end
+
+ private
+
+ # defaults to STDIN
+ def input
+ @@input ||= STDIN
+ end
+
+ # defaults to STDOUT
+ def output
+ @@output ||= STDOUT
end
end
end