lib/uffizzi/cli/project.rb in uffizzi-cli-0.8.0 vs lib/uffizzi/cli/project.rb in uffizzi-cli-0.9.0

- old
+ new

@@ -1,10 +1,12 @@ # frozen_string_literal: true require 'uffizzi' require 'uffizzi/auth_helper' require 'uffizzi/response_helper' +require 'uffizzi/helpers/project_helper' +require 'uffizzi/services/project_service' module Uffizzi class Cli::Project < Thor include ApiClient @@ -25,37 +27,109 @@ desc 'set-default PROJECT_SLUG', 'set-default' def set_default(project_slug) run('set-default', project_slug: project_slug) end + desc 'describe [PROJECT_SLUG]', 'describe' + method_option :output, type: :string, aliases: '-o', enum: ['json', 'pretty'], default: 'json' + def describe(project_slug) + run('describe', project_slug: project_slug) + end + map('set-default' => :set_default) + method_option :name, required: true + method_option :slug, default: '' + method_option :description, required: false + desc 'create', 'Create a project' + def create + run('create') + end + + desc 'delete [PROJECT_SLUG]', 'Delete a project' + def delete(project_slug) + run('delete', project_slug: project_slug) + end + private def run(command, project_slug: nil) return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? case command when 'list' handle_list_command when 'set-default' handle_set_default_command(project_slug) + when 'create' + handle_create_command + when 'delete' + handle_delete_command(project_slug) + when 'describe' + handle_describe_command(project_slug) end end + def handle_describe_command(project_slug) + response = describe_project(ConfigFile.read_option(:server), project_slug) + + if ResponseHelper.ok?(response) + handle_succeed_describe_response(response) + else + ResponseHelper.handle_failed_response(response) + end + end + + def handle_succeed_describe_response(response) + project = response[:body][:project] + project[:deployments] = ProjectService.select_active_deployments(project) + ProjectService.describe_project(project, options[:output]) + end + def handle_list_command server = ConfigFile.read_option(:server) response = fetch_projects(server) if ResponseHelper.ok?(response) - handle_succeed_response(response) + handle_list_success_response(response) else ResponseHelper.handle_failed_response(response) end end - def handle_succeed_response(response) + def handle_create_command + name = options[:name] + slug = options[:slug].empty? ? Uffizzi::ProjectHelper.generate_slug(name) : options[:slug] + raise Uffizzi::Error.new('Slug must not content spaces or special characters') unless slug.match?(/^[a-zA-Z0-9\-_]+\Z/i) + + server = ConfigFile.read_option(:server) + params = { + name: name, + description: options[:description], + slug: slug, + } + response = create_project(server, params) + + if ResponseHelper.created?(response) + handle_create_success_response(response) + else + ResponseHelper.handle_failed_response(response) + end + end + + def handle_delete_command(project_slug) + server = ConfigFile.read_option(:server) + response = delete_project(server, project_slug) + + if ResponseHelper.no_content?(response) + Uffizzi.ui.say("Project with slug #{project_slug} was deleted successfully") + else + ResponseHelper.handle_failed_response(response) + end + end + + def handle_list_success_response(response) projects = response[:body][:projects] return Uffizzi.ui.say('No projects related to this email') if projects.empty? set_default_project(projects.first) if projects.size == 1 print_projects(projects) @@ -72,9 +146,15 @@ end def handle_succeed_set_default_response(response) set_default_project(response[:body][:project]) Uffizzi.ui.say('Default project has been updated.') + end + + def handle_create_success_response(response) + project_name = response[:body][:project][:name] + + Uffizzi.ui.say("Project #{project_name} was successfully created") end def print_projects(projects) projects_list = projects.reduce('') do |acc, project| "#{acc}#{project[:slug]}\n"