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

Add configuration to allow skipping missing template files/keys #48

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, options: {}, not_found: :return, found: :return).force_encoding 'utf-8'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you had to remove the not_found and found args here in order to implement the rescue logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in this commit message: 28220a8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the clarification

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}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
STDERR.puts "Consult: Skipping missing template: #{name}"
STDERR.puts "Consult: Skipping missing key: #{name}"

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, currently the thing being skipped is the template. Maybe it'd be better to include the file path or consul key causing the skip, so you know what's missing?

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