Sha256: 4285f12e52b15da5cef9f5b2a9a0a1bdb44d40c67d60a7f6a6db939179d6f609

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'aptible/api'
require 'git'

module Aptible
  module CLI
    module Helpers
      module App
        include Helpers::Token
        include Helpers::Environment

        class HandleFromGitRemote
          PATTERN = %r{
  :((?<environment_handle>[0-9a-z\-_\.]+?)/)?(?<app_handle>[0-9a-z\-_\.]+)\.git
  \z
}x

          def self.parse(url)
            PATTERN.match(url)
          end
        end

        def ensure_app(options = {})
          remote = options[:remote] || ENV['APTIBLE_REMOTE']
          handle = options[:app]
          if handle
            environment = ensure_environment(options)
          else
            handles = handle_from_remote(remote) || ensure_default_handle
            handle = handles[:app_handle]
            env_handle = handles[:environment_handle] || options[:environment]
            environment = ensure_environment(environment: env_handle)
          end

          app = app_from_handle(handle, environment)
          return app if app
          fail Thor::Error, "Could not find app #{handle}"
        end

        def app_from_handle(handle, environment)
          environment.apps.find do |a|
            a.handle == handle
          end
        end

        def ensure_default_handle
          return default_handle if default_handle
          fail Thor::Error, <<-ERR.gsub(/\s+/, ' ').strip
            Could not find app in current working directory, please specify
            with --app
          ERR
        end

        def default_handle
          handle_from_remote(:aptible)
        end

        def handle_from_remote(remote_name)
          git = Git.open(Dir.pwd)
          aptible_remote = git.remote(remote_name).url || ''
          HandleFromGitRemote.parse(aptible_remote)
        rescue
          nil
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aptible-cli-0.6.0 lib/aptible/cli/helpers/app.rb