lib/choria/colt/cli.rb in choria-colt-0.4.0 vs lib/choria/colt/cli.rb in choria-colt-0.5.0
- old
+ new
@@ -25,30 +25,30 @@
option :targets_with_classes,
aliases: ['--targets-with-class', '-C'],
desc: 'Select the targets which have the specified Puppet classes.'
def run(*args) # rubocop:disable Metrics/AbcSize
input = extract_task_parameters_from_args(args)
+ targets = extract_targets_from_options
raise Thor::Error, 'Task name is required' if args.empty?
raise Thor::Error, "Too many arguments: #{args}" unless args.count == 1
raise Thor::Error, 'Flag --targets or --targets-with-class is required' if options['targets'].nil? && options['targets_with_classes'].nil?
task_name = args.shift
- targets = options['targets']&.split(',')
- targets = nil if options['targets'] == 'all'
+ logger.debug "Targets: #{targets}"
targets_with_classes = options['targets_with_classes']&.split(',')
results = colt.run_bolt_task task_name, input: input, targets: targets, targets_with_classes: targets_with_classes do |result|
$stdout.puts formatter.process_result(result)
end
File.write 'last_run.json', JSON.pretty_generate(results)
- rescue Choria::Orchestrator::Error => e
- raise Thor::Error, "#{e.class}: #{e}"
+ rescue Choria::Orchestrator::Error
+ # This error is already logged and displayed.
end
desc 'show [task name] [options]', 'Show available tasks and task documentation'
long_desc <<~DESC
Show available tasks and task documentation.
@@ -88,12 +88,12 @@
results = colt.wait_bolt_task task_id do |result|
$stdout.puts formatter.process_result(result)
end
File.write 'last_run.json', JSON.pretty_generate(results)
- rescue Choria::Orchestrator::Error => e
- raise Thor::Error, "#{e.class}: #{e}"
+ rescue Choria::Orchestrator::Error
+ # This error is already logged and displayed.
end
no_commands do # rubocop:disable Metrics/BlockLength
def colt
@colt ||= Choria::Colt.new logger: logger
@@ -126,9 +126,19 @@
value = true if value == 'true'
value = false if value == 'false'
[key, value]
end.to_h
+ end
+
+ def extract_targets_from_options
+ return nil if options['targets'] == 'all'
+
+ targets = options['targets']&.split(',')
+ targets&.map do |t|
+ t = File.read(t.sub(/^@/, '')).lines.map(&:strip) if t.start_with? '@'
+ t
+ end&.flatten
end
def show_tasks_summary(tasks)
tasks.reject! { |_task, metadata| metadata['metadata']['private'] }