Skip to content

Commit

Permalink
[324] Various CLI updates
Browse files Browse the repository at this point in the history
  • Loading branch information
zipofar committed Sep 11, 2023
1 parent 35745d2 commit a68229e
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 26 deletions.
4 changes: 2 additions & 2 deletions lib/uffizzi/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +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 :email, required: false, aliases: '-e'
method_option :email, required: false, aliases: '-e', lazy_default: ''
def login
require_relative 'cli/login'
Login.new(options).run
Expand Down Expand Up @@ -121,7 +121,7 @@ def handle_repl_exceptions(exception)
when Thor::Error
raise exception
when Interrupt
raise Uffizzi::CliError.new('The command was interrupted')
nil
when StandardError
raise Uffizzi::CliError.new(exception.message)
else
Expand Down
17 changes: 8 additions & 9 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ def list
run('list')
end

desc 'create', 'Create a cluster'
method_option :name, type: :string, required: false, aliases: '-n'
desc 'create [NAME]', 'Create a cluster'
method_option :kubeconfig, type: :string, required: false, aliases: '-k'
method_option :manifest, type: :string, required: false, aliases: '-m'
method_option :'update-current-context', type: :boolean, required: false
method_option :'update-current-context', type: :boolean, required: false, default: true
method_option :output, required: false, type: :string, aliases: '-o', enum: ['json', 'pretty-json']
method_option :'creation-source', required: false, type: :string
def create
run('create')
def create(name = nil)
run('create', { name: name })
end

desc 'describe [NAME]', 'Describe a cluster'
Expand All @@ -42,7 +41,7 @@ def describe(name)
end

desc 'delete [NAME]', 'Delete a cluster'
method_option :'delete-config', required: false, type: :boolean, aliases: '-dc'
method_option :'delete-config', required: false, type: :boolean, default: true
def delete(name)
run('delete', cluster_name: name)
end
Expand All @@ -68,7 +67,7 @@ def run(command, command_args = {})
when 'list'
handle_list_command(project_slug)
when 'create'
handle_create_command(project_slug)
handle_create_command(project_slug, command_args)
when 'describe'
handle_describe_command(project_slug, command_args)
when 'delete'
Expand All @@ -94,9 +93,9 @@ def handle_list_command(project_slug)
end
end

def handle_create_command(project_slug)
def handle_create_command(project_slug, command_args)
Uffizzi.ui.disable_stdout if Uffizzi.ui.output_format
cluster_name = options[:name] || ClusterService.generate_name
cluster_name = command_args[:name] || ClusterService.generate_name
creation_source = options[:"creation-source"] || MANUAL

unless ClusterService.valid_name?(cluster_name)
Expand Down
8 changes: 4 additions & 4 deletions lib/uffizzi/cli/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(options)

def run
AuthHelper.sign_out if AuthHelper.signed_in?
return perform_email_login if @options[:email]
return perform_email_login unless @options[:email].nil?

perform_browser_login
end
Expand All @@ -45,7 +45,7 @@ def perform_email_login
def perform_browser_login
session_id = SecureRandom.uuid
response = create_access_token(@server, session_id)
return handle_failed_response(response) unless ResponseHelper.created?(response)
return ResponseHelper.handle_failed_response(response) unless ResponseHelper.created?(response)

url = browser_sign_in_url(@server, session_id)
open_browser(url)
Expand All @@ -68,7 +68,7 @@ def handle_token_success(response)
token = response[:body][:access_token]
Uffizzi::Token.delete
Uffizzi::Token.write(token)
Uffizzi.ui.say('Login successfull')
Uffizzi.ui.say('Login successful')

set_current_account_and_project
end
Expand All @@ -84,7 +84,7 @@ def handle_succeed_response(response, username)
ConfigFile.write_option(:server, @server)
ConfigFile.write_option(:username, username)
ConfigFile.write_option(:cookie, response[:headers])
Uffizzi.ui.say('Login successfull')
Uffizzi.ui.say('Login successful')

if ENV.fetch('CI_PIPELINE_RUN', false)
account = response[:body][:user][:default_account]
Expand Down
5 changes: 3 additions & 2 deletions lib/uffizzi/helpers/login_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ def prepare_request_params(username, password)

def set_server(options)
config_server = ConfigFile.exists? ? Uffizzi::ConfigHelper.read_option_from_config(:server) : nil
server_address = (options[:server] || config_server || Uffizzi.ui.ask('Server:')).sub(/\/+$/, '')
server_address = options[:server] || config_server || Uffizzi.configuration.default_server.to_s
server_address.start_with?('http:', 'https:') ? server_address : "https://#{server_address}"
end

def set_username(options)
config_username = ConfigFile.exists? ? Uffizzi::ConfigHelper.read_option_from_config(:username) : nil
options[:username] || config_username || Uffizzi.ui.ask('Username:')
options_username = options[:email].present? ? options[:email] : nil
options_username || config_username || Uffizzi.ui.ask('Username:')
end

def set_password
Expand Down
1 change: 1 addition & 0 deletions man/uffizzi-cluster-create.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ uffizzi-cluster-create - create a cluster

--update-current-context
Update current-context in kubeconfig file
Default is true

--output=pretty-json
--output=json
Expand Down
5 changes: 5 additions & 0 deletions man/uffizzi-cluster-delete.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ uffizzi-cluster-delete - delete a cluster
[CLUSTER_NAME]
Name for the cluster you want to delete.

