From 18063fd9e6b6b6fd0a3bcab32d98f258d0db6ad6 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 31 May 2021 09:07:22 +1000 Subject: [PATCH] fix: stop long values in columns from being truncated --- .../client/cli/environment_commands.rb | 12 +++---- .../client/cli/pacticipant_commands.rb | 6 ++-- .../client/matrix/text_formatter.rb | 34 ++++++++++++------- lib/pact_broker/client/pacticipants.rb | 1 - .../client/pacticipants/text_formatter.rb | 17 ++++++---- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/lib/pact_broker/client/cli/environment_commands.rb b/lib/pact_broker/client/cli/environment_commands.rb index 93a19aa2..0d914e0e 100644 --- a/lib/pact_broker/client/cli/environment_commands.rb +++ b/lib/pact_broker/client/cli/environment_commands.rb @@ -19,7 +19,7 @@ def self.shared_environment_options(name_required: false) shared_environment_options(name_required: true) shared_authentication_options def create_environment - execute_command(params_from_options(ENVIRONMENT_PARAM_NAMES), "CreateEnvironment") + execute_environment_command(params_from_options(ENVIRONMENT_PARAM_NAMES), "CreateEnvironment") end desc "update-environment", "Update an environment resource in the Pact Broker." @@ -27,7 +27,7 @@ def create_environment shared_environment_options(name_required: false) shared_authentication_options def update_environment - execute_command(params_from_options(ENVIRONMENT_PARAM_NAMES + [:uuid]), "UpdateEnvironment") + execute_environment_command(params_from_options(ENVIRONMENT_PARAM_NAMES + [:uuid]), "UpdateEnvironment") end desc "describe-environment", "Describe an environment" @@ -35,14 +35,14 @@ def update_environment method_option :output, aliases: "-o", desc: "json or text", default: 'text' shared_authentication_options def describe_environment - execute_command(params_from_options([:uuid]), "DescribeEnvironment") + execute_environment_command(params_from_options([:uuid]), "DescribeEnvironment") end desc "list-environments", "List environment" method_option :output, aliases: "-o", desc: "json or text", default: 'text' shared_authentication_options def list_environments - execute_command({}, "ListEnvironments") + execute_environment_command({}, "ListEnvironments") end desc "delete-environment", "Delete an environment" @@ -50,11 +50,11 @@ def list_environments method_option :output, aliases: "-o", desc: "json or text", default: 'text' shared_authentication_options def delete_environment - execute_command(params_from_options([:uuid]), "DeleteEnvironment") + execute_environment_command(params_from_options([:uuid]), "DeleteEnvironment") end no_commands do - def execute_command(params, command_class_name) + def execute_environment_command(params, command_class_name) require 'pact_broker/client/environments' command_options = { verbose: options.verbose, output: options.output } result = PactBroker::Client::Environments.const_get(command_class_name).call(params, command_options, pact_broker_client_options) diff --git a/lib/pact_broker/client/cli/pacticipant_commands.rb b/lib/pact_broker/client/cli/pacticipant_commands.rb index 64f0ef39..0246059f 100644 --- a/lib/pact_broker/client/cli/pacticipant_commands.rb +++ b/lib/pact_broker/client/cli/pacticipant_commands.rb @@ -16,7 +16,7 @@ def self.included(thor) def create_or_update_pacticipant(*required_but_ignored) raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.name.strip.size == 0 - execute_command(params_from_options(PACTICIPANT_PARAM_NAMES), 'Create') + execute_pacticipant_command(params_from_options(PACTICIPANT_PARAM_NAMES), 'Create') end desc 'list-pacticipants', 'List pacticipants' @@ -24,11 +24,11 @@ def create_or_update_pacticipant(*required_but_ignored) shared_authentication_options verbose_option def list_pacticipants - execute_command(params_from_options(PACTICIPANT_PARAM_NAMES), 'List') + execute_pacticipant_command(params_from_options(PACTICIPANT_PARAM_NAMES), 'List') end no_commands do - def execute_command(params, command_class_name) + def execute_pacticipant_command(params, command_class_name) require 'pact_broker/client/pacticipants' command_options = { verbose: options.verbose, output: options.output } result = PactBroker::Client::Pacticipants2.const_get(command_class_name).call(params, command_options, pact_broker_client_options) diff --git a/lib/pact_broker/client/matrix/text_formatter.rb b/lib/pact_broker/client/matrix/text_formatter.rb index 1daca98c..b491392d 100644 --- a/lib/pact_broker/client/matrix/text_formatter.rb +++ b/lib/pact_broker/client/matrix/text_formatter.rb @@ -11,21 +11,17 @@ class TextFormatter Line = Struct.new(:consumer, :consumer_version, :provider, :provider_version, :success, :ref, :ignored) - TP_OPTIONS = [ - { consumer: {} }, - { consumer_version: {display_name: 'C.VERSION'} }, - { provider: {} }, - { provider_version: {display_name: 'P.VERSION'} }, - { success: {display_name: 'SUCCESS?'} }, - { ref: { display_name: 'RESULT#' } } - ] - def self.call(matrix) - tp_options = TP_OPTIONS.dup matrix_rows = matrix[:matrix] return "" if matrix_rows.size == 0 + data = prepare_data(matrix_rows) + printer = TablePrint::Printer.new(data, tp_options(data)) + printer.table_print + verification_result_urls_text(matrix) + end + + def self.prepare_data(matrix_rows) verification_result_number = 0 - data = matrix_rows.each_with_index.collect do | line | + matrix_rows.each_with_index.collect do | line | has_verification_result_url = lookup(line, nil, :verificationResult, :_links, :self, :href) if has_verification_result_url verification_result_number += 1 @@ -40,9 +36,17 @@ def self.call(matrix) lookup(line, nil, :ignored) ) end + end - printer = TablePrint::Printer.new(data, tp_options) - printer.table_print + verification_result_urls_text(matrix) + def self.tp_options(data) + [ + { consumer: { width: max_width(data, :consumer, 'CONSUMER') } }, + { consumer_version: { display_name: 'C.VERSION', width: max_width(data, :consumer_version, 'C.VERSION') } }, + { provider: { width: max_width(data, :provider, 'PROVIDER') } }, + { provider_version: { display_name: 'P.VERSION', width: max_width(data, :provider_version, 'P.VERSION') } }, + { success: { display_name: 'SUCCESS?' } }, + { ref: { display_name: 'RESULT#' } } + ] end def self.lookup line, default, *keys @@ -75,6 +79,10 @@ def self.verification_result_urls_text(matrix) text end end + + def self.max_width(data, column, title) + (data.collect{ |row| row.send(column) } + [title]).compact.collect(&:size).max + end end end end diff --git a/lib/pact_broker/client/pacticipants.rb b/lib/pact_broker/client/pacticipants.rb index 8efe62e8..834b94aa 100644 --- a/lib/pact_broker/client/pacticipants.rb +++ b/lib/pact_broker/client/pacticipants.rb @@ -1,6 +1,5 @@ # New code Dir.glob(File.join(__FILE__.gsub(".rb", "/**/*.rb"))).sort.each do | path | - puts path require path end diff --git a/lib/pact_broker/client/pacticipants/text_formatter.rb b/lib/pact_broker/client/pacticipants/text_formatter.rb index 1aae3d96..25e28810 100644 --- a/lib/pact_broker/client/pacticipants/text_formatter.rb +++ b/lib/pact_broker/client/pacticipants/text_formatter.rb @@ -13,9 +13,13 @@ def self.call(pacticipants) OpenStruct.new({ uuid: "" }.merge(pacticipant).merge(url: pacticipant["_links"]["self"]["href"])) end.sort_by{ | pacticipant | pacticipant.name.downcase } - uuid_width = max_width(data, :uuid) - name_width = max_width(data, :name) - display_name_width = max_width(data, :display_name) + TablePrint::Printer.new(data, tp_options(data)).table_print + end + + def self.tp_options(data) + uuid_width = max_width(data, :uuid, "") + name_width = max_width(data, :name, "NAME") + display_name_width = max_width(data, :displayName, "DISPLAY NAME") tp_options = [ { name: { width: name_width} }, @@ -25,12 +29,11 @@ def self.call(pacticipants) if uuid_width > 0 tp_options.unshift({ uuid: { width: uuid_width } }) end - - TablePrint::Printer.new(data, tp_options).table_print + tp_options end - def self.max_width(data, column) - data.collect{ |row| row.send(column) }.compact.collect(&:size).max + def self.max_width(data, column, title) + (data.collect{ |row| row.send(column) } + [title]).compact.collect(&:size).max end end end