Skip to content

Commit e6dce06

Browse files
committed
Allow fact file to be set per branch
1 parent befb3d3 commit e6dce06

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

lib/octocatalog-diff/cli/options.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,17 @@ def self.option_globally_or_per_branch_string(opts)
112112
translated = translate_option(opts[:translator], x)
113113
options[to_option] ||= translated
114114
options[from_option] ||= translated
115+
post_process(opts[:post_process], options)
115116
end
116117
parser.on("--to-#{flag}", "#{desc} for the to branch") do |x|
117118
validate_option(opts[:validator], x) if opts[:validator]
118119
options[to_option] = translate_option(opts[:translator], x)
120+
post_process(opts[:post_process], options)
119121
end
120122
parser.on("--from-#{flag}", "#{desc} for the from branch") do |x|
121123
validate_option(opts[:validator], x) if opts[:validator]
122124
options[from_option] = translate_option(opts[:translator], x)
125+
post_process(opts[:post_process], options)
123126
end
124127
end
125128

@@ -178,6 +181,15 @@ def self.translate_option(translator, value)
178181
return value if translator.nil?
179182
translator.call(value)
180183
end
184+
185+
# Code that can run after a translation and operate upon all options. This returns nothing but may
186+
# modify options that were input.
187+
# @param processor [Code] Processor function
188+
# @param options [Hash] Options hash
189+
def self.post_process(processor, options)
190+
return if processor.nil?
191+
processor.call(options)
192+
end
181193
end
182194
end
183195
end

lib/octocatalog-diff/cli/options/fact_file.rb

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,38 @@
77
has_weight 150
88

99
def parse(parser, options)
10-
parser.on('--fact-file FILENAME', 'Fact file to use instead of node lookup') do |fact_file|
11-
raise Errno::ENOENT, 'Invalid fact file provided' unless File.file?(fact_file)
12-
facts = nil
13-
local_opts = { fact_file_string: File.read(fact_file) }
14-
if fact_file =~ /\.ya?ml$/
15-
facts = OctocatalogDiff::Facts.new(local_opts.merge(backend: :yaml))
16-
elsif fact_file =~ /\.json$/
17-
facts = OctocatalogDiff::Facts.new(local_opts.merge(backend: :json))
18-
else
19-
raise ArgumentError, 'I do not know how to parse the provided fact file. Needs .yaml or .json extension.'
10+
OctocatalogDiff::Cli::Options.option_globally_or_per_branch(
11+
parser: parser,
12+
options: options,
13+
cli_name: 'fact-file',
14+
option_name: 'facts',
15+
desc: 'Override fact',
16+
datatype: '',
17+
validator: ->(fact_file) { File.file?(fact_file) && (fact_file =~ /\.ya?ml$/ || fact_file =~ /\.json$/) },
18+
translator: lambda do |fact_file|
19+
local_opts = { fact_file_string: File.read(fact_file) }
20+
if fact_file =~ /\.ya?ml$/
21+
OctocatalogDiff::Facts.new(local_opts.merge(backend: :yaml))
22+
elsif fact_file =~ /\.json$/
23+
OctocatalogDiff::Facts.new(local_opts.merge(backend: :json))
24+
else
25+
# :nocov:
26+
# Believed to be a bug condition since the validator should kick this out before it ever gets here.
27+
raise ArgumentError, 'I do not know how to parse the provided fact file. Needs .yaml or .json extension.'
28+
# :nocov:
29+
end
30+
end,
31+
post_process: lambda do |opts|
32+
unless options[:node]
33+
%w[to_facts from_facts facts].each do |opt|
34+
next unless opts[opt.to_sym] && opts[opt.to_sym].node
35+
opts[:node] = opts[opt.to_sym].node
36+
break
37+
end
38+
end
39+
40+
options[:facts] ||= options[:to_facts]
2041
end
21-
options[:facts] = facts
22-
options[:node] ||= facts.facts['name']
23-
end
42+
)
2443
end
2544
end

spec/octocatalog-diff/tests/cli/options/fact_file_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,28 @@
4848
end.to raise_error(Errno::ENOENT)
4949
end
5050
end
51+
52+
describe '#opt_to_fact_file' do
53+
it 'should distinguish between the to-facts and from-facts' do
54+
fact_file_1 = OctocatalogDiff::Spec.fixture_path('facts/facts.yaml')
55+
fact_file_2 = OctocatalogDiff::Spec.fixture_path('facts/fact-overrides-datatypes.yaml')
56+
result = run_optparse(['--fact-file', fact_file_1, '--to-fact-file', fact_file_2])
57+
58+
result_facts_1 = result[:facts].facts
59+
expect(result_facts_1).to be_a_kind_of(Hash)
60+
expect(result_facts_1['name']).to eq('rspec-node.xyz.github.net')
61+
expect(result_facts_1['values']).to be_a_kind_of(Hash)
62+
expect(result_facts_1['values']['fqdn']).to eq('rspec-node.xyz.github.net')
63+
expect(result_facts_1['values']['ipaddress']).to be_nil
64+
expect(result_facts_1['values'].keys).not_to include('expiration')
65+
66+
result_facts_2 = result[:to_facts].facts
67+
expect(result_facts_2).to be_a_kind_of(Hash)
68+
expect(result_facts_2['name']).to eq('rspec-node.xyz.github.net')
69+
expect(result_facts_2['values']).to be_a_kind_of(Hash)
70+
expect(result_facts_2['values']['fqdn']).to eq('rspec-node.xyz.github.net')
71+
expect(result_facts_2['values']['ipaddress']).to eq('10.20.30.40')
72+
expect(result_facts_2['values'].keys).not_to include('expiration')
73+
end
74+
end
5175
end

0 commit comments

Comments
 (0)