Skip to content

Commit 466f653

Browse files
author
Kevin Paulisse
committed
Add negative options for --hiera-path and --hiera-path-strip
1 parent 46afe5e commit 466f653

4 files changed

Lines changed: 74 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
def parse(parser, options)
99
parser.on('--hiera-path PATH', 'Path to hiera data directory, relative to top directory of repository') do |path_in|
10-
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive' if options.key?(:hiera_path_strip)
10+
if options.key?(:hiera_path_strip) && options[:hiera_path_strip] != :none
11+
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
12+
end
13+
14+
if options[:hiera_path] == :none
15+
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
16+
end
1117

1218
options[:hiera_path] = path_in
1319

@@ -18,5 +24,13 @@ def parse(parser, options)
1824
options[:hiera_path].sub!(%r{/+$}, '')
1925
raise ArgumentError, '--hiera-path must not be empty' if options[:hiera_path].empty?
2026
end
27+
28+
parser.on('--no-hiera-path', 'Do not use any default hiera path settings') do
29+
if options[:hiera_path].is_a?(String)
30+
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
31+
end
32+
33+
options[:hiera_path] = :none
34+
end
2135
end
2236
end

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@
66

77
def parse(parser, options)
88
parser.on('--hiera-path-strip PATH', 'Path prefix to strip when munging hiera.yaml') do |path_in|
9-
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive' if options.key?(:hiera_path)
9+
if options.key?(:hiera_path) && options[:hiera_path] != :none
10+
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
11+
end
12+
13+
if options[:hiera_path_strip] == :none
14+
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
15+
end
1016

1117
options[:hiera_path_strip] = path_in
1218
end
19+
20+
parser.on('--no-hiera-path-strip', 'Do not use any default hiera path strip settings') do
21+
if options[:hiera_path_strip].is_a?(String)
22+
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
23+
end
24+
options[:hiera_path_strip] = :none
25+
end
1326
end
1427
end

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,22 @@
2525
run_optparse(['--hiera-path', 'foo', '--hiera-path-strip', 'bar'])
2626
end.to raise_error(ArgumentError, /mutually exclusive/)
2727
end
28+
29+
it 'should recognize --no-hiera-path option' do
30+
result = run_optparse(['--no-hiera-path'])
31+
expect(result.fetch(:hiera_path, 'key-not-defined')).to eq(:none)
32+
end
33+
34+
it 'should error if --hiera-path and --no-hiera-path are used together (1)' do
35+
expect do
36+
run_optparse(['--hiera-path', 'foo/bar/baz', '--no-hiera-path'])
37+
end.to raise_error(ArgumentError, /mutually exclusive/)
38+
end
39+
40+
it 'should error if --hiera-path and --no-hiera-path are used together (2)' do
41+
expect do
42+
run_optparse(['--no-hiera-path', '--hiera-path', 'foo/bar/baz'])
43+
end.to raise_error(ArgumentError, /mutually exclusive/)
44+
end
2845
end
2946
end

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,33 @@
66
result = run_optparse(['--hiera-path-strip', '/var/tmp/foo/bar/baz'])
77
expect(result.fetch(:hiera_path_strip, 'key-not-defined')).to eq('/var/tmp/foo/bar/baz')
88
end
9+
10+
it 'should allow empty' do
11+
result = run_optparse(['--hiera-path-strip', ''])
12+
expect(result.fetch(:hiera_path_strip, 'key-not-defined')).to eq('')
13+
end
14+
15+
it 'should error if --hiera-path and --hiera-path-strip are both specified' do
16+
expect do
17+
run_optparse(['--hiera-path-strip', 'foo', '--hiera-path', 'bar'])
18+
end.to raise_error(ArgumentError, /mutually exclusive/)
19+
end
20+
21+
it 'should recognize --no-hiera-path-strip option' do
22+
result = run_optparse(['--no-hiera-path-strip'])
23+
expect(result.fetch(:hiera_path_strip, 'key-not-defined')).to eq(:none)
24+
end
25+
26+
it 'should error if --hiera-path-strip and --no-hiera-path-strip are used together (1)' do
27+
expect do
28+
run_optparse(['--hiera-path-strip', 'foo/bar/baz', '--no-hiera-path-strip'])
29+
end.to raise_error(ArgumentError, /mutually exclusive/)
30+
end
31+
32+
it 'should error if --hiera-path-strip and --no-hiera-path-strip are used together (2)' do
33+
expect do
34+
run_optparse(['--no-hiera-path-strip', '--hiera-path-strip', 'foo/bar/baz'])
35+
end.to raise_error(ArgumentError, /mutually exclusive/)
36+
end
937
end
1038
end

0 commit comments

Comments
 (0)