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, #current_token_hash, #decode_token, #fetch_token, #save_token, #token_file, #whoami

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

#app_from_handle(handle, environment) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/aptible/cli/helpers/app.rb', line 164

def app_from_handle(handle, environment)
  url = "/find/app?handle=#{handle}"
  url += "&environment=#{environment.handle}" unless environment.nil?

  Aptible::Api::App.find_by_url(
    url,
    token: fetch_token
  )
rescue HyperResource::ClientError => e
  raise unless e.body.is_a?(Hash) &&
               e.body['error'] == 'multiple_resources_found'
  raise Thor::Error,
        "Multiple apps named #{handle} exist, please specify " \
        'with --environment'
end

#apps_allObject



157
158
159
160
161
162
# File 'lib/aptible/cli/helpers/app.rb', line 157

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

#apps_hrefObject



149
150
151
152
153
154
155
# File 'lib/aptible/cli/helpers/app.rb', line 149

def apps_href
  href = '/apps'
  if Renderer.format != 'json'
    href = '/apps?per_page=5000&no_embed=true'
  end
  href
end

#current_configuration(app) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'lib/aptible/cli/helpers/app.rb', line 201

def current_configuration(app)
  conf_link = app.links['current_configuration']
  return unless conf_link

  Aptible::Api::Configuration.find_by_url(
    conf_link.href,
    token: fetch_token,
    headers: { 'Prefer' => 'no_sensitive_extras=false' }
  )
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
# 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

  app = app_from_handle(s.app_handle, environment)

  if app.nil?
    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(' ')
  end

  app
end

#ensure_service(options, type) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/aptible/cli/helpers/app.rb', line 130

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



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

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)


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

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)


196
197
198
199
# File 'lib/aptible/cli/helpers/app.rb', line 196

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