Add echo function

Is intended to be used for debugging like this:
$a = 'test'
echo($a, 'My string')
$b = [1,2,3]
echo($b, 'My array')
> 2015/02/10 21:43:26.939: My string (String) "test"
> 2015/02/10 21:43:26.040: My array (Array) ["1", "2", "3"]

Like 'notice' function but better

Change-Id: I0efb98a34b8d955c0703719c534c9dff912dcee9
This commit is contained in:
Dmitry Ilyin 2015-02-10 21:30:55 +03:00
parent 8384b8ca4d
commit ecebcacb0a
1 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,41 @@
require 'time'
require 'pp'
Puppet::Parser::Functions::newfunction(:echo, :doc => <<-EOS
Output the inspected value of the variable and its type.
Example:
$a = 'test'
$b = ["1", "2", "3"]
$c = {"a"=>"1", "b"=>"2"}
$d = true
$f = { "b" => { "b" => [1,2,3], "c" => true, "d" => { 'x' => 'y' }}, 'x' => 'y', 'z' => [1,2,3,4,5,6]}
$g = 12345
echo($a, 'My string')
echo($b, 'My array')
echo($c, 'My hash')
echo($d, 'My boolean')
echo($e, 'My undef')
echo($f, 'My structure')
echo($g) # no comment here
2015/02/10 21:53:11.067: My string (String) "test"
2015/02/10 21:53:11.067: My array (Array) ["1", "2", "3"]
2015/02/10 21:53:11.067: My hash (Hash) {"a"=>"1", "b"=>"2"}
2015/02/10 21:53:11.067: My boolean (TrueClass) true
2015/02/10 21:53:11.068: My undef (String) ""
2015/02/10 21:53:11.068: My structure (Hash) {"b"=>{"b"=>["1", "2", "3"], "c"=>true, "d"=>{"x"=>"y"}},
"x"=>"y",
"z"=>["1", "2", "3", "4", "5", "6"]}
2015/02/10 21:53:11.068: (String) "12345"
EOS
) do |argv|
value = argv[0]
comment = argv[1]
timestamp = Time.now.strftime("%Y/%m/%d %H:%M:%S.%3N")
message = "(#{value.class}) #{value.pretty_inspect}"
if comment
message = "#{comment} #{message}"
end
message = "#{timestamp}: #{message}"
puts message
end