Module: RHC::ContextHelpers

Includes:
GitHelpers
Included in:
RHC::Commands::Base
Defined in:
lib/rhc/context_helper.rb

Overview

Methods in this module should not attempt to read from the options hash in a recursive manner (server_context can’t read options.server).

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GitHelpers

#git_clone_application, #git_clone_deploy_hooks, #git_clone_repo, #git_config_get, #git_config_set, #git_version, #has_git?

Class Method Details

.included(other) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rhc/context_helper.rb', line 11

def self.included(other)
  other.module_eval do
    def self.takes_domain(opts={})
      if opts[:argument]
        argument :namespace, "Name of a domain", ["-n", "--namespace NAME"], :allow_nil => true, :default => :from_local_git
      else
        option ["-n", "--namespace NAME"], "Name of a domain", :default => :from_local_git
      end
    end
    # Does not take defaults to avoid conflicts
    def self.takes_application_or_domain(opts={})
      option ["-n", "--namespace NAME"], "Name of a domain"
      option ["-a", "--app NAME"], "Name of an application"
      if opts[:argument]
        argument :target, "The name of a domain, or an application name with domain (domain or domain/application)", ["-t", "--target NAME_OR_PATH"], :allow_nil => true, :covered_by => [:application_id, :namespace, :app]
      end
    end
    def self.takes_application(opts={})
      if opts[:argument]
        argument :app, "Name of an application", ["-a", "--app NAME"], :allow_nil => true, :default => :from_local_git, :covered_by => :application_id
      else
        option ["-a", "--app NAME"], "Name of an application", :default => :from_local_git, :covered_by => :application_id
      end
      option ["-n", "--namespace NAME"], "Name of a domain", :default => :from_local_git
      option ["--application-id ID"], "ID of an application", :hide => true, :default => :from_local_git, :covered_by => :app
    end
  end
end

Instance Method Details

#find_app(opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rhc/context_helper.rb', line 69

def find_app(opts={})
  if id = options.application_id
    if opts.delete(:with_gear_groups)
      return rest_client.find_application_by_id_gear_groups(id, opts)
    else
      return rest_client.find_application_by_id(id, opts)
    end
  end
  domain, app =
    if options.app
      if options.app =~ /\//
        options.app.split(/\//)
      else
        [options.namespace || namespace_context, options.app]
      end
    end
  if app && domain
    if opts.delete(:with_gear_groups)
      rest_client.find_application_gear_groups(domain, app, opts)
    else
      rest_client.find_application(domain, app, opts)
    end
  else
    raise ArgumentError, "You must specify an application with -a, or run this command from within a Git directory cloned from ProtonBox."
  end
end

#find_app_or_domain(opts = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rhc/context_helper.rb', line 49

def find_app_or_domain(opts={})
  domain, app =
    if options.target.present?
      options.target.split(/\//)
    elsif options.namespace || options.app
      if options.app =~ /\//
        options.app.split(/\//)
      else
        [options.namespace || namespace_context, options.app]
      end
    end
  if app && domain
    rest_client.find_application(domain, app)
  elsif domain
    rest_client.find_domain(domain)
  else
    raise ArgumentError, "You must specify a domain with -n, or an application with -a."
  end
end

#find_domain(opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/rhc/context_helper.rb', line 40

def find_domain(opts={})
  domain = options.namespace || options.target || namespace_context
  if domain
    rest_client.find_domain(domain)
  else
    raise ArgumentError, "You must specify a domain with -n."
  end
end

#from_local_git(defaults, arg) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/rhc/context_helper.rb', line 102

def from_local_git(defaults, arg)
  @local_git_config ||= {
    :application_id => git_config_get('pbox.app-id').presence,
    :app => git_config_get('pbox.app-name').presence,
    :namespace => git_config_get('pbox.domain-name').presence,
  }
  defaults[arg] ||= @local_git_config[arg] unless @local_git_config[arg].nil?
  @local_git_config
end

#namespace_contextObject



112
113
114
115
116
117
118
119
# File 'lib/rhc/context_helper.rb', line 112

def namespace_context
  # right now we don't have any logic since we only support one domain
  # TODO: add domain lookup based on uuid
  domain = rest_client.domains.first
  raise RHC::NoDomainsForUser if domain.nil?

  domain.name
end

#server_context(defaults = nil, arg = nil) ⇒ Object



96
97
98
99
100
# File 'lib/rhc/context_helper.rb', line 96

def server_context(defaults=nil, arg=nil)
  value = ENV['PROTONBOX_SERVER'] || (!options.clean && config['protonbox_server']) || "api.protonbox.com"
  defaults[arg] = value if defaults && arg
  value
end