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, #environment_href, #environment_map, #scoped_environments

Methods included from Token

#current_token_hash, #decode_token, #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_allObject



162
163
164
165
166
167
# File 'lib/aptible/cli/helpers/app.rb', line 162

def apps_all
  Aptible::Api::App.all(
    token: fetch_token,
    href: apps_href
  )
end

#apps_from_handle(handle, environment) ⇒ Object



169
170
171
172
173
174
175
176
# File 'lib/aptible/cli/helpers/app.rb', line 169

def apps_from_handle(handle, environment)
  # TODO: This should probably use each_app for more efficiency.
  if environment
    environment.apps
  else
    apps_all
  end.select { |a| a.handle == handle }
end

#apps_hrefObject



154
155
156
157
158
159
160
# File 'lib/aptible/cli/helpers/app.rb', line 154

def apps_href
  href = '/apps'
  if Renderer.format != 'json'
    href = '/apps?per_page=5000&no_embed=true'
  end
  href
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



178
179
180
181
182
183
184
185
# File 'lib/aptible/cli/helpers/app.rb', line 178

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)


187
188
189
190
191
192
# File 'lib/aptible/cli/helpers/app.rb', line 187

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)


194
195
196
197
# File 'lib/aptible/cli/helpers/app.rb', line 194

def validate_env_pair!(k, v)
  # Nil values
  raise Thor::Error, "Invalid argument: #{k}" if v.nil?
end