Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#157] json fix #161

Merged
merged 6 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions lib/uffizzi/cli/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Cli::Preview < Thor

desc 'list', 'List all previews'
method_option :filter, required: false, type: :string, aliases: '-f'
method_option :output, required: false, type: :string, aliases: '-o', enum: ['json']
method_option :output, required: false, type: :string, aliases: '-o', enum: ['pretty-json']
def list
run('list')
end
Expand Down Expand Up @@ -87,6 +87,7 @@ def handle_list_command(project_slug, filter)

def handle_create_command(file_path, project_slug, labels)
Uffizzi.ui.disable_stdout unless options[:output].nil?

params = prepare_params(file_path, labels)

response = create_deployment(ConfigFile.read_option(:server), project_slug, params)
Expand Down Expand Up @@ -145,7 +146,8 @@ def handle_events_command(deployment_name, project_slug)
end

def handle_succeed_events_response(response)
Uffizzi.ui.pretty_say(response[:body][:events])
Uffizzi.ui.output_format = Uffizzi::UI::Shell::PRETTY_JSON
Uffizzi.ui.say(response[:body][:events])
end

def handle_delete_command(deployment_name, project_slug)
Expand Down Expand Up @@ -180,13 +182,12 @@ def handle_succeed_list_response(response)
deployments = response[:body][:deployments] || []
raise Uffizzi::Error.new('The project has no active deployments') if deployments.empty?

deployments.each do |deployment|
if Uffizzi.ui.output_format.nil?
Uffizzi.ui.say("deployment-#{deployment[:id]}")
else
Uffizzi.ui.pretty_say(deployment)
end
if Uffizzi.ui.output_format.nil?
deployments = deployments.reduce('') do |acc, deployment|
"#{acc}deployment-#{deployment[:id]}\n"
end.strip
end
Uffizzi.ui.say(deployments)
end

def handle_succeed_delete_response(deployment_id)
Expand All @@ -202,9 +203,8 @@ def handle_succeed_describe_response(response)

container
end
deployment.each_key do |key|
Uffizzi.ui.say("#{key}: #{deployment[key]}")
end
deployment_data = deployment.reduce('') { |acc, (key, value)| "#{acc}#{key}: #{value}\n" }.strip
Uffizzi.ui.say(deployment_data)
end

def hide_secrets(secret_variables)
Expand Down Expand Up @@ -240,7 +240,8 @@ def display_deployment_data(deployment, success)
Uffizzi.ui.say(preview_url) if success
else
deployment_data = build_deployment_data(deployment)
Uffizzi.ui.output(deployment_data)
Uffizzi.ui.enable_stdout
moklidia marked this conversation as resolved.
Show resolved Hide resolved
Uffizzi.ui.say(deployment_data)
end
end

Expand Down
39 changes: 24 additions & 15 deletions lib/uffizzi/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@ module UI
class Shell
attr_accessor :output_format

PRETTY_JSON = 'pretty-json'
REGULAR_JSON = 'json'
GITHUB_ACTION = 'github-action'

def initialize
@shell = Thor::Shell::Basic.new
end

def say(message)
@shell.say(message)
formatted_message = case output_format
when PRETTY_JSON
format_to_pretty_json(message)
when REGULAR_JSON
format_to_json(message)
when GITHUB_ACTION
format_to_github_action(message)
else
message
end
@shell.say(formatted_message)
end

def print_in_columns(messages)
Expand All @@ -34,33 +48,28 @@ def last_message
@shell.send(:stdout).string.strip
end

def pretty_say(collection, index = true)
ap(collection, { index: index })
end

def disable_stdout
$stdout = StringIO.new
end

def output(data)
def enable_stdout
$stdout = IO.new(1, 'w')
json_format? ? output_in_json(data) : output_in_github_format(data)
end

private

def json_format?
output_format == 'json'
def format_to_json(data)
data.to_json
end

def output_in_json(data)
say(data.to_json)
def format_to_pretty_json(data)
JSON.pretty_generate(data)
end

def output_in_github_format(data)
data.each_key do |key|
say("::set-output name=#{key}::#{data[key]}")
end
def format_to_github_action(data)
return '' unless data.is_a?(Hash)

data.reduce('') { |acc, (key, value)| "#{acc}::set-output name=#{key}::#{value}\n" }
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions man/uffizzi-preview-list
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" generated with Ronn-NG/v0.9.1
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
.TH "UFFIZZI\-PREVIEW\-LIST" "" "August 2022" ""
.TH "UFFIZZI\-PREVIEW\-LIST" "" "September 2022" ""
.SH "NAME"
\fBuffizzi\-preview\-list\fR \- list previews in a project
.SH "SYNOPSIS"
Expand All @@ -20,7 +20,7 @@ https://github\.com/UffizziCloud/uffizzi_cli
\-\-filter=METADATA
Metadata to filtering list of deployments\.

