Skip to content

Commit b0c46ed

Browse files
committed
Split hiera-path and hiera-path-strip to be per-branch too
1 parent dee4682 commit b0c46ed

File tree

4 files changed

+63
-38
lines changed

4 files changed

+63
-38
lines changed

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

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,42 @@
88
has_weight 181
99

1010
def parse(parser, options)
11-
parser.on('--hiera-path PATH', 'Path to hiera data directory, relative to top directory of repository') do |path_in|
12-
if options.key?(:hiera_path_strip) && options[:hiera_path_strip] != :none
13-
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
11+
OctocatalogDiff::Cli::Options.option_globally_or_per_branch(
12+
parser: parser,
13+
options: options,
14+
cli_name: 'hiera-path',
15+
option_name: 'hiera_path',
16+
desc: 'Path to hiera data directory, relative to top directory of repository',
17+
validator: lambda do |path|
18+
if path.start_with?('/')
19+
raise ArgumentError, '--hiera-path PATH must be a relative path not an absolute path'
20+
end
21+
end,
22+
translator: lambda do |path|
23+
result = path.sub(%r{/+$}, '')
24+
raise ArgumentError, '--hiera-path must not be empty' if result.empty?
25+
result
26+
end,
27+
post_process: lambda do |opts|
28+
if opts.key?(:to_hiera_path_strip) && opts[:to_hiera_path_strip] != :none
29+
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
30+
end
31+
if opts.key?(:from_hiera_path_strip) && opts[:from_hiera_path_strip] != :none
32+
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
33+
end
34+
if opts[:to_hiera_path] == :none || opts[:from_hiera_path] == :none
35+
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
36+
end
1437
end
15-
16-
if options[:hiera_path] == :none
17-
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
18-
end
19-
20-
options[:hiera_path] = path_in
21-
22-
if options[:hiera_path].start_with?('/')
23-
raise ArgumentError, '--hiera-path PATH must be a relative path not an absolute path'
24-
end
25-
26-
options[:hiera_path].sub!(%r{/+$}, '')
27-
raise ArgumentError, '--hiera-path must not be empty' if options[:hiera_path].empty?
28-
end
38+
)
2939

3040
parser.on('--no-hiera-path', 'Do not use any default hiera path settings') do
31-
if options[:hiera_path].is_a?(String)
41+
if options[:to_hiera_path].is_a?(String) || options[:from_hiera_path].is_a?(String)
3242
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
3343
end
3444

35-
options[:hiera_path] = :none
45+
options[:from_hiera_path] = :none
46+
options[:to_hiera_path] = :none
3647
end
3748
end
3849
end

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,31 @@
77
has_weight 182
88

99
def parse(parser, options)
10-
parser.on('--hiera-path-strip PATH', 'Path prefix to strip when munging hiera.yaml') do |path_in|
11-
if options.key?(:hiera_path) && options[:hiera_path] != :none
12-
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
10+
OctocatalogDiff::Cli::Options.option_globally_or_per_branch(
11+
parser: parser,
12+
options: options,
13+
cli_name: 'hiera-path-strip',
14+
option_name: 'hiera_path_strip',
15+
desc: 'Path prefix to strip when munging hiera.yaml',
16+
post_process: lambda do |opts|
17+
if opts.key?(:to_hiera_path) && opts[:to_hiera_path] != :none
18+
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
19+
end
20+
if opts.key?(:from_hiera_path) && opts[:from_hiera_path] != :none
21+
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
22+
end
23+
if opts[:to_hiera_path_strip] == :none || opts[:from_hiera_path_strip] == :none
24+
raise ArgumentError, '--hiera-path-strip and --no-hiera-path-strip are mutually exclusive'
25+
end
1326
end
14-
15-
if options[:hiera_path_strip] == :none
16-
raise ArgumentError, '--hiera-path-strip and --no-hiera-path-strip are mutually exclusive'
17-
end
18-
19-
options[:hiera_path_strip] = path_in
20-
end
27+
)
2128