## FLAGS
--delete-config=false
Delete cluster from kubeconfig.
Default is true.

## EXAMPLES
The following command deletes the cluster with CLUSTER_NAME my-cluster:

Expand Down
8 changes: 4 additions & 4 deletions test/uffizzi/cli/cluster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ def test_delete_cluster
end

def test_delete_cluster_with_flag_delete_config_and_single_cluster_in_kubeconfig
@cluster.options = command_options('delete-config' => true)
clusters_get_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json')
kubeconfig = Psych.safe_load(Base64.decode64(clusters_get_body[:cluster][:kubeconfig]))
clusters_config = [{ id: clusters_get_body[:cluster][:id], kubeconfig_path: Uffizzi.configuration.default_kubeconfig_path }]
Expand All @@ -275,6 +274,7 @@ def test_delete_cluster_with_flag_delete_config_and_single_cluster_in_kubeconfig
stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(clusters_get_body, @project_slug)
stubbed_uffizzi_cluster_delete_request = stub_uffizzi_delete_cluster(@project_slug)

@cluster.options = command_options('delete-config': true)
@cluster.delete('cluster-name')

kubeconfig_after_exclude = Psych.safe_load(File.read(Uffizzi.configuration.default_kubeconfig_path))
Expand All @@ -288,7 +288,6 @@ def test_delete_cluster_with_flag_delete_config_and_single_cluster_in_kubeconfig
end

def test_delete_cluster_with_flag_delete_config_and_multiply_clusters_in_kubeconfig
@cluster.options = command_options('delete-config' => true)
clusters_get_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json')
kubeconfig = Psych.safe_load(Base64.decode64(clusters_get_body[:cluster][:kubeconfig]))
clusters_config = [{ id: clusters_get_body[:cluster][:id], kubeconfig_path: Uffizzi.configuration.default_kubeconfig_path }]
Expand All @@ -313,6 +312,7 @@ def test_delete_cluster_with_flag_delete_config_and_multiply_clusters_in_kubecon
stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(clusters_get_body, @project_slug)
stubbed_uffizzi_cluster_delete_request = stub_uffizzi_delete_cluster(@project_slug)

@cluster.options = command_options('delete-config': true)
@cluster.delete('cluster-name')

kubeconfig_after_exclude = Psych.safe_load(File.read(Uffizzi.configuration.default_kubeconfig_path))
Expand All @@ -326,7 +326,6 @@ def test_delete_cluster_with_flag_delete_config_and_multiply_clusters_in_kubecon
end

def test_delete_cluster_with_flag_delete_config_and_kubeconfig_file_not_exists
@cluster.options = command_options('delete-config' => true)
clusters_get_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json')
clusters_config = [{ id: clusters_get_body[:cluster][:id], kubeconfig_path: Uffizzi.configuration.default_kubeconfig_path }]

Expand All @@ -335,6 +334,7 @@ def test_delete_cluster_with_flag_delete_config_and_kubeconfig_file_not_exists
stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(clusters_get_body, @project_slug)
stubbed_uffizzi_cluster_delete_request = stub_uffizzi_delete_cluster(@project_slug)

@cluster.options = command_options('delete-config': true)
@cluster.delete('cluster-name')

assert_match('Warning', Uffizzi.ui.last_message)
Expand All @@ -344,7 +344,6 @@ def test_delete_cluster_with_flag_delete_config_and_kubeconfig_file_not_exists
end

def test_delete_cluster_with_flag_delete_config_and_kubeconfig_file_has_empty_clusters
@cluster.options = command_options('delete-config' => true)
clusters_get_body = json_fixture('files/uffizzi/uffizzi_cluster_describe.json')
clusters_config = [{ id: clusters_get_body[:cluster][:id], kubeconfig_path: Uffizzi.configuration.default_kubeconfig_path }]
kubeconfig = Psych.safe_load(Base64.decode64(clusters_get_body[:cluster][:kubeconfig]))
Expand All @@ -359,6 +358,7 @@ def test_delete_cluster_with_flag_delete_config_and_kubeconfig_file_has_empty_cl
stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(clusters_get_body, @project_slug)
stubbed_uffizzi_cluster_delete_request = stub_uffizzi_delete_cluster(@project_slug)

@cluster.options = command_options('delete-config': true)
@cluster.delete('cluster-name')

kubeconfig_after_exclude = Psych.safe_load(File.read(Uffizzi.configuration.default_kubeconfig_path))
Expand Down
3 changes: 1 addition & 2 deletions test/uffizzi/cli/login_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def test_login_success_with_options_from_config
Uffizzi::ConfigFile.write_option(:project, 'project_slug_1')
Uffizzi::ConfigFile.write_option(:account, { 'id' => 1, 'name' => 'uffizzi' })

@cli.options = command_options(email: true)

@cli.options = command_options(email: '')
@cli.login

assert_requested(stubbed_uffizzi_login)
Expand Down
10 changes: 7 additions & 3 deletions uffizzi
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.push(File.expand_path('lib', __dir__))
begin
$LOAD_PATH.push(File.expand_path('lib', __dir__))

require 'uffizzi/cli'
Uffizzi::Cli.start
require 'uffizzi/cli'
Uffizzi::Cli.start
rescue Interrupt
exit(1)
end

0 comments on commit a68229e

Please sign in to comment.