require 'choria/colt/cli' require 'choria/colt/cli/thor' module Choria class Colt class CLI < Thor class Formatter attr_reader :pastel def initialize(colored:) @pastel = Pastel.new(enabled: colored) pastel.alias_color(:host, :cyan) end def process_result(result) return process_error(result) unless result.dig(:data, :exitcode)&.zero? process_success(result) end def process_success(result) output_lines = [ "#{pastel.host(result[:sender]).ljust(60, ' ')}duration: #{pastel.bright_white result[:data][:runtime]}", ] output_lines += if result.dig(:result, :_output).nil? JSON.pretty_generate(result[:result]).split("\n").map { |line| " #{line}" } else result.dig(:result, :_output).map { |line| " #{line}" } end output_lines.join("\n") end def process_error(result) # rubocop:disable Metrics/AbcSize host = "#{pastel.bright_red 'тип'} #{pastel.host(result[:sender]).ljust(60, ' ')}duration: #{pastel.bright_white result[:data][:runtime]}" output = result.dig(:result, '_output') error_details = JSON.pretty_generate(result.dig(:result, :_error, :details)).split "\n" error_description = [ "#{pastel.bright_red result.dig(:result, :_error, :kind)}: #{pastel.bright_white result.dig(:result, :_error, :msg)}", " details: #{error_details.shift}", error_details.map { |line| " #{line}" }, ] output_description = if output.nil? || output.empty? [] else [ nil, pastel.bright_red('output:'), output, ] end headline = "#{pastel.on_red ' '} " [ host, [ error_description, output_description, ].flatten.map { |line| "#{headline}#{line}" }, ].flatten.join("\n") end end end end end