2229
parser.on('--no-hiera-path-strip', 'Do not use any default hiera path strip settings') do
23-
if options[:hiera_path_strip].is_a?(String)
30+
if options[:to_hiera_path_strip].is_a?(String) || options[:from_hiera_path_strip].is_a?(String)
2431
raise ArgumentError, '--hiera-path-strip and --no-hiera-path-strip are mutually exclusive'
2532
end
26-
options[:hiera_path_strip] = :none
33+
options[:to_hiera_path_strip] = :none
34+
options[:from_hiera_path_strip] = :none
2735
end
2836
end
2937
end

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
describe '#opt_hiera_path' do
77
it 'should set options[:hiera_path] when relative path is specified' do
88
result = run_optparse(['--hiera-path', 'foo/bar/baz'])
9-
expect(result.fetch(:hiera_path, 'key-not-defined')).to eq('foo/bar/baz')
9+
expect(result.fetch(:to_hiera_path, 'key-not-defined')).to eq('foo/bar/baz')
10+
expect(result.fetch(:from_hiera_path, 'key-not-defined')).to eq('foo/bar/baz')
1011
end
1112

1213
it 'should error if an absolute path is specified' do
@@ -15,7 +16,8 @@
1516

1617
it 'should strip trailing slashes' do
1718
result = run_optparse(['--hiera-path', 'foo/bar/baz///'])
18-
expect(result.fetch(:hiera_path, 'key-not-defined')).to eq('foo/bar/baz')
19+
expect(result.fetch(:from_hiera_path, 'key-not-defined')).to eq('foo/bar/baz')
20+
expect(result.fetch(:to_hiera_path, 'key-not-defined')).to eq('foo/bar/baz')
1921
end
2022

2123
it 'should error if empty' do
@@ -30,7 +32,8 @@
3032

3133
it 'should recognize --no-hiera-path option' do
3234
result = run_optparse(['--no-hiera-path'])
33-
expect(result.fetch(:hiera_path, 'key-not-defined')).to eq(:none)
35+
expect(result.fetch(:to_hiera_path, 'key-not-defined')).to eq(:none)
36+
expect(result.fetch(:from_hiera_path, 'key-not-defined')).to eq(:none)
3437
end
3538

3639
it 'should error if --hiera-path and --no-hiera-path are used together (1)' do

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
describe OctocatalogDiff::Cli::Options do
66
describe '#opt_hiera_path_strip' do
7-
it 'should set options[:hiera_path_strip] when path is specified' do
7+
it 'should set options[:to_hiera_path_strip] when path is specified' do
88
result = run_optparse(['--hiera-path-strip', '/var/tmp/foo/bar/baz'])
9-
expect(result.fetch(:hiera_path_strip, 'key-not-defined')).to eq('/var/tmp/foo/bar/baz')
9+
expect(result.fetch(:to_hiera_path_strip, 'key-not-defined')).to eq('/var/tmp/foo/bar/baz')
10+
expect(result.fetch(:from_hiera_path_strip, 'key-not-defined')).to eq('/var/tmp/foo/bar/baz')
1011
end
1112

1213
it 'should allow empty' do
1314
result = run_optparse(['--hiera-path-strip', ''])
14-
expect(result.fetch(:hiera_path_strip, 'key-not-defined')).to eq('')
15+
expect(result.fetch(:to_hiera_path_strip, 'key-not-defined')).to eq('')
16+
expect(result.fetch(:from_hiera_path_strip, 'key-not-defined')).to eq('')
1517
end
1618

1719
it 'should error if --hiera-path and --hiera-path-strip are both specified' do
@@ -22,7 +24,8 @@
2224

2325
it 'should recognize --no-hiera-path-strip option' do
2426
result = run_optparse(['--no-hiera-path-strip'])
25-
expect(result.fetch(:hiera_path_strip, 'key-not-defined')).to eq(:none)
27+
expect(result.fetch(:to_hiera_path_strip, 'key-not-defined')).to eq(:none)
28+
expect(result.fetch(:from_hiera_path_strip, 'key-not-defined')).to eq(:none)
2629
end
2730

2831
it 'should error if --hiera-path-strip and --no-hiera-path-strip are used together (1)' do

0 commit comments

Comments
 (0)