Skip to content

Commit

Permalink
Fixes #37993 - Update cvpurge count to a better description
Browse files Browse the repository at this point in the history
  • Loading branch information
chris1984 committed Nov 7, 2024
1 parent 3c0ceca commit fa82d46
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
16 changes: 9 additions & 7 deletions lib/hammer_cli_katello/content_view_purge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class ContentViewPurgeCommand < HammerCLIKatello::Command
command_name "purge"
desc "Delete old versions of a content view"

option "--id", "ID", _("Content View numeric identifier")
option "--name", "NAME", _("Content View name")
option "--count", "COUNT", _("count of unused versions to keep"),
option "--id", "Id", _("Content View numeric identifier")
option "--name", "Name", _("Content View name")
option "--versions-to-keep", "Versions to keep", _("Number of unused versions to keep"),
default: 3, format: HammerCLI::Options::Normalizers::Number.new

validate_options :before, 'IdResolution' do
Expand Down Expand Up @@ -48,18 +48,19 @@ def option_sources
sources
end

# rubocop:disable Layout/LineLength
def execute
if option_count.negative?
output.print_error _("Invalid value for --count option: value must be 0 or greater.")
if option_versions_to_keep.negative?
output.print_error _("Invalid value for --versions-to-keep option: value must be 0 or greater.")
return HammerCLI::EX_USAGE
end

# Check if there is something to do
if option_count >= old_unused_versions.size
if option_versions_to_keep >= old_unused_versions.size
output.print_error _("No versions to delete.")
HammerCLI::EX_NOT_FOUND
else
versions_to_purge = old_unused_versions.slice(0, old_unused_versions.size - option_count)
versions_to_purge = old_unused_versions.slice(0, old_unused_versions.size - option_versions_to_keep)

versions_to_purge.each do |v|
purge_version(v)
Expand All @@ -68,6 +69,7 @@ def execute
end
end

# rubocop:enable Layout/LineLength
private def purge_version(v)
if option_async?
task = destroy(:content_view_versions, 'id' => v["id"])
Expand Down
12 changes: 7 additions & 5 deletions test/functional/content_view/purge_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ module HammerCLIKatello
it 'fails gracefully if count <= 0' do
api_expects_no_call

r = run_cmd(%w(content-view purge --id 2 --count -1))
assert(r.err.include?('Invalid value for --count option'), 'Incorrect error message')
r = run_cmd(%w(content-view purge --id 2 --versions-to-keep -1))
# rubocop:disable Layout/LineLength
assert(r.err.include?('Invalid value for --versions-to-keep option'), 'Incorrect error message')
# rubocop:enable Layout/LineLength
end

it 'fails gracefully when there are no versions to delete' do
ex = api_expects(:content_view_versions, :index)
ex = ex.with_params("content_view_id" => '2')
ex.returns(versions)

r = run_cmd(%w(content-view purge --id 2 --count 3))
r = run_cmd(%w(content-view purge --id 2 --versions-to-keep 3))
assert(r.err.include?('No versions to delete.'), 'Incorrect error message')
end

Expand All @@ -77,7 +79,7 @@ module HammerCLIKatello
expect_foreman_task('3')
end

run_cmd(%w(content-view purge --id 2 --count 0))
run_cmd(%w(content-view purge --id 2 --versions-to-keep 0))
end

it 'allows for async purge of versions' do
Expand All @@ -91,7 +93,7 @@ module HammerCLIKatello
ex.returns('id' => '3')
end

run_cmd(%w(content-view purge --id 2 --count 0 --async))
run_cmd(%w(content-view purge --id 2 --versions-to-keep 0 --async))
end
end
end

0 comments on commit fa82d46

Please sign in to comment.