module Aptible::CLI::Helpers::App

Public Class Methods

included(base) click to toggle source
# File lib/aptible/cli/helpers/app.rb, line 23
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

apps_from_handle(handle, environment) click to toggle source
# File lib/aptible/cli/helpers/app.rb, line 154
def apps_from_handle(handle, environment)
  # TODO: This should probably use each_app for more efficiency.
  if environment
    environment.apps
  else
    Aptible::Api::App.all(token: fetch_token)
  end.select { |a| a.handle == handle }
end
ensure_app(options = {}) click to toggle source
# File lib/aptible/cli/helpers/app.rb, line 94
def ensure_app(options = {})
  s = handle_strategies.map { |cls| cls.new(options) }.find(&:usable?)

  if s.nil?
    err = 'Could not find app in current working directory, please ' \
          'specify with --app'
    raise Thor::Error, err
  end

  environment = nil
  if s.env_handle
    environment = environment_from_handle(s.env_handle)
    if environment.nil?
      err_bits = ['Could not find environment', s.env_handle]
      err_bits << s.explain
      raise Thor::Error, err_bits.join(' ')
    end
  end

  apps = apps_from_handle(s.app_handle, environment)

  case apps.count
  when 1
    return apps.first
  when 0
    err_bits = ['Could not find app', s.app_handle]
    if environment
      err_bits << 'in environment'
      err_bits << environment.handle
    else
      err_bits << 'in any environment'
    end
    err_bits << s.explain
    raise Thor::Error, err_bits.join(' ')
  else
    err = "Multiple apps named #{s.app_handle} exist, please specify " \
          'with --environment'
    raise Thor::Error, err
  end
end
ensure_service(options, type) click to toggle source
# File lib/aptible/cli/helpers/app.rb, line 135
def ensure_service(options, type)
  app = ensure_app(options)
  service = app.services.find { |s| s.process_type == type }

  if service.nil?
    valid_types = if app.services.empty?
                    'NONE (deploy the app first)'
                  else
                    app.services.map(&:process_type).join(', ')
                  end

    raise Thor::Error, "Service with type #{type} does not " \
                       "exist for app #{app.handle}. Valid " \
                       "types: #{valid_types}."
  end

  service
end
extract_env(args) click to toggle source
# File lib/aptible/cli/helpers/app.rb, line 163
def extract_env(args)
  Hash[args.map do |arg|
    k, v = arg.split('=', 2)
    validate_env_key!(k)
    validate_env_pair!(k, v)
    [k, v]
  end]
end
validate_env_key!(k) click to toggle source
# File lib/aptible/cli/helpers/app.rb, line 172
def validate_env_key!(k)
  # Keys that start with '-' are likely to be mispelled options. As of
  # May 2017 (> 3 years of Aptible!), there are only 2 such cases, both
  # of which are indeed mispelled options.
  raise Thor::Error, "Invalid argument: #{k}" if k.start_with?('-')
end
validate_env_pair!(k, v) click to toggle source
# File lib/aptible/cli/helpers/app.rb, line 179
def validate_env_pair!(k, v)
  # Nil values
  raise Thor::Error, "Invalid argument: #{k}" if v.nil?
end

Private Instance Methods

handle_strategies() click to toggle source
# File lib/aptible/cli/helpers/app.rb, line 186
def handle_strategies
  [OptionsHandleStrategy, GitRemoteHandleStrategy]
end