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] }
end
In 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, but source "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
source to accept a new type. For example, a two-elements array like source: [:auto, "foo.erb"], where the second argument is the filename to search for.
- Add a new attribute to the
template, something like source_filename, defaulting to nil.
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"
end
If this proposal makes sense for you, I'll create a pull request.
At the moment one can use
source: autoin thetemplateresource to search for the matching file somewhere under thetemplatesdir in the directory of the file. These two are different operations: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:
In 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:
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.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.template, something likesource_filename, defaulting tonil.So it could look like this:
If this proposal makes sense for you, I'll create a pull request.