Module: Aptible::CLI::Helpers::App

Includes:
Environment, Token
Included in:
AppOrDatabase
Defined in:
lib/aptible/cli/helpers/app.rb

Defined Under Namespace

Modules: ClassMethods Classes: GitRemoteHandleStrategy, HandleFromGitRemote, OptionsHandleStrategy

Constant Summary

Constants included from Token

Token::TOKEN_ENV_VAR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Environment

#ensure_default_environment, #ensure_environment, #environment_from_handle, #scoped_environments

Methods included from Token

#current_token_hash, #fetch_token, #save_token, #token_file

Methods included from ConfigPath

#aptible_config_path

Class Method Details

.included(base) ⇒ Object



23
24
25
# File 'lib/aptible/cli/helpers/app.rb', line 23

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#apps_from_handle(handle, environment) ⇒ Object



154
155
156
157
158
159
160
161
# 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 = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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) ⇒ Object



163
164
165
166
167
168
169
170
# 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) ⇒ Object

Raises:

  • (Thor::Error)


172
173
174
175
176
177
# 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) ⇒ Object

Raises:

  • (Thor::Error)


179
180
181
182
# 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