-
Notifications
You must be signed in to change notification settings - Fork 123
Description
At the moment one can use source: auto in the template resource to search for the matching file somewhere under the templates dir in the directory of the file. These two are different operations:
- Search in subdirectories
- Search for a file with the matching name
but in the current implementation they are tied together, so it's "all or nothing": one can search in the subdirectories, but only if the file name matches. Sometimes it's inconvenient:
def self.ssh_service(name, num:, remote_ip:)
template "/etc/systemd/system/ssh-tunnel-#{name}.service" do
mode "644"
source "templates/etc/systemd/network/ssh-tunnel.service.erb"
variables(name: name, num: num, remote_ip: remote_ip)
notifies :run, "execute[systemctl daemon-reload]", :immediately
end
service("ssh-tunnel-#{name}") { action [:enable] }
endIn this case I use the same source file for a bunch of different destination files, so it cannot be automatically matched. So I have to specify the full path to the source file.
It would be great if there was an option to explicitly specify a name of the source file, but still let the code search the file for you in the subdirectories.
I see few possible approaches here:
- Treat
source "./templates/foo"as a specific path, butsource "foo"as an autosearch path (so it must start with./to be specific). This could be the best option, but it will break existing code (just like in my snippet above), so I doubt it's viable. - Extend
sourceto accept a new type. For example, a two-elements array likesource: [:auto, "foo.erb"], where the second argument is the filename to search for. - Add a new attribute to the
template, something likesource_filename, defaulting tonil.
So it could look like this:
# current code, searches for "bar" inside the "templates" and its subdirectories
template "foo/bar"
# current code, doesn't search but uses the specific path
template "foo/bar" do
source "templates/one/two/three/four.erb"
end
# proposed approach #2, searches for "four.erb" inside the "templates" and its subdirectories
template "foo/bar" do
source [:auto, "four.erb"]
end
# proposed approach #3, searches for "four.erb" inside the "templates" and its subdirectories
template "foo/bar" do
source_filename "four.erb"
endIf this proposal makes sense for you, I'll create a pull request.