From 4de63e956d554ad65a6817642a1ec0870c9ab057 Mon Sep 17 00:00:00 2001 From: Zipofar Date: Fri, 13 Oct 2023 18:49:47 +0300 Subject: [PATCH] [343] Refactor --- lib/uffizzi/cli/cluster.rb | 7 ++----- lib/uffizzi/cli/dev.rb | 8 +++----- lib/uffizzi/services/cluster_service.rb | 4 ---- lib/uffizzi/shell.rb | 14 ++++++++++++++ test/support/mocks/mock_shell.rb | 21 +++++++++++++++------ test/uffizzi/cli/dev_test.rb | 2 +- 6 files changed, 35 insertions(+), 21 deletions(-) diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index a08bf38d..27d8f473 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -158,11 +158,8 @@ def handle_describe_command(command_args) cluster_data = ClusterService.fetch_cluster_data(command_args[:cluster_name], **cluster_api_connection_params) render_data = ClusterService.build_render_data(cluster_data) - if Uffizzi.ui.output_format.nil? - Uffizzi.ui.say(ClusterService.stringify_render_data(render_data)) - else - Uffizzi.ui.say(render_data) - end + Uffizzi.ui.output_format = Uffizzi::UI::Shell::PRETTY_LIST + Uffizzi.ui.say(render_data) end def handle_delete_command(command_args) diff --git a/lib/uffizzi/cli/dev.rb b/lib/uffizzi/cli/dev.rb index b4febbae..663f465e 100644 --- a/lib/uffizzi/cli/dev.rb +++ b/lib/uffizzi/cli/dev.rb @@ -55,9 +55,7 @@ def describe(name = nil) check_login dev_environment = get_dev_environment(name) - if dev_environment.nil? && name.present? - return Uffizzi.ui.say("No running dev environment by name: #{name}") - elsif dev_environment.nil? + if dev_environment.nil? return Uffizzi.ui.say('No running dev environments') end @@ -65,9 +63,9 @@ def describe(name = nil) cluster_data = ClusterService.fetch_cluster_data(cluster_name, **cluster_api_connection_params) cluster_render_data = ClusterService.build_render_data(cluster_data) dev_environment_render_data = cluster_render_data.merge(config_path: dev_environment[:config_path]) - rendered_data = dev_environment_render_data.map { |k, v| "- #{k.to_s.upcase}: #{v}" }.join("\n").strip - Uffizzi.ui.say(rendered_data) + Uffizzi.ui.output_format = Uffizzi::UI::Shell::PRETTY_LIST + Uffizzi.ui.say(dev_environment_render_data) end private diff --git a/lib/uffizzi/services/cluster_service.rb b/lib/uffizzi/services/cluster_service.rb index e9485a3d..4da7d307 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -128,9 +128,5 @@ def build_render_data(cluster_data) url: cluster_data[:host], } end - - def stringify_render_data(data) - data.map { |k, v| "- #{k.to_s.upcase}: #{v}" }.join("\n").strip - end end end diff --git a/lib/uffizzi/shell.rb b/lib/uffizzi/shell.rb index b4514407..6009bf8e 100644 --- a/lib/uffizzi/shell.rb +++ b/lib/uffizzi/shell.rb @@ -11,6 +11,7 @@ class ExitError < Thor::Error; end PRETTY_JSON = 'pretty-json' REGULAR_JSON = 'json' + PRETTY_LIST = 'pretty-list' def initialize @shell = Thor::Shell::Basic.new @@ -73,12 +74,25 @@ def format_to_pretty_json(data) JSON.pretty_generate(data) end + def format_to_pretty_list(data) + case data + when Array + data.map { |v| format_to_pretty_list(v) }.join("\n\n") + when Hash + data.map { |k, v| "- #{k.to_s.upcase}: #{v}" }.join("\n").strip + else + data + end + end + def format_message(message) case output_format when PRETTY_JSON format_to_pretty_json(message) when REGULAR_JSON format_to_json(message) + when PRETTY_LIST + format_to_pretty_list(message) else message end diff --git a/test/support/mocks/mock_shell.rb b/test/support/mocks/mock_shell.rb index 57fa3032..c8664ca4 100644 --- a/test/support/mocks/mock_shell.rb +++ b/test/support/mocks/mock_shell.rb @@ -21,10 +21,6 @@ def value attr_accessor :messages, :output_format, :stdout_pipe - PRETTY_JSON = 'pretty-json' - REGULAR_JSON = 'json' - GITHUB_ACTION = 'github-action' - def initialize @messages = [] @command_responses = [] @@ -112,12 +108,25 @@ def format_to_github_action(data) end end + def format_to_pretty_list(data) + case data + when Array + data.map { |v| format_to_pretty_list(v) }.join("\n\n") + when Hash + data.map { |k, v| "- #{k.to_s.upcase}: #{v}" }.join("\n").strip + else + data + end + end + def format_message(message) case output_format - when PRETTY_JSON + when Uffizzi::UI::Shell::PRETTY_JSON format_to_pretty_json(message) - when REGULAR_JSON + when Uffizzi::UI::Shell::REGULAR_JSON format_to_json(message) + when Uffizzi::UI::Shell::PRETTY_LIST + format_to_pretty_list(message) else message end diff --git a/test/uffizzi/cli/dev_test.rb b/test/uffizzi/cli/dev_test.rb index b927ae86..66b3e849 100644 --- a/test/uffizzi/cli/dev_test.rb +++ b/test/uffizzi/cli/dev_test.rb @@ -217,6 +217,6 @@ def test_describe_dev_with_wrong_name @dev.describe(name) - assert_match("No running dev environment by name: #{name}", Uffizzi.ui.last_message) + assert_match('No running dev environment', Uffizzi.ui.last_message) end end