\-\-output=json
\-\-output=pretty\-json
Use this option for a more detailed description of listed
deployments\.
.fi
Expand All @@ -41,7 +41,7 @@ To list all previews in a project with name my_project, run:

To list all previews in json format, run:

$ uffizzi preview list \-\-output="json"
$ uffizzi preview list \-\-output="pretty\-json"

To list all previews filtered by metadata using single
label, run:
Expand Down
4 changes: 2 additions & 2 deletions man/uffizzi-preview-list.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ uffizzi-preview-list - list previews in a project
--filter=METADATA
Metadata to filtering list of deployments.

--output=json
--output=pretty-json
Use this option for a more detailed description of listed
deployments.

Expand All @@ -34,7 +34,7 @@ uffizzi-preview-list - list previews in a project

To list all previews in json format, run:

$ uffizzi preview list --output="json"
$ uffizzi preview list --output="pretty-json"

To list all previews filtered by metadata using single
label, run:
Expand Down
46 changes: 33 additions & 13 deletions test/support/mocks/mock_shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@
class MockShell
attr_accessor :messages, :output_format

PRETTY_JSON = 'pretty-json'
REGULAR_JSON = 'json'
GITHUB_ACTION = 'github-action'

def initialize
@messages = []
@output_enabled = true
end

def say(message)
@messages << message
return unless @output_enabled

formatted_message = case output_format
when PRETTY_JSON
format_to_pretty_json(message)
when REGULAR_JSON
format_to_json(message)
when GITHUB_ACTION
format_to_github_action(message)
else
message
end
@messages << formatted_message
end

def last_message
Expand All @@ -32,21 +49,24 @@ def describe_project(project)
end

def disable_stdout
true
@output_enabled = false
end

def output(data)
case output_format
when 'json'
say(data.to_json)
when 'github-action'
data.each_key do |key|
say("::set-output name=#{key}::#{data[key]}")
end
end
def enable_stdout
@output_enabled = true
end

private

def format_to_json(data)
data.to_json
end

def format_to_pretty_json(data)
JSON.pretty_generate(data)
end

def pretty_say(collection, _index = true)
collection
def format_to_github_action(data)
data.reduce('') { |acc, (key, value)| "#{acc}::set-output name=#{key}::#{value}\n" }
end
end
18 changes: 16 additions & 2 deletions test/uffizzi/cli/preview_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ def test_list_preview
assert_requested(stubbed_uffizzi_preview_list)
end

def test_list_preview_with_output_option
body = json_fixture('files/uffizzi/uffizzi_preview_list.json')
filter = {}
stubbed_uffizzi_preview_list = stub_uffizzi_preview_list_success(body, @project_slug, filter)

deployments = body[:deployments]

@preview.options = command_options(output: Uffizzi::UI::Shell::PRETTY_JSON)
@preview.list

assert_equal(JSON.pretty_generate(deployments), Uffizzi.ui.last_message)
assert_requested(stubbed_uffizzi_preview_list)
end

def test_list_preview_with_filter_success
body = json_fixture('files/uffizzi/uffizzi_preview_list.json')
filter = {
Expand Down Expand Up @@ -388,11 +402,11 @@ def test_update_preview_success_with_format
stubbed_uffizzi_preview_deploy_containers = stub_uffizzi_preview_deploy_containers_success(@project_slug, deployment_id)
stubbed_uffizzi_preview_activity_items = stub_uffizzi_preview_activity_items_success(activity_items_body, @project_slug, deployment_id)

@preview.options = command_options(output: 'github-action')
@preview.options = command_options(output: Uffizzi::UI::Shell::GITHUB_ACTION)
@preview.update("deployment-#{deployment_id}", 'test/compose_files/test_compose_success.yml')

expected_message_keys = ['name=id', 'name=url', 'containers_uri']
actual_messages = Uffizzi.ui.messages.last(3)
actual_messages = Uffizzi.ui.messages.last.split("\n")

expected_message_keys.zip(actual_messages).each do |(expected_msg_key, actual_msg)|
assert_match(expected_msg_key, actual_msg)
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_project_describe_success
project_slug = body[:project][:slug]
stubbed_uffizzi_projects = stub_uffizzi_project_success(body, project_slug)

@project.options = { output: 'json' }
@project.options = { output: Uffizzi::UI::Shell::REGULAR_JSON }

@project.describe(project_slug.to_s)

Expand Down