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] BUG: Render invalid json when preview list #159

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
18 changes: 9 additions & 9 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ if
[ $UFFIZZI_SERVER ] &&
[ $UFFIZZI_PASSWORD ]
then
uffizzi login --username "${UFFIZZI_USER}" --server "${UFFIZZI_SERVER}"
uffizzi login --username "${UFFIZZI_USER}" --server "${UFFIZZI_SERVER}" --silent
if [ $UFFIZZI_PROJECT ]
then
uffizzi config set project "${UFFIZZI_PROJECT}"
uffizzi config set project "${UFFIZZI_PROJECT}" --silent
fi
else
if
[ $REQUEST_TOKEN ] &&
[ $REQUEST_TOKEN_URL ]
then
OIDC_TOKEN=$(curl -sLS "${REQUEST_TOKEN_URL}&audience=uffizzi" -H "User-Agent: actions/oidc-client" -H "Authorization: Bearer $REQUEST_TOKEN")
uffizzi login_by_identity_token --token "${OIDC_TOKEN}" --server "${UFFIZZI_SERVER}"
uffizzi login_by_identity_token --token "${OIDC_TOKEN}" --server "${UFFIZZI_SERVER}" --silent
else
echo "Specify environment variables to login before executing Uffizzi CLI."
echo "UFFIZZI_USER, UFFIZZI_SERVER, UFFIZZI_PASSWORD, and optionally UFFIZZI_PROJECT"
Expand All @@ -31,44 +31,44 @@ if
[ $DOCKERHUB_USERNAME ] &&
[ $DOCKERHUB_PASSWORD ]
then
uffizzi connect docker-hub --update-credential-if-exists
uffizzi connect docker-hub --update-credential-if-exists --silent
fi

if
[ $DOCKER_REGISTRY_USERNAME ] &&
[ $DOCKER_REGISTRY_PASSWORD ] &&
[ $DOCKER_REGISTRY_URL ]
then
uffizzi connect docker-registry --update-credential-if-exists
uffizzi connect docker-registry --update-credential-if-exists --silent
fi

if
[ $ACR_USERNAME ] &&
[ $ACR_PASSWORD ] &&
[ $ACR_REGISTRY_URL ]
then
uffizzi connect acr --update-credential-if-exists
uffizzi connect acr --update-credential-if-exists --silent
fi

if
[ $AWS_ACCESS_KEY_ID ] &&
[ $AWS_SECRET_ACCESS_KEY ] &&
[ $AWS_REGISTRY_URL ]
then
uffizzi connect ecr --update-credential-if-exists
uffizzi connect ecr --update-credential-if-exists --silent
fi

if
[ $GCLOUD_SERVICE_KEY ]
then
uffizzi connect gcr --update-credential-if-exists
uffizzi connect gcr --update-credential-if-exists --silent
fi

if
[ $GITHUB_USERNAME ] &&
[ $GITHUB_ACCESS_TOKEN ]
then
uffizzi connect ghcr --update-credential-if-exists
uffizzi connect ghcr --update-credential-if-exists --silent
fi

exec uffizzi "$@"
3 changes: 3 additions & 0 deletions lib/uffizzi/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def version
desc 'login [OPTIONS]', 'Login to Uffizzi to view and manage your previews'
method_option :server, required: false, aliases: '-s'
method_option :username, required: false, aliases: '-u'
method_option :silent, required: false, type: :boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use class_option:

module Uffizzi
  class Cli < Thor
    class_option :project, type: :string
    class_option :silent, type: :bool

def login
require_relative 'cli/login'
Login.new(options).run
Expand All @@ -27,6 +28,7 @@ def login
desc 'login_by_identity_token [OPTIONS]', 'Login or register to Uffizzi to view and manage your previews'
method_option :server, required: true, aliases: '-s'
method_option :token, required: true, aliases: '-t'
method_option :silent, required: false, type: :boolean
def login_by_identity_token
require_relative 'cli/login_by_identity_token'
LoginByIdentityToken.new(options).run
Expand All @@ -52,6 +54,7 @@ def logout
subcommand 'preview', Cli::Preview

desc 'connect', 'connect'
method_option :silent, required: false, type: :boolean
require_relative 'cli/connect'
subcommand 'connect', Cli::Connect

Expand Down
2 changes: 2 additions & 0 deletions lib/uffizzi/cli/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def get_value(property)
end

desc 'set [PROPERTY] [VALUE]', 'Sets the value of the specified option'
method_option :silent, required: false, type: :boolean
def set(property, value)
run('set', property, value)
end
Expand Down Expand Up @@ -78,6 +79,7 @@ def handle_get_command(property)
end

def handle_set_command(property, value)
Uffizzi.ui.disable_stdout if @options[:silent]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we should hide this functionality in Uffizzi.ui.say

