diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 7dfc891e..bc25eae9 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -144,11 +144,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 a42f2f20..d0022a33 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 1ac1bb8a..ac4f9f5f 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -81,9 +81,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