-
Notifications
You must be signed in to change notification settings - Fork 45
/
products.rb
84 lines (70 loc) · 2.72 KB
/
products.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'rmt/cli/repos'
class RMT::CLI::Products < RMT::CLI::Base
include ::RMT::CLI::ArrayPrintable
desc 'list', 'List products which are marked to be mirrored.'
option :all, aliases: '-a', type: :boolean, desc: 'List all products, including ones which are not marked to be mirrored'
option :release_stage, aliases: '-r', type: :string, desc: 'beta, released'
option :csv, type: :boolean, desc: 'Output data in CSV format'
def list
products = options.all ? Product.all : Product.mirrored
products = products.with_release_stage(options[:release_stage])
if products.empty?
if options.all
warn 'Run "rmt-cli sync" to synchronize with your SUSE Customer Center data first.'
else
warn 'No matching products found in the database.'
end
else
puts format_array(products, {
id: 'ID',
name: 'Name',
version: 'Version',
arch: 'Architecture',
product_string: 'Product string',
release_stage: 'Release stage',
'mirror?' => 'Mirror?',
last_mirrored_at: 'Last mirrored'
}, options.csv)
end
puts 'Only enabled products are shown by default. Use the `--all` option to see all products.' unless options.all || options.csv
end
map ls: :list
desc 'enable', 'Enable mirroring of product repositories by product ID or product string.'
def enable(target)
change_product(target, true)
end
desc 'disable', 'Disable mirroring of product repositories by product ID or product string.'
def disable(target)
change_product(target, false)
end
protected
def change_product(target, set_enabled)
product_id = Integer(target, 10) rescue nil
products = []
if product_id
product = Product.find(product_id)
products << product unless product.nil?
else
identifier, version, arch = target.split('/')
conditions = { identifier: identifier, version: version }
conditions[:arch] = arch if arch
products = Product.where(conditions).to_a
end
if set_enabled
products.each do |product|
extensions = Product.recommended_extensions(product.id).to_a
next if extensions.empty?
puts "The following required extensions for #{product.product_string} have been enabled: #{extensions.pluck(:name).join(', ')}."
products.push(*extensions)
end
end
repo_count = repository_service.change_mirroring_by_product!(set_enabled, products.uniq)
puts "#{repo_count} repo(s) successfully #{set_enabled ? 'enabled' : 'disabled'}."
rescue ActiveRecord::RecordNotFound
raise RMT::CLI::Error.new("Product by id \"#{product_id}\" not found.")
end
private
def repository_service
@repository_service ||= RepositoryService.new
end
end