ConfigFile.write_option(property.to_sym, value)
Uffizzi.ui.say("Updated property [#{property}]")
end
Expand Down
6 changes: 6 additions & 0 deletions lib/uffizzi/cli/connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def list_credentials
method_option :username, type: :string, aliases: :u
method_option :password, type: :string, aliases: :p
def docker_hub
Uffizzi.ui.disable_stdout if options[:silent]
type = Uffizzi.configuration.credential_types[:dockerhub]
credential_exists = credential_exists?(type)
handle_existing_credential_options('docker-hub') if credential_exists
Expand Down Expand Up @@ -57,6 +58,7 @@ def docker_hub
method_option :username, type: :string, aliases: :u
method_option :password, type: :string, aliases: :p
def docker_registry
Uffizzi.ui.disable_stdout if @options[:silent]
type = Uffizzi.configuration.credential_types[:docker_registry]
credential_exists = credential_exists?(type)
handle_existing_credential_options('docker-registry') if credential_exists
Expand Down Expand Up @@ -89,6 +91,7 @@ def docker_registry
method_option :username, type: :string, aliases: :u
method_option :password, type: :string, aliases: :p
def acr
Uffizzi.ui.disable_stdout if @options[:silent]
type = Uffizzi.configuration.credential_types[:azure]
credential_exists = credential_exists?(type)
handle_existing_credential_options('acr') if credential_exists
Expand Down Expand Up @@ -121,6 +124,7 @@ def acr
method_option :id, type: :string
method_option :secret, type: :string, aliases: :s
def ecr
Uffizzi.ui.disable_stdout if @options[:silent]
type = Uffizzi.configuration.credential_types[:amazon]
credential_exists = credential_exists?(type)
handle_existing_credential_options('ecr') if credential_exists
Expand Down Expand Up @@ -150,6 +154,7 @@ def ecr
desc: 'Skip raising an error within check the credential'
method_option :update_credential_if_exists, type: :boolean, default: false
def gcr(credential_file_path = nil)
Uffizzi.ui.disable_stdout if @options[:silent]
type = Uffizzi.configuration.credential_types[:google]
credential_exists = credential_exists?(type)
handle_existing_credential_options('gcr') if credential_exists
Expand Down Expand Up @@ -179,6 +184,7 @@ def gcr(credential_file_path = nil)
method_option :username, type: :string, aliases: :u
method_option :token, type: :string, aliases: :t
def ghcr
Uffizzi.ui.disable_stdout if @options[:silent]
type = Uffizzi.configuration.credential_types[:github_registry]
credential_exists = credential_exists?(type)
handle_existing_credential_options('ghcr') if credential_exists
Expand Down
1 change: 1 addition & 0 deletions lib/uffizzi/cli/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(options)
end

def run
Uffizzi.ui.disable_stdout if @options[:silent]
Uffizzi.ui.say('Login to Uffizzi server.')
server = set_server

Expand Down
1 change: 1 addition & 0 deletions lib/uffizzi/cli/login_by_identity_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(options)
end

def run
Uffizzi.ui.disable_stdout if @options[:silent]
token = @options[:token]
server = @options[:server]
params = prepare_request_params(token)
Expand Down
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
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
5 changes: 3 additions & 2 deletions man/uffizzi-config
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 "CONFIG" "" "May 2022" ""
.TH "CONFIG" "" "September 2022" ""
.SH "NAME"
\fBconfig\fR \- configure the Uffizzi command\-line interface (CLI)
.SH "SYNOPSIS"
Expand Down Expand Up @@ -32,7 +32,8 @@ COMMAND is one of the following:
Lists all options and their values from the config file\.

set OPTION VALUE
Sets the value of the specified option\.
Sets the value of the specified option\. This command has \-\-silent flag
which disable any output during execution\.

unset OPTION
Deletes the value of the specified option\.
Expand Down
3 changes: 2 additions & 1 deletion man/uffizzi-config.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ uffizzi config - configure the Uffizzi command-line interface (CLI)
Lists all options and their values from the config file.

set OPTION VALUE
Sets the value of the specified option.
Sets the value of the specified option. This command has --silent flag
which disable any output during execution.

unset OPTION
Deletes the value of the specified option.
Expand Down
5 changes: 4 additions & 1 deletion man/uffizzi-connect-acr
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\-CONNECT\-ACR" "" "August 2022" ""
.TH "UFFIZZI\-CONNECT\-ACR" "" "September 2022" ""
.SH "NAME"
\fBuffizzi\-connect\-acr\fR \- grant a Uffizzi user account access to a private Azure Container Registry (ACR)
.SH "SYNOPSIS"
Expand Down Expand Up @@ -31,6 +31,9 @@ Username for the service\.
.TP
\fB\-p\fR, \fB\-\-password=<password>\fR
Password for the service\.
.TP
\fB\-\-silent\fR
Disable any output during execution\.
.SH "EXAMPLES"
The following command will prompt the user to enter ACR credentials, including registry domain, Docker ID and password or access token:
.P
Expand Down
2 changes: 2 additions & 0 deletions man/uffizzi-connect-acr.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ https://docs.uffizzi.com/guides/container-registry-integrations
Username for the service.
* `-p`, `--password=<password>`:
Password for the service.
* `--silent`:
Disable any output during execution.

## EXAMPLES

Expand Down
5 changes: 4 additions & 1 deletion man/uffizzi-connect-docker-hub
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\-CONNECT\-DOCKER\-HUB" "" "August 2022" ""
.TH "UFFIZZI\-CONNECT\-DOCKER\-HUB" "" "September 2022" ""
.SH "NAME"
\fBuffizzi\-connect\-docker\-hub\fR \- grant a Uffizzi user account access to a private Docker Hub registry\.
.SH "SYNOPSIS"
Expand Down Expand Up @@ -28,6 +28,9 @@ Username for the service\.
.TP
\fB\-p\fR, \fB\-\-password=<password>\fR
Password for the service\.
.TP
\fB\-\-silent\fR
Disable any output during execution\.
.SH "EXAMPLES"
The following command will prompt the user to enter Docker Hub credentials, including Docker ID and password or access token:
.P
Expand Down
2 changes: 2 additions & 0 deletions man/uffizzi-connect-docker-hub.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ https://docs.uffizzi.com/guides/container-registry-integrations
Username for the service.
* `-p`, `--password=<password>`:
Password for the service.
* `--silent`:
Disable any output during execution.

## EXAMPLES

Expand Down
Loading