Skip to content

Commit

Permalink
Create directories for rendered files as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
aharpervc committed Oct 3, 2023
1 parent b4190b2 commit c9a299b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Better logging for template rendering status, errors, and config (verbose mode)
* 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

#### 0.11.0

Expand Down
8 changes: 7 additions & 1 deletion lib/consult/template.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'fileutils'
require_relative 'template_functions'
require_relative '../support/hash_extensions'

Expand All @@ -23,7 +24,12 @@ def render(save: true)
result = renderer.result(binding)

puts "Consult: Rendering #{name}" + (save ? " to #{dest}" : "...") if verbose?
File.open(dest, 'wb') { |f| f << result } if save

if save
FileUtils.mkdir_p(dest.dirname) unless dest.dirname.exist?
File.open(dest, 'wb') { |f| f << result }
end

result
rescue StandardError => e
STDERR.puts "Error rendering template: #{name}"
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
end
let(:fail_config) do
{
consul_key: 'templates/elements/aziz',
consul_key: 'templates/var-missing',
dest: 'rendered/nope/dest_fail.keep',
vars: {aziz: 'Light!'}
vars: {another_var: 'another value'}
}
end
let(:template) { Consult::Template.new(name, config) }
Expand Down
6 changes: 6 additions & 0 deletions spec/support/populate_consul.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ curl \
--data $'Aziz! <%= vars[:aziz] %> 🙄\n' \
http://localhost:8500/v1/kv/templates/elements/aziz

curl \
--request PUT \
-H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" \
--data $'<%= vars.fetch(:missing) %>\n' \
http://localhost:8500/v1/kv/templates/var-missing

curl \
--request PUT \
-H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" \
Expand Down

0 comments on commit c9a299b

Please sign in to comment.