Skip to content

Commit

Permalink
[343] Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zipofar committed Oct 16, 2023
1 parent 641bcae commit 301cdcc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
7 changes: 2 additions & 5 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions lib/uffizzi/cli/dev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,17 @@ 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

cluster_name = dev_environment[:name]
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
Expand Down
4 changes: 0 additions & 4 deletions lib/uffizzi/services/cluster_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions lib/uffizzi/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions test/support/mocks/mock_shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/dev_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 301cdcc

Please sign in to comment.