Module: Nimbu::Command
- Extended by:
- Helpers
- Defined in:
- lib/nimbu/command.rb,
lib/nimbu/command/base.rb
Defined Under Namespace
Classes: Auth, Base, Browse, CommandFailed, Help, Init, Server, Sites, Themes, Version
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, error_with_failure=, extended, extended_into, fail, format_bytes, format_date, format_error, format_with_bang, get_terminal_environment, git, has_git?, home_directory, hprint, hputs, included, included_into, json_decode, json_encode, launchy, line_formatter, 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, spinner, status, string_distance, styled_array, styled_error, styled_hash, styled_header, suggestion, time_ago, truncate, with_tty
#browser_launcher, #command?, #osx?, #tmp_dir, #which, #windows?
Class Method Details
.command_aliases ⇒ Object
27
28
29
|
# File 'lib/nimbu/command.rb', line 27
def self.command_aliases
@@command_aliases ||= {}
end
|
.commands ⇒ Object
23
24
25
|
# File 'lib/nimbu/command.rb', line 23
def self.commands
@@commands ||= {}
end
|
.current_args ⇒ Object
55
56
57
|
# File 'lib/nimbu/command.rb', line 55
def self.current_args
@current_args
end
|
.current_command ⇒ Object
47
48
49
|
# File 'lib/nimbu/command.rb', line 47
def self.current_command
@current_command
end
|
.current_command=(new_current_command) ⇒ Object
51
52
53
|
# File 'lib/nimbu/command.rb', line 51
def self.current_command=(new_current_command)
@current_command = new_current_command
end
|
.current_options ⇒ Object
59
60
61
|
# File 'lib/nimbu/command.rb', line 59
def self.current_options
@current_options ||= {}
end
|
.display_warnings ⇒ Object
75
76
77
78
79
|
# File 'lib/nimbu/command.rb', line 75
def self.display_warnings
unless warnings.empty?
$stderr.puts(warnings.map {|warning| " ! #{warning}"}.join("\n"))
end
end
|
237
238
239
240
|
# File 'lib/nimbu/command.rb', line 237
def self.(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
|
.files ⇒ Object
31
32
33
|
# File 'lib/nimbu/command.rb', line 31
def self.files
@@files ||= Hash.new {|hash,key| hash[key] = File.readlines(key).map {|line| line.strip}}
end
|
.global_option(name, *args, &blk) ⇒ Object
67
68
69
|
# File 'lib/nimbu/command.rb', line 67
def self.global_option(name, *args, &blk)
global_options << { :name => name.to_s, :args => args.sort.reverse, :proc => blk }
end
|
.global_options ⇒ Object
63
64
65
|
# File 'lib/nimbu/command.rb', line 63
def self.global_options
@global_options ||= []
end
|
.load ⇒ Object
17
18
19
20
21
|
# File 'lib/nimbu/command.rb', line 17
def self.load
Dir[File.join(File.dirname(__FILE__), "command", "*.rb")].each do |file|
require file
end
end
|
.namespaces ⇒ Object
35
36
37
|
# File 'lib/nimbu/command.rb', line 35
def self.namespaces
@@namespaces ||= {}
end
|
.parse(cmd) ⇒ Object
233
234
235
|
# File 'lib/nimbu/command.rb', line 233
def self.parse(cmd)
commands[cmd] || commands[command_aliases[cmd]]
end
|
.parse_error_json(body) ⇒ Object
249
250
251
252
|
# File 'lib/nimbu/command.rb', line 249
def self.parse_error_json(body)
json = json_decode(body.to_s) rescue false
json ? json['error'] : nil
end
|
.parse_error_plain(body) ⇒ Object
254
255
256
257
|
# File 'lib/nimbu/command.rb', line 254
def self.parse_error_plain(body)
return unless body.respond_to?(:headers) && body.[:content_type].to_s.include?("text/plain")
body.to_s
end
|
.parse_error_xml(body) ⇒ Object
242
243
244
245
246
247
|
# File 'lib/nimbu/command.rb', line 242
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
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
129
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/nimbu/command.rb', line 84
def self.prepare_run(cmd, args=[])
command = parse(cmd)
if args.include?('-h') || args.include?('--help')
args.unshift(cmd) unless cmd =~ /^-.*/
cmd = 'help'
command = parse(cmd)
end
if cmd == '--version'
cmd = 'version'
command = parse(cmd)
end
@current_command = cmd
@anonymized_args, @normalized_args = [], []
opts = {}
invalid_options = []
parser = OptionParser.new do |parser|
parser.base.long.delete('version')
(global_options + (command && command[:options] || [])).each do |option|
parser.on(*option[:args]) do |value|
if option[:proc]
option[:proc].call(value)
end
opts[option[:name].gsub('-', '_').to_sym] = value
ARGV.join(' ') =~ /(#{option[:args].map {|arg| arg.split(' ', 2).first}.join('|')})/
@anonymized_args << "#{$1} _"
@normalized_args << "#{option[:args].last.split(' ', 2).first} _"
end
end
end
begin
parser.order!(args) do |nonopt|
invalid_options << nonopt
@anonymized_args << '!'
@normalized_args << '!'
end
rescue OptionParser::InvalidOption => ex
invalid_options << ex.args.first
@anonymized_args << '!'
@normalized_args << '!'
retry
end
args.concat(invalid_options)
@current_args = args
@current_options = opts
@invalid_arguments = invalid_options
if opts[:debug]
Nimbu.debug = true
end
@anonymous_command = [ARGV.first, *@anonymized_args].join(' ')
begin
usage_directory = "#{home_directory}/.nimbu/usage"
FileUtils.mkdir_p(usage_directory)
usage_file = usage_directory << "/#{Nimbu::VERSION}"
usage = if File.exists?(usage_file)
json_decode(File.read(usage_file).force_encoding('UTF-8'))
else
{}
end
usage[@anonymous_command] ||= 0
usage[@anonymous_command] += 1
File.write(usage_file, json_encode(usage) + "\n")
rescue
end
if command
command_instance = command[:klass].new(args.dup, opts.dup)
if !@normalized_args.include?('--app _') && (implied_app = command_instance.app rescue nil)
@normalized_args << '--app _'
end
@normalized_command = [ARGV.first, @normalized_args.sort_by {|arg| arg.gsub('-', '')}].join(' ')
[ command_instance, command[:method] ]
else
error([
"`#{cmd}` is not a Nimbu command.",
suggestion(cmd, commands.keys + command_aliases.keys),
"See `Nimbu help` for a list of available commands."
].compact.join("\n"))
end
end
|
.register_command(command) ⇒ Object
39
40
41
|
# File 'lib/nimbu/command.rb', line 39
def self.register_command(command)
commands[command[:command]] = command
end
|
.register_namespace(namespace) ⇒ Object
43
44
45
|
# File 'lib/nimbu/command.rb', line 43
def self.register_namespace(namespace)
namespaces[namespace[:name]] = namespace
end
|
.run(cmd, arguments = []) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
# File 'lib/nimbu/command.rb', line 177
def self.run(cmd, arguments=[])
begin
object, method = prepare_run(cmd, arguments.dup)
object.send(method)
rescue Interrupt, StandardError, SystemExit => error
raise(error)
end
rescue CommandFailed => e
error e.message
rescue OptionParser::ParseError
commands[cmd] ? run("help", [cmd]) : run("help")
rescue Excon::Errors::SocketError => e
if e.message == 'getaddrinfo: nodename nor servname provided, or not known (SocketError)'
error("Unable to connect to Nimbu API, please check internet connectivity and try again.")
else
raise(e)
end
ensure
display_warnings
end
|
.warnings ⇒ Object
71
72
73
|
# File 'lib/nimbu/command.rb', line 71
def self.warnings
@warnings ||= []
end
|