Skip to content

Template: source: auto but with non-matching name #369

@ineu

Description

@ineu

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:

  1. Search in subdirectories
  2. 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:

  1. 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.
  2. 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.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions