Module: Nimbu::Command

Extended by:
Helpers
Defined in:
lib/nimbu/command.rb,
lib/nimbu/command/base.rb

Defined Under Namespace

Classes: Auth, Base, CommandFailed, Help, Init, Server, Themes

Constant Summary collapse

BaseWithApp =
Base

Class Method Summary collapse

Methods included from Helpers

action, ask, confirm, confirm_billing, confirm_command, create_git_remote, deprecate, disable_error_capture, display, display_header, display_object, display_row, display_table, enable_error_capture, error, error_with_failure, extended, extended_into, fail, format_bytes, format_date, format_with_bang, get_terminal_environment, git, has_git?, home_directory, hprint, hputs, included, included_into, json_decode, json_encode, longest, output, output_with_arrow, output_with_bang, quantify, redisplay, retry_on_exception, run_command, running_on_a_mac?, running_on_windows?, set_buffer, shell, status, string_distance, time_ago, truncate, with_tty

Class Method Details

.command_aliasesObject



21
22
23
# File 'lib/nimbu/command.rb', line 21

def self.command_aliases
  @@command_aliases ||= {}
end

.commandsObject



17
18
19
# File 'lib/nimbu/command.rb', line 17

def self.commands
  @@commands ||= {}
end

.current_argsObject



41
42
43
# File 'lib/nimbu/command.rb', line 41

def self.current_args
  @current_args
end

.current_commandObject



37
38
39
# File 'lib/nimbu/command.rb', line 37

def self.current_command
  @current_command
end

.current_optionsObject



45
46
47
# File 'lib/nimbu/command.rb', line 45

def self.current_options
  @current_options
end

.extract_error(body, options = {}) ⇒ Object



167
168
169
170
# File 'lib/nimbu/command.rb', line 167

def self.extract_error(body, options={})
  default_error = block_given? ? yield : "Internal server error.\nRun 'nimbu status' to check for known platform issues."
  parse_error_xml(body) || parse_error_json(body) || parse_error_plain(body) || default_error
end

.global_option(name, *args) ⇒ Object



53
54
55
# File 'lib/nimbu/command.rb', line 53

def self.global_option(name, *args)
  global_options << { :name => name, :args => args }
end

.global_optionsObject



49
50
51
# File 'lib/nimbu/command.rb', line 49

def self.global_options
  @global_options ||= []
end

.loadObject



11
12
13
14
15
# File 'lib/nimbu/command.rb', line 11

def self.load
  Dir[File.join(File.dirname(__FILE__), "command", "*.rb")].each do |file|
    require file
  end
end

.namespacesObject



25
26
27
# File 'lib/nimbu/command.rb', line 25

def self.namespaces
  @@namespaces ||= {}
end

.parse(cmd) ⇒ Object



163
164
165
# File 'lib/nimbu/command.rb', line 163

def self.parse(cmd)
  commands[cmd] || commands[command_aliases[cmd]]
end

.parse_error_json(body) ⇒ Object



179
180
181
182
# File 'lib/nimbu/command.rb', line 179

def self.parse_error_json(body)
  json = json_decode(body.to_s) rescue false
  json ? json['error'] : nil
end

.parse_error_plain(body) ⇒ Object



184
185
186
187
# File 'lib/nimbu/command.rb', line 184

def self.parse_error_plain(body)
  return unless body.respond_to?(:headers) && body.headers[:content_type].to_s.include?("text/plain")
  body.to_s
end

.parse_error_xml(body) ⇒ Object



172
173
174
175
176
177
# File 'lib/nimbu/command.rb', line 172

def self.parse_error_xml(body)
  xml_errors = REXML::Document.new(body).elements.to_a("//errors/error")
  msg = xml_errors.map { |a| a.text }.join(" / ")
  return msg unless msg.empty?
rescue Exception
end

.prepare_run(cmd, args = []) ⇒ Object

Raises:

  • (OptionParser::ParseError)


62
63
64
65
66
67
68
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
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/nimbu/command.rb', line 62

def self.prepare_run(cmd, args=[])
  command = parse(cmd)

  unless command
    if %w( -v --version ).include?(cmd)
      display Nimbu::VERSION
      exit
    end

    output_with_bang("`#{cmd}` is not a nimbu command.")

    distances = {}
    (commands.keys + command_aliases.keys).each do |suggestion|
      distance = string_distance(cmd, suggestion)
      distances[distance] ||= []
      distances[distance] << suggestion
    end

    if distances.keys.min < 4
      suggestions = distances[distances.keys.min].sort
      if suggestions.length == 1
        output_with_bang("Perhaps you meant `#{suggestions.first}`.")
      else
        output_with_bang("Perhaps you meant #{suggestions[0...-1].map {|suggestion| "`#{suggestion}`"}.join(', ')} or `#{suggestions.last}`.")
      end
    end

    output_with_bang("See `nimbu help` for additional details.")
    exit(1)
  end

  @current_command = cmd

  opts = {}
  invalid_options = []

  parser = OptionParser.new do |parser|
    global_options.each do |global_option|
      parser.on(*global_option[:args]) do |value|
        opts[global_option[:name]] = value
      end
    end
    command[:options].each do |name, option|
      parser.on("-#{option[:short]}", "--#{option[:long]}", option[:desc]) do |value|
        opts[name.gsub("-", "_").to_sym] = value
      end
    end
  end

  begin
    parser.order!(args) do |nonopt|
      invalid_options << nonopt
    end
  rescue OptionParser::InvalidOption => ex
    invalid_options << ex.args.first
    retry
  end

  raise OptionParser::ParseError if opts[:help]

  args.concat(invalid_options)

  @current_args = args
  @current_options = opts

  [ command[:klass].new(args.dup, opts.dup), command[:method] ]
end

.register_command(command) ⇒ Object



29
30
31
# File 'lib/nimbu/command.rb', line 29

def self.register_command(command)
  commands[command[:command]] = command
end

.register_namespace(namespace) ⇒ Object



33
34
35
# File 'lib/nimbu/command.rb', line 33

def self.register_namespace(namespace)
  namespaces[namespace[:name]] = namespace
end

.run(cmd, arguments = []) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/nimbu/command.rb', line 130

def self.run(cmd, arguments=[])
  object, method = prepare_run(cmd, arguments.dup)
  object.send(method)
rescue RestClient::Unauthorized
  puts "Authentication failure"
  unless ENV['HEROKU_API_KEY'] 
    run "login"
    retry
  end
rescue RestClient::PaymentRequired => e
  retry if run('account:confirm_billing', arguments.dup)
rescue RestClient::ResourceNotFound => e
  error extract_error(e.http_body) {
    e.http_body =~ /^[\w\s]+ not found$/ ? e.http_body : "Resource not found"
  }
rescue RestClient::Locked => e
  app = e.response.headers[:x_confirmation_required]
  if confirm_command(app, extract_error(e.response.body))
    arguments << '--confirm' << app
    retry
  end
rescue RestClient::RequestFailed => e
  error extract_error(e.http_body)
rescue RestClient::RequestTimeout
  error "API request timed out. Please try again, or contact [email protected] if this issue persists."
rescue CommandFailed => e
  error e.message
rescue OptionParser::ParseError => ex
  commands[cmd] ? run("help", [cmd]) : run("help")
rescue Interrupt => e
  error "\n[exited]"
end