class Capistrano::Harrow::Installer

Constants

MESSAGES
PROMPTS

Public Class Methods

new(params={ui: UI::TTY, api: API, config: Config::Git}) click to toggle source
# File lib/capistrano/harrow/installer.rb, line 91
def initialize(params={ui: UI::TTY, api: API, config: Config::Git})
  @ui = params.fetch(:ui)
  @config = params.fetch(:config)
  @api = params.fetch(:api)
  @quit = false
  @password = nil
  update_message_catalog!({messages: MESSAGES,
                           prompts: PROMPTS})
end

Public Instance Methods

install!() click to toggle source
# File lib/capistrano/harrow/installer.rb, line 101
def install!
  return if @config.disabled?
  return if @config.installed?
  messages = @api.participating?(signup_data)
  return unless messages
  update_message_catalog!(messages)

  @ui.show Banner.new.to_s
  @ui.show message(:preinstall, {})

  begin
    if @ui.prompt(prompt(:want_install)).downcase == 'no'
      quit!
      return
    end
  rescue UI::TimeoutError
    quit!("timeout")
    return
  end

  data = signup_data
  if data[:email].to_s.empty? or data[:name].to_s.empty?
    begin
      data[:name] = @ui.prompt(prompt(:enter_name), [])
      data[:email] = @ui.prompt(prompt(:enter_email), [])
    rescue UI::TimeoutError
      quit!("timeout")
    end
  end

  @ui.show message(:signup_data, data)
  unless data[:repository_url].to_s.empty?
    @ui.show message(:repository, data)
  end

  @password = prompt_password!
  unless @password
    quit!("no password provided")
    return
  end

  sign_up_user!
end
message(tag, format_data) click to toggle source
# File lib/capistrano/harrow/installer.rb, line 83
def message(tag, format_data)
  sprintf(@messages.fetch(tag, tag.to_s), format_data)
end
prompt(tag) click to toggle source
# File lib/capistrano/harrow/installer.rb, line 87
def prompt(tag)
  @prompts.fetch(tag, tag.to_s)
end
quit!(reason="") click to toggle source
# File lib/capistrano/harrow/installer.rb, line 154
def quit!(reason="")
  unless reason.empty?
    reason = ": #{reason}"
  end

  @quit = true
  @ui.show(message(:aborting,{reason: reason}))
end
quit?() click to toggle source
# File lib/capistrano/harrow/installer.rb, line 163
def quit?
  @quit
end
signup_data() click to toggle source
# File lib/capistrano/harrow/installer.rb, line 145
def signup_data
  {
    repository_url: @config.repository_url,
    name: @config.username,
    email: @config.email,
    password: @password,
  }
end

Private Instance Methods

prompt_password!() click to toggle source
# File lib/capistrano/harrow/installer.rb, line 209
def prompt_password!
  tries = 3
  while tries > 0
    password = @ui.prompt_password(prompt(:enter_password))
    confirmed_password = @ui.prompt_password(prompt(:confirm_password))


    if password == confirmed_password && password.to_s.length >= 10
      return password
    elsif password.to_s.length < 10
      @ui.show message(:password_too_short, {})
    else
      @ui.show message(:password_mismatch, {})
    end

    tries = tries - 1
  end
end
sign_up_user!() click to toggle source
# File lib/capistrano/harrow/installer.rb, line 168
def sign_up_user!
  begin
    response_data = @api.sign_up(signup_data)
    @config.session_uuid = response_data[:session_uuid]
    @config.project_uuid = response_data[:project_uuid]
    @config.organization_uuid = response_data[:organization_uuid]
    if response_data.fetch(:reason, 'ok') == 'invalid'
      if response_data.fetch(:errors, {}).fetch(:email, []).first == 'not_unique'
        @ui.show message(:existing_account_found, {})
        return :account_exists
      else
        @ui.show message(:api_fatal_error, {})
        return response_data
      end
    else
      success_message_data = response_data.merge(email: @config.email)
      @ui.show message(:installation_successful, success_message_data)
      return :signed_up
    end
  rescue API::NetworkError
    @ui.show message(:api_network_error, {})
    should_retry = false
    begin
      if @ui.prompt(prompt(:retry_request), [:no, :yes]) == :yes
        should_retry = true
      end
    rescue UI::TimeoutError
      quit!
      return
    end

    retry if should_retry
  rescue API::ProtocolError
    quit! message(:api_protocol_error, {})
    return
  rescue API::FatalError
    quit! message(:api_fatal_error, {})
    return
  end
end
update_message_catalog!(new_messages) click to toggle source
# File lib/capistrano/harrow/installer.rb, line 228
def update_message_catalog!(new_messages)
  return unless new_messages.respond_to? :fetch
  @messages = {} unless @messages
  @prompts = {} unless @prompts

  @messages.merge!(new_messages.fetch(:messages, {}))
  @prompts.merge!(new_messages.fetch(:prompts, {}))
end