Sha256: 45279a92232e664268adcc96c7bc0e5db9ef40c507ebb1add88dc202147e2768

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'io/console'
require 'uffizzi'
require 'uffizzi/auth_helper'
require 'uffizzi/response_helper'
require 'thor'

module Uffizzi
  class CLI::Project < Thor
    include ApiClient

    desc 'compose', 'compose'
    method_option :file, required: false, aliases: '-f'
    require_relative 'project/compose'
    subcommand 'compose', Uffizzi::CLI::Project::Compose

    desc 'list', 'list'
    def list
      run('list')
    end

    private

    def run(command)
      return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?

      case command
      when 'list'
        handle_list_command
      end
    end

    def handle_list_command
      hostname = ConfigFile.read_option(:hostname)
      response = fetch_projects(hostname)

      if ResponseHelper.ok?(response)
        handle_succeed_response(response)
      else
        ResponseHelper.handle_failed_response(response)
      end
    end

    def handle_succeed_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)
    end

    def print_projects(projects)
      projects_list = projects.reduce('') do |acc, project|
        "#{acc}#{project[:slug]}\n"
      end
      Uffizzi.ui.say(projects_list)
    end

    def set_default_project(project)
      ConfigFile.write_option(:project, project[:slug])
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
uffizzi-cli-0.2.2 lib/uffizzi/cli/project.rb
uffizzi-cli-0.2.1 lib/uffizzi/cli/project.rb
uffizzi-cli-0.2.0 lib/uffizzi/cli/project.rb