Skip to content

Commit 944f391

Browse files
authored
Merge pull request #403 from DannyBen/add/erb-config
Pre-process config files using ERB
2 parents 92d14eb + 44294bb commit 944f391

File tree

13 files changed

+67
-4
lines changed

13 files changed

+67
-4
lines changed

lib/bashly/config.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
module Bashly
44
# A convenience class to use either a hash or a filename as a configuration
5-
# source
5+
# source.
6+
#
7+
# When a filename is provided, it is loaded with these extra features:
8+
# - Support for `import` keyword to merge additional YAML files
9+
# - Preprocessing with ERB
610
class Config
711
using ComposeRefinements
812

913
attr_reader :config
1014

1115
def self.new(config)
1216
if config.is_a? String
13-
YAML.load_file(config).compose
17+
YAML.load_erb_file(config).compose
1418
else
1519
config
1620
end

lib/bashly/extensions/yaml.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ module YAML
44
# StackOverflow: https://stackoverflow.com/questions/71191685/visit-psych-nodes-alias-unknown-alias-default-psychbadalias/71192990#71192990
55
class << self
66
alias load unsafe_load
7+
8+
def load_erb_file(path)
9+
YAML.load ERB.new(File.read(path)).result
10+
end
711
end
812
end

lib/bashly/refinements/compose_refinements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def compose(keyword = 'import')
2222
end
2323

2424
def safe_load_yaml(path)
25-
loaded = YAML.load_file path
25+
loaded = YAML.load_erb_file path
2626
return loaded if loaded.is_a?(Array) || loaded.is_a?(Hash)
2727

2828
raise Bashly::ConfigurationError, "Cannot find a valid YAML in g`#{path}`"

spec/approvals/config/erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
commands:
3+
before: ok
4+
list:
5+
- file: import_one.yml
6+
erb: CHOP
7+
- file: import_two.yml
8+
more_imports:
9+
- file: last_file.yml
10+
after: ok

spec/approvals/examples/dependencies-alt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ args: none
1414
deps:
1515
- ${deps[git]} = /usr/bin/git
1616
- ${deps[http_client]} = /usr/bin/curl
17-
- ${deps[ruby]} = /home/vagrant/.rbenv/versions/3.1.3/bin/ruby
17+
- ${deps[ruby]} = /home/vagrant/.rbenv/versions/3.2.2/bin/ruby
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
commands:
3+
pi: 3.14
4+
list:
5+
- Item 1
6+
- Item 2
7+
- Item 3

spec/bashly/config_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,13 @@
3131
expect(subject['commands'].first['loaded']).to eq 'yes indeed'
3232
end
3333
end
34+
35+
context 'when the loaded YAML contains ERB and import directives' do
36+
let(:config) { 'spec/fixtures/erb/with_imports.yml' }
37+
38+
it 'evaluates ERB and imports' do
39+
expect(subject.to_yaml).to match_approval('config/erb')
40+
end
41+
end
3442
end
3543
end

spec/bashly/extensions/yaml_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe YAML do
4+
describe '::load_erb_file' do
5+
let(:path) { 'spec/fixtures/erb/simple.yml' }
6+
7+
it 'pre-processes the loaded YAML file with ERB' do
8+
expect(described_class.load_erb_file(path).to_yaml).to match_approval('extensions/yaml/load-erb-file')
9+
end
10+
end
11+
end

spec/fixtures/erb/import_one.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
file: import_one.yml
2+
erb: <%= 'C1-10P'.gsub('1-1', 'H').gsub('0', 'O') %>

spec/fixtures/erb/import_two.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
file: import_two.yml
2+
more_imports:
3+
- import: spec/fixtures/erb/last_file.yml

0 commit comments

Comments
 (0)