diff --git a/lib/uffizzi/auth_helper.rb b/lib/uffizzi/auth_helper.rb index 91f20a3b..0d3d5d1a 100644 --- a/lib/uffizzi/auth_helper.rb +++ b/lib/uffizzi/auth_helper.rb @@ -4,7 +4,7 @@ module Uffizzi module AuthHelper class << self def signed_in? - config_data_exists? || Uffizzi::Token.exists? + config_data_exists? && authorized? end def sign_out @@ -16,13 +16,23 @@ def sign_out Uffizzi::Token.delete if Uffizzi::Token.exists? end + def check_login(options = nil) + raise Uffizzi::Error.new('You are not logged in. Run `uffizzi login`.') unless signed_in? + return unless options + + raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options) + end + private def config_data_exists? ConfigFile.exists? && - ConfigFile.option_has_value?(:account) && - ConfigFile.option_has_value?(:cookie) && - ConfigFile.option_has_value?(:server) + ConfigFile.option_has_value?(:server) && + ConfigFile.option_has_value?(:account) + end + + def authorized? + ConfigFile.option_has_value?(:cookie) || Uffizzi::Token.exists? end end end diff --git a/lib/uffizzi/cli/account.rb b/lib/uffizzi/cli/account.rb index a99ffc17..0012810e 100644 --- a/lib/uffizzi/cli/account.rb +++ b/lib/uffizzi/cli/account.rb @@ -25,7 +25,7 @@ def set_default(account_name) private def run(command, account_name = nil) - return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? + Uffizzi::AuthHelper.check_login case command when 'list' diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 02bbd31f..9677ca72 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -10,6 +10,7 @@ require 'uffizzi/services/cluster_service' require 'uffizzi/services/kubeconfig_service' require 'uffizzi/services/cluster/disconnect_service' +require 'byebug' module Uffizzi class Cli::Cluster < Thor @@ -65,9 +66,7 @@ def disconnect def run(command, command_args = {}) Uffizzi.ui.output_format = options[:output] - raise Uffizzi::Error.new('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? - raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options) - + Uffizzi::AuthHelper.check_login(options) project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project] case command @@ -335,7 +334,6 @@ def handle_succeed_create_response(cluster_data) end def save_kubeconfig(kubeconfig, kubeconfig_path) - kubeconfig_path = kubeconfig_path.nil? ? KubeconfigService.default_path : kubeconfig_path is_update_current_context = options[:'update-current-context'] KubeconfigService.save_to_filepath(kubeconfig_path, kubeconfig) do |kubeconfig_by_path| @@ -354,6 +352,8 @@ def save_kubeconfig(kubeconfig, kubeconfig_path) merged_kubeconfig end end + + Uffizzi.ui.say("Kubeconfig was updated by the path: #{kubeconfig_path}") if is_update_current_context end def update_clusters_config(id, params) diff --git a/lib/uffizzi/cli/dev.rb b/lib/uffizzi/cli/dev.rb index a41bbdb0..eea4a628 100644 --- a/lib/uffizzi/cli/dev.rb +++ b/lib/uffizzi/cli/dev.rb @@ -4,6 +4,7 @@ require 'uffizzi/services/cluster_service' require 'uffizzi/services/dev_service' require 'uffizzi/services/kubeconfig_service' +require 'uffizzi/auth_helper' module Uffizzi class Cli::Dev < Thor @@ -14,10 +15,10 @@ class Cli::Dev < Thor method_option :'default-repo', type: :string method_option :kubeconfig, type: :string def start(config_path = 'skaffold.yaml') + Uffizzi::AuthHelper.check_login(options) DevService.check_skaffold_existence DevService.check_running_daemon if options[:quiet] DevService.check_skaffold_config_existence(config_path) - check_login cluster_id, cluster_name = start_create_cluster kubeconfig = wait_cluster_creation(cluster_name) @@ -49,11 +50,6 @@ def stop private - def check_login - raise Uffizzi::Error.new('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? - raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options) - end - def start_create_cluster cluster_name = ClusterService.generate_name creation_source = ClusterService::MANUAL_CREATION_SOURCE diff --git a/lib/uffizzi/cli/preview.rb b/lib/uffizzi/cli/preview.rb index edc6c198..77ec38d9 100644 --- a/lib/uffizzi/cli/preview.rb +++ b/lib/uffizzi/cli/preview.rb @@ -55,8 +55,7 @@ def events(deployment_name) def run(command, file_path: nil, deployment_name: nil) Uffizzi.ui.output_format = options[:output] - raise Uffizzi::Error.new('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? - raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options) + Uffizzi::AuthHelper.check_login(options) project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project] diff --git a/lib/uffizzi/cli/preview/service.rb b/lib/uffizzi/cli/preview/service.rb index a65b3199..d22699ae 100644 --- a/lib/uffizzi/cli/preview/service.rb +++ b/lib/uffizzi/cli/preview/service.rb @@ -12,8 +12,7 @@ class Cli::Preview::Service < Thor desc 'logs [LOGS_TYPE] [DEPLOYMENT_ID] [CONTAINER_NAME]', 'Show the logs for a container service of a preview' def logs(logs_type, deployment_name, container_name = args) - return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? - return Uffizzi.ui.say('This command needs project to be set in config file') unless CommandService.project_set?(options) + Uffizzi::AuthHelper.check_login(options) deployment_id = PreviewService.read_deployment_id(deployment_name) response = service_logs_response(logs_type, deployment_id, container_name) @@ -28,8 +27,7 @@ def logs(logs_type, deployment_name, container_name = args) desc 'list [DEPLOYMENT_ID]', 'List the container services of a given compose environment (preview)' def list(deployment_name) - return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? - return Uffizzi.ui.say('This command needs project to be set in config file') unless CommandService.project_set?(options) + Uffizzi::AuthHelper.check_login(options) project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project] server = ConfigFile.read_option(:server) diff --git a/lib/uffizzi/cli/project.rb b/lib/uffizzi/cli/project.rb index 4652a2df..40725d6a 100644 --- a/lib/uffizzi/cli/project.rb +++ b/lib/uffizzi/cli/project.rb @@ -55,7 +55,7 @@ def delete(project_slug) private def run(command, project_slug: nil) - return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? + Uffizzi::AuthHelper.check_login case command when 'list' diff --git a/lib/uffizzi/cli/project/compose.rb b/lib/uffizzi/cli/project/compose.rb index 66771989..63736316 100644 --- a/lib/uffizzi/cli/project/compose.rb +++ b/lib/uffizzi/cli/project/compose.rb @@ -29,8 +29,7 @@ def describe private def run(command) - return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? - return Uffizzi.ui.say('This command needs project to be set in config file') unless CommandService.project_set?(options) + Uffizzi::AuthHelper.check_login(options) project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project] file_path = options[:file]