Skip to content

Commit

Permalink
Merge branch 'custom-repos-extended-cli' into custom-repos-label
Browse files Browse the repository at this point in the history
  • Loading branch information
tmuntaner committed Feb 5, 2018
2 parents 3954dc0 + fbe3235 commit 82a6df8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
15 changes: 8 additions & 7 deletions lib/rmt/cli/repos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def list
end
map ls: :list

desc 'enable TARGET', 'Enable mirroring of repositories by repository ID or product string'
def enable(target)
change_mirroring(target, true)
desc 'enable ID', 'Enable mirroring of repositories by repository ID'
def enable(id)
change_mirroring(id, true)
end

desc 'disable TARGET', 'Disable mirroring of repositories by repository ID or product string'
def disable(target)
change_mirroring(target, false)
desc 'disable ID', 'Disable mirroring of repositories by repository ID'
def disable(id)
change_mirroring(id, false)
end

protected
Expand Down Expand Up @@ -51,9 +51,10 @@ def list_repositories(scope: :enabled)
def change_mirroring(id, set_enabled)
repository = Repository.find_by!(scc_id: id)
repository.change_mirroring!(set_enabled)

puts "Repository successfully #{set_enabled ? 'enabled' : 'disabled'}."
rescue ActiveRecord::RecordNotFound
raise RMT::CLI::Error, 'Repository not found.'
raise RMT::CLI::Error.new("Repository not found by id \"#{id}\".")
end

end
55 changes: 27 additions & 28 deletions lib/rmt/cli/repos_custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ def add(url, name)
def list
repositories = Repository.only_custom

if repositories.empty?
raise RMT::CLI::Error.new('No custom repositories found.')
else
puts array_to_table(repositories, {
custom_repository_id: 'ID',
name: 'Name',
external_url: 'URL',
enabled: 'Mandatory?',
mirroring_enabled: 'Mirror?',
last_mirrored_at: 'Last Mirrored'
})
end
raise RMT::CLI::Error.new('No custom repositories found.') if repositories.empty?

puts array_to_table(repositories, {
id: 'ID',
name: 'Name',
external_url: 'URL',
enabled: 'Mandatory?',
mirroring_enabled: 'Mirror?',
last_mirrored_at: 'Last Mirrored'
})
end
map ls: :list

Expand All @@ -53,21 +51,20 @@ def disable(id)

desc 'remove ID', 'Remove a custom repository'
def remove(id)
repository = find_repository(id)
repository = find_repository!(id)
repository.destroy!

puts "Removed custom repository by id \"#{id}\"."
end
map rm: :remove

desc 'products ID', 'Shows products attached to a custom repository'
def products(id)
repository = find_repository(id)

repository = find_repository!(id)
products = repository.products

if products.empty?
raise RMT::CLI::Error.new('No products attached to repository.')
end
raise RMT::CLI::Error.new('No products attached to repository.') if products.empty?

puts array_to_table(products, {
id: 'Product ID',
name: 'Product Name'
Expand All @@ -78,25 +75,27 @@ def products(id)
def attach(id, product_id)
product, repository = attach_or_detach(id, product_id)
repository_service.attach_product!(product, repository)
puts 'Attached repository to product'

puts "Attached repository to product \"#{product.name}\"."
end

desc 'detach ID PRODUCT_ID', 'Detach an existing custom repository from a product'
def detach(id, product_id)
product, repository = attach_or_detach(id, product_id)
repository_service.detach_product!(product, repository)
puts 'Detached repository from product'

puts "Detached repository from product \"#{product.name}\"."
end

private

def change_mirroring(id, set_enabled)
repository = find_repository(id)
repository = find_repository!(id)
repository.change_mirroring!(set_enabled)
puts "Repository successfully #{set_enabled ? 'enabled' : 'disabled'}."
end

def find_repository(namespaced_id)
def find_repository!(namespaced_id)
id_parts = namespaced_id.split(':')
raise StandardError unless (id_parts.size == 2) && (id_parts[0] == 'C')

Expand All @@ -108,19 +107,19 @@ def find_repository(namespaced_id)
raise RMT::CLI::Error.new("Cannot find custom repository by id \"#{namespaced_id}\".")
end

def find_product(id)
Product.find_by(id: id)
def find_product!(id)
Product.find_by!(id: id)
rescue ActiveRecord::RecordNotFound
raise RMT::CLI::Error.new("Cannot find product by id \"#{id}\".")
end

def repository_service
@repository_service ||= RepositoryService.new
end

def attach_or_detach(id, product_id)
repository = find_repository(id)
product = find_product(product_id)

raise RMT::CLI::Error.new("Cannot find product by id \"#{product_id}\".") if product.nil?
repository = find_repository!(id)
product = find_product!(product_id)

[product, repository]
end
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/rmt/cli/repos_custom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@

before do
expect(described_class).to receive(:exit)
expect { command }.to output(/Cannot find custom repository by id/).to_stderr.and output('').to_stdout
expect { command }.to output("Cannot find custom repository by id \"C:0\".\n").to_stderr.and output('').to_stdout
end

its(:mirroring_enabled) { is_expected.to be(false) }
Expand Down Expand Up @@ -137,7 +137,7 @@

before do
expect(described_class).to receive(:exit)
expect { command }.to output(/Cannot find custom repository by id/).to_stderr.and output('').to_stdout
expect { command }.to output("Cannot find custom repository by id \"C:0\".\n").to_stderr.and output('').to_stdout
end

its(:mirroring_enabled) { is_expected.to be(true) }
Expand Down Expand Up @@ -247,7 +247,7 @@
it('does not have an attached product') { expect(repository.products.count).to eq(0) }

it 'attaches the repository to the product' do
expect { described_class.start(argv) }.to output('').to_stderr.and output("Attached repository to product\n").to_stdout
expect { described_class.start(argv) }.to output('').to_stderr.and output("Attached repository to product \"#{product.name}\".\n").to_stdout
expect(repository.products.first.id).to eq(product.id)
end
end
Expand Down Expand Up @@ -308,7 +308,7 @@
it('has an attached product') { expect(repository.products.count).to eq(1) }

it 'detaches the repository from the product' do
expect { described_class.start(argv) }.to output('').to_stderr.and output("Detached repository from product\n").to_stdout
expect { described_class.start(argv) }.to output('').to_stderr.and output("Detached repository from product \"#{product.name}\".\n").to_stdout
expect(repository.products.count).to eq(0)
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/rmt/cli/repos_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
end

context 'repo id does not exist' do
let(:argv) { ['enable', (repository.scc_id + 1).to_s] }
let(:argv) { ['enable', 0] }

before do
expect(described_class).to receive(:exit)
expect { command }.to output("Repository not found.\n").to_stderr.and output('').to_stdout
expect { command }.to output("Repository not found by id \"0\".\n").to_stderr.and output('').to_stdout
end

its(:mirroring_enabled) { is_expected.to be(false) }
Expand Down Expand Up @@ -58,11 +58,11 @@
end

context 'repo id does not exist' do
let(:argv) { ['disable', (repository.scc_id + 1).to_s] }
let(:argv) { ['disable', 0] }

before do
expect(described_class).to receive(:exit)
expect { command }.to output("Repository not found.\n").to_stderr.and output('').to_stdout
expect { command }.to output("Repository not found by id \"0\".\n").to_stderr.and output('').to_stdout
end

its(:mirroring_enabled) { is_expected.to be(true) }
Expand Down

0 comments on commit 82a6df8

Please sign in to comment.