Permalink
Browse files

- add location finding

  • Loading branch information...
jordansissel committed Feb 21, 2012
1 parent c3dbac2 commit c38ac381a4b5da1b651ea618934081c480fc0766
View
@@ -0,0 +1,16 @@
+# Find where a module is loaded from
+
+This trick uses a template, $settings::modulepath, and __FILE__ from a template
+to find the location of a template (and thus its module) relative to the puppet
+module path.
+
+
+From this directory, run:
+
+ % puppet apply --modulepath . -e 'include whereareyou'
+
+Output:
+
+ notice: Scope(Class[Whereareyou]): [".", "whereareyou/templates/example.erb"]
+
+The output above is ["module path for this file", "file path relative to the modulepath"]
@@ -0,0 +1,3 @@
+class whereareyou {
+ notice(template("whereareyou/example.erb"))
+}
@@ -0,0 +1,10 @@
+<%=
+ scope.lookupvar("settings::modulepath").split(":").collect do |path|
+ expandedpath = File.expand_path(path)
+ if __FILE__.start_with?(expandedpath)
+ [path, __FILE__.gsub(expandedpath + "/", "")]
+ else
+ nil
+ end
+ end.select { |f| !f.nil? }.first
+%>

0 comments on commit c38ac38

Please sign in to comment.