Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ineu opened this issue Jan 31, 2024 · 2 comments
Open

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

ineu opened this issue Jan 31, 2024 · 2 comments

Comments

@ineu
Copy link

ineu commented Jan 31, 2024

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.

@unasuke
Copy link
Member

unasuke commented Feb 1, 2024

@ineu It makes sense. I like 3 the best, 2 is my second favorite.

However, I have a question about the name of the "source_filename" attribute in 3.

Is it really "source_filename"? I need some time to think about it. 🤔

If we cannot discover a good name, we may choose 2.

@ineu
Copy link
Author

ineu commented Feb 1, 2024

Yeah, naming is difficult. It's the best I thought of at the moment, but I don't like it too much either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants