Skip to content

Commit

Permalink
Add configuration to skip missing templates
Browse files Browse the repository at this point in the history
- since `contents` is called multiple times and does work, it's now memoized
  • Loading branch information
aharpervc committed Oct 3, 2023
1 parent ac19c85 commit 798b070
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Development: adjust require's to be able to run CLI from any folder
* Enable defining vars at the env block level, rather than only on template blocks
* Create directories for rendered files as needed
* Add the `skip_missing_template` config for templates

#### 0.11.0

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ test:
vars:
test_specific_key: and_the_value

extra_test_config:
# normally there's an error for missing templates, but this can be allowed via config
skip_missing_template: true
# config files are also processed through ERB, so paths can be made dynamic
path: config/templates/<%= ENV['extra_test_file'] %>.yml
dest: config/extra_test_config.yml

production:
# vars can be defined at the environment level, which are available to these templates
vars:
Expand Down
24 changes: 21 additions & 3 deletions lib/consult/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def initialize(name, config)
end

def render(save: true)
if contents.empty? && @config[:skip_missing_template]
return
end

# Attempt to render
renderer = ERB.new(contents, nil, '-')
result = renderer.result(binding)
Expand Down Expand Up @@ -76,20 +80,34 @@ def ordered_locations

# Concatenate all the source templates together, in the order provided
def contents
ordered_locations.map do |location|
@_contents ||= ordered_locations.map do |location|
location.to_s.start_with?('consul') ? consul_contents(location) : disk_contents(location)
end.join
end.compact.join
end

def consul_contents(location)
[@config[location]].compact.flatten.map do |key|
Diplomat::Kv.get(key, {}, :return, :return).force_encoding 'utf-8'
Diplomat::Kv.get(key, {}, :reject, :return).force_encoding 'utf-8'
rescue Diplomat::KeyNotFound
if @config[:skip_missing_template]
STDERR.puts "Consult: Skipping missing template: #{name}"
next
end

raise
end.join
end

def disk_contents(location)
[public_send(location)].compact.flatten.map do |file_path|
File.read file_path, encoding: 'utf-8'
rescue Errno::ENOENT
if @config[:skip_missing_template]
STDERR.puts "Consult: Skipping missing template: #{name}"
next
end

raise
end.join
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/lib/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,24 @@
vars: {another_var: 'another value'}
}
end
let(:missing_template_file_config) do
{
skip_missing_template: true,
path: 'x/y/z.txt',
dest: 'rendered/nope/skip_missing_template_file',
}
end
let(:missing_template_key_config) do
{
skip_missing_template: true,
consul_key: 'x/y/z.txt',
dest: 'rendered/nope/skip_missing_template_key',
}
end
let(:template) { Consult::Template.new(name, config) }
let(:fail_template) { Consult::Template.new('aziz', fail_config) }
let(:missing_template_file) { Consult::Template.new('missing', missing_template_file_config) }
let(:missing_template_key) { Consult::Template.new('missing', missing_template_key_config) }

before :all do
Consult.load config_dir: 'spec/support'
Expand Down Expand Up @@ -59,6 +75,20 @@
expect { fail_template.render }.to output(/Error rendering template*/).to_stderr_from_any_process
end

context 'skip_missing_template' do
it 'allows missing template files' do
expect { missing_template_file.render }.to output(/Consult: Skipping missing template: missing/).to_stderr_from_any_process
expect(missing_template_file.render).to be_nil
expect(File.exist?(missing_template_file.dest)).to be(false)
end

it 'allows missing template keys' do
expect { missing_template_key.render }.to output(/Consult: Skipping missing template: missing/).to_stderr_from_any_process
expect(missing_template_key.render).to be_nil
expect(File.exist?(missing_template_key.dest)).to be(false)
end
end

it 'should obey location order' do
size = rand(4) + 1
locations = %i[path paths consul_key consul_keys].sample(size).shuffle
Expand Down
5 changes: 5 additions & 0 deletions spec/support/config/consult.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ shared:
vars:
aziz: 'Light!'

missing_template_file:
skip_missing_template: true
path: x/y/z.txt
dest: rendered/nope/skip_missing_template

test:
vars:
test_env_override: some value
Expand Down

0 comments on commit 798b070

Please sign in to comment.