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

Run the support script via a less opinionated shell #47

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* Stop with error when force rendering and no template is found
* Remove non-functional short verbosity argument
* Add `--version` option to show the version
* 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

#### 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 @@ -94,8 +94,15 @@ test:
secrets:
path: config/templates/secrets.yml
dest: config/secrets.yml
# vars can be defined on a per-template basis
vars:
test_specific_key: and_the_value

production:
# vars can be defined at the environment level, which are available to these templates
vars:
hello: world

templates:
# You can concatenate multiple files together
my_config:
Expand Down
8 changes: 7 additions & 1 deletion lib/consult.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ def load(config_dir: nil, force_render: false, verbose: nil)
root directory: config_dir
yaml = root.join('config', 'consult.yml')

if verbose
puts "Consult: Loading config from #{yaml}"
end

@all_config = if yaml.exist?
if Gem::Version.new(YAML::VERSION) < Gem::Version.new('4.0')
YAML.safe_load(ERB.new(yaml.read).result, [], [], true, symbolize_names: true).to_h
else
YAML.safe_load(ERB.new(yaml.read).result, aliases: true, symbolize_names: true).to_h
end
else
STDERR.puts "Consult: No config file found at #{root} -> #{yaml}"
end

@all_config ||= {}

@config = @all_config[:shared].to_h.deep_merge @all_config[env&.to_sym].to_h
@templates = @config[:templates]&.map { |name, config| Template.new(name, config.merge(verbose: verbose)) } || []
@templates = @config[:templates]&.map { |name, config| Template.new(name, config.merge(verbose: verbose).merge(env_vars: @config[:vars])) } || []

@force_render = force_render

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

require_relative 'template_functions'
require_relative '../support/hash_extensions'

module Consult
class Template
Expand All @@ -21,12 +22,13 @@ def render(save: true)
renderer = ERB.new(contents, nil, '-')
result = renderer.result(binding)

puts "Consult: Rendering #{name}" + (save ? " to #{dest} ..." : "...") if verbose?
File.open(dest, 'wb') { |f| f << result } if save
puts "Consult: Rendered #{name}" if verbose?
result
rescue StandardError => e
STDERR.puts "Error rendering template: #{name}"
STDERR.puts e
STDERR.puts e.backtrace if verbose?
nil
end

Expand All @@ -39,7 +41,7 @@ def paths
end

def vars
@config[:vars]
@config[:env_vars].to_h.deep_merge @config[:vars].to_h
end

def dest
Expand Down
32 changes: 30 additions & 2 deletions spec/lib/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
end

it 'supports verbose output' do
t = Consult::Template.new 'verbose', config.merge(verbose: true)
t = Consult::Template.new 'verbose-template', config.merge(verbose: true)
expect(t.verbose?).to be(true)
expect { t.render }.to output(/Consult: Rendered verbose/i).to_stdout_from_any_process
expect { t.render }.to output(/Consult: Rendering verbose-template.../).to_stdout_from_any_process
end

it 'outputs render failures to stderr' do
Expand Down Expand Up @@ -95,6 +95,34 @@
expect(template.key('infrastructure/db1/dns')).to eq 'db1.local.net'
end

describe '#vars' do
let(:env_vars) do
{
env_vars: {
'test_env_override' => 'some value from env vars',
},
}
end

let(:env_vars_and_template_vars) do
env_vars.merge({
vars: {
'test_var_override' => 'some value from template vars',
},
})
end

it 'can read vars from environment block' do
config.merge! env_vars
expect(template.vars['test_env_override']).to eq 'some value from env vars'
end

it 'can read vars from vars block' do
config.merge! env_vars_and_template_vars
expect(template.vars['test_var_override']).to eq 'some value from template vars'
end
end

it '#with' do
expect { |b| template.with(0, &b) }.to yield_control
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@

config.before(:suite) do
puts 'Populating consul & vault...'
system '/bin/bash spec/support/populate_consul.sh'
system 'sh spec/support/populate_consul.sh'
end
end
3 changes: 3 additions & 0 deletions spec/support/config/consult.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ shared:
aziz: 'Light!'

test:
vars:
test_env_override: some value

templates:
secrets:
path: templates/secrets.yml.erb
Expand Down
2 changes: 2 additions & 0 deletions spec/support/templates/database.yml.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# rendered at <%= timestamp %>
# additional var: <%= vars.dig 'test_env_override' %>

common: &common
appname: Rails
adapter: postgres
Expand Down