|
7 | 7 | has_weight 150 |
8 | 8 |
|
9 | 9 | 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] |
20 | 41 | end |
21 | | - options[:facts] = facts |
22 | | - options[:node] ||= facts.facts['name'] |
23 | | - end |
| 42 | + ) |
24 | 43 | end |
25 | 44 | end |
0 commit comments