summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2014-03-13 15:18:03 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2015-08-18 12:03:21 -0700
commit63a2d95003bc062d251d453d63ab1c5d91cbdb06 (patch)
tree5f50177a3cd8c66242030e198e2c1387ab4d56f6 /lib
parent74e6bd6b50af383829f4bfc47ca18a4e089b1428 (diff)
downloadchef-63a2d95003bc062d251d453d63ab1c5d91cbdb06.tar.gz
CHEF-5012: add methods for template breadcrumbs
adds: - cookbook_name - recipe_name - recipe_line_string - recipe_path - recipe_line - template_name - template_path accessible both as instance var (@-) and method (bare), like @node/node.
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/mixin/template.rb47
-rw-r--r--lib/chef/provider/template/content.rb10
-rw-r--r--lib/chef/resource.rb21
3 files changed, 75 insertions, 3 deletions
diff --git a/lib/chef/mixin/template.rb b/lib/chef/mixin/template.rb
index d705a9e7be..546b76cf80 100644
--- a/lib/chef/mixin/template.rb
+++ b/lib/chef/mixin/template.rb
@@ -58,11 +58,58 @@ class Chef
# by the bare `node` everywhere.
def node
return @node if @node
+ # XXX: we include @node even if you explicitly pass vars, so is this warning never reached?
raise "Could not find a value for node. If you are explicitly setting variables in a template, " +
"include a node variable if you plan to use it."
end
#
+ # Helpers for adding context of which resource is rendering the template (CHEF-5012)
+ #
+
+ # name of the cookbook containing the template resource, e.g.:
+ # test
+ def cookbook_name
+ return @cookbook_name if @cookbook_name
+ end
+
+ # name of the recipe containing the template resource, e.g.:
+ # default
+ def recipe_name
+ return @recipe_name if @recipe_name
+ end
+
+ # string representation of the line in the recipe containing the template resource, e.g.:
+ # /Users/lamont/solo/cookbooks/test/recipes/default.rb:2:in `from_file'
+ def recipe_line_string
+ return @recipe_line_string if @recipe_line_string
+ end
+
+ # path to the recipe containing the template resource, e.g.:
+ # /Users/lamont/solo/cookbooks/test/recipes/default.rb
+ def recipe_path
+ return @recipe_path if @recipe_path
+ end
+
+ # line in the recipe containing the template reosurce, e.g.:
+ # 2
+ def recipe_line
+ return @recipe_line if @recipe_line
+ end
+
+ # name of the template source itself, e.g.:
+ # foo.erb
+ def template_name
+ return @template_name if @template_name
+ end
+
+ # path to the template source itself, e.g.:
+ # /Users/lamont/solo/cookbooks/test/templates/default/foo.erb
+ def template_path
+ return @template_path if @template_path
+ end
+
+ #
# Takes the name of the partial, plus a hash of options. Returns a
# string that contains the result of the evaluation of the partial.
#
diff --git a/lib/chef/provider/template/content.rb b/lib/chef/provider/template/content.rb
index 7fc680ec85..a231bd509e 100644
--- a/lib/chef/provider/template/content.rb
+++ b/lib/chef/provider/template/content.rb
@@ -39,6 +39,16 @@ class Chef
context = TemplateContext.new(@new_resource.variables)
context[:node] = @run_context.node
context[:template_finder] = template_finder
+
+ # helper variables
+ context[:cookbook_name] = @new_resource.cookbook_name unless context.keys.include?(:coookbook_name)
+ context[:recipe_name] = @new_resource.recipe_name unless context.keys.include?(:recipe_name)
+ context[:recipe_line_string] = @new_resource.source_line unless context.keys.include?(:recipe_line_string)
+ context[:recipe_path] = @new_resource.source_line_file unless context.keys.include?(:recipe_path)
+ context[:recipe_line] = @new_resource.source_line_number unless context.keys.include?(:recipe_line)
+ context[:template_name] = @new_resource.source unless context.keys.include?(:template_name)
+ context[:template_path] = template_location unless context.keys.include?(:template_path)
+
context._extend_modules(@new_resource.helper_modules)
output = context.render_template(template_location)
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 60ec6bde93..5bef40625f 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -1627,16 +1627,31 @@ class Chef
run_context.delayed_notifications(self)
end
+ def source_line_file
+ if source_line
+ source_line.match(/(.*):(\d+):?.*$/).to_a[1]
+ else
+ nil
+ end
+ end
+
+ def source_line_number
+ if source_line
+ source_line.match(/(.*):(\d+):?.*$/).to_a[2]
+ else
+ nil
+ end
+ end
+
def defined_at
# The following regexp should match these two sourceline formats:
# /some/path/to/file.rb:80:in `wombat_tears'
# C:/some/path/to/file.rb:80 in 1`wombat_tears'
# extracting the path to the source file and the line number.
- (file, line_no) = source_line.match(/(.*):(\d+):?.*$/).to_a[1,2] if source_line
if cookbook_name && recipe_name && source_line
- "#{cookbook_name}::#{recipe_name} line #{line_no}"
+ "#{cookbook_name}::#{recipe_name} line #{source_line_number}"
elsif source_line
- "#{file} line #{line_no}"
+ "#{source_line_file} line #{source_line_number}"
else
"dynamically defined"
end