lib/choria/colt/cli.rb in choria-colt-0.1.1 vs lib/choria/colt/cli.rb in choria-colt-0.2.0
- old
+ new
@@ -29,11 +29,14 @@
targets = options['targets'].split ','
targets = nil if options['targets'] == 'all'
results = colt.run_bolt_task task_name, input: input, targets: targets
- $stdout.puts JSON.pretty_generate(results)
+
+ File.write 'last_run.json', JSON.pretty_generate(results)
+
+ show_results(results)
rescue Choria::Orchestrator::Error => e
raise Thor::Error, "#{e.class}: #{e}"
end
desc 'show [task name] [options]', 'Show available tasks and task documentation'
@@ -52,17 +55,16 @@
default: 'production'
def show(*tasks_names)
environment = options['environment']
cache_directory = File.expand_path('.cache/colt/tasks')
FileUtils.mkdir_p cache_directory
- # TODO: Support multiple infrastructure
cache = Cache.new(path: File.join(cache_directory, "#{environment}.yaml"))
tasks = colt.tasks(environment: environment, cache: cache)
- if tasks_names.nil?
- show_tasks_summary
+ if tasks_names.empty?
+ show_tasks_summary(tasks)
else
tasks_names.each { |task_name| show_task_details(task_name, tasks) }
end
end
@@ -94,11 +96,30 @@
metadata = tasks[task_name]
puts <<~OUTPUT
Task: '#{task_name}'
#{metadata['metadata']['description']}
- #{metadata}
+ Parameters:
+ #{JSON.pretty_generate(metadata['metadata']['parameters']).gsub(/^/, ' ')}
OUTPUT
+ end
+
+ def show_results(results)
+ results.each { |result| show_result(result) }
+ end
+
+ def show_result(result)
+ return show_generic_output(result) unless result.dig(:result, '_output').nil? || (result.dig(:result, 'exit_code') != 0)
+
+ $stdout.puts JSON.pretty_generate(result)
+ end
+
+ def show_generic_output(result)
+ target = result[:sender]
+
+ output = result.dig(:result, '_output')
+ $stdout.puts "'#{target}':"
+ output.split("\n").each { |line| $stdout.puts(" #{line}") }
end
end
end
desc 'tasks', 'Show and run Bolt tasks.'