Module: Zillabyte::Command
- Defined in:
- lib/zillabyte/command.rb,
lib/zillabyte/cli/base.rb
Defined Under Namespace
Classes: Apps, Auth, Base, CommandFailed, Components, Data, Download, Flows, Help, Nuke, RPC, Repl, Version
Constant Summary
collapse
- BaseWithApp =
Base
Class Method Summary
collapse
Class Method Details
.command_aliases ⇒ Object
35
36
37
|
# File 'lib/zillabyte/command.rb', line 35
def self.command_aliases
@@command_aliases ||= {}
end
|
.commands ⇒ Object
31
32
33
|
# File 'lib/zillabyte/command.rb', line 31
def self.commands
@@commands ||= {}
end
|
.current_args ⇒ Object
69
70
71
|
# File 'lib/zillabyte/command.rb', line 69
def self.current_args
@current_args
end
|
.current_command ⇒ Object
61
62
63
|
# File 'lib/zillabyte/command.rb', line 61
def self.current_command
@current_command
end
|
.current_command=(new_current_command) ⇒ Object
65
66
67
|
# File 'lib/zillabyte/command.rb', line 65
def self.current_command=(new_current_command)
@current_command = new_current_command
end
|
.current_options ⇒ Object
73
74
75
|
# File 'lib/zillabyte/command.rb', line 73
def self.current_options
@current_options ||= {}
end
|
.display_warnings ⇒ Object
111
112
113
114
115
|
# File 'lib/zillabyte/command.rb', line 111
def self.display_warnings
unless warnings.empty?
$stderr.puts(warnings.map {|warning| " ! #{warning}"}.join("\n"))
end
end
|
311
312
313
314
|
# File 'lib/zillabyte/command.rb', line 311
def self.(body, options={})
default_error = block_given? ? yield : "Internal server error.\nRun `zillabyte status` to check for known platform issues."
parse_error_xml(body) || parse_error_json(body) || parse_error_plain(body) || default_error
end
|
.files ⇒ Object
39
40
41
|
# File 'lib/zillabyte/command.rb', line 39
def self.files
@@files ||= Hash.new {|hash,key| hash[key] = File.readlines(key).map {|line| line.strip}}
end
|
.global_option(name, *args, &blk) ⇒ Object
117
118
119
120
|
# File 'lib/zillabyte/command.rb', line 117
def self.global_option(name, *args, &blk)
global_options << { :name => name.to_s, :args => args.sort.reverse, :proc => blk }
end
|
.global_options ⇒ Object
77
78
79
|
# File 'lib/zillabyte/command.rb', line 77
def self.global_options
@global_options ||= []
end
|
.invalid_arguments ⇒ Object
81
82
83
|
# File 'lib/zillabyte/command.rb', line 81
def self.invalid_arguments
@invalid_arguments
end
|
.load(command) ⇒ Object
command should be non-nil
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/zillabyte/command.rb', line 6
def self.load(command)
base_dir = File.join(File.dirname(__FILE__), "cli")
command_file = "#{command[/^([^:]*):?/, 1]}.rb"
if File.fnmatch?("*.rb", command_file, File::FNM_PATHNAME)
begin
require(File.join(base_dir, command_file))
return
rescue LoadError
end
end
Dir[File.join(base_dir, "*.rb")].each do |file|
require(file)
end
ensure
unregister_commands_made_private_after_the_fact
end
|
.namespaces ⇒ Object
43
44
45
|
# File 'lib/zillabyte/command.rb', line 43
def self.namespaces
@@namespaces ||= {}
end
|
.parse(cmd) ⇒ Object
307
308
309
|
# File 'lib/zillabyte/command.rb', line 307
def self.parse(cmd)
commands[cmd] || commands[command_aliases[cmd]]
end
|
.parse_error_json(body) ⇒ Object
323
324
325
326
327
328
329
330
331
332
333
|
# File 'lib/zillabyte/command.rb', line 323
def self.parse_error_json(body)
json = json_decode(body.to_s) rescue false
case json
when Array
json.first.join(' ') when Hash
json['error'] else
nil
end
end
|
.parse_error_plain(body) ⇒ Object
335
336
337
338
|
# File 'lib/zillabyte/command.rb', line 335
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
316
317
318
319
320
321
|
# File 'lib/zillabyte/command.rb', line 316
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
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
176
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
|
# File 'lib/zillabyte/command.rb', line 127
def self.prepare_run(cmd, args=[])
command = parse(cmd)
if args.include?('-h') || args.include?('--help')
args.unshift(cmd) unless cmd =~ /^-.*/
self.load("help")
cmd = 'help'
command = parse(cmd)
end
if cmd == '--version' or cmd == '-version'
self.load("version")
cmd = 'version'
command = parse(cmd)
end
@current_command = cmd
@anonymized_args, @normalized_args = [], []
opts = {}
invalid_options = []
require("optparse")
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)
if invalid_options.include?("--json")
invalid_options.delete("--json")
opts[:output_type] = "json"
end
@current_args = args
@current_options = opts
@invalid_arguments = invalid_options
@anonymous_command = [ARGV.first, *@anonymized_args].join(' ')
begin
require("fileutils")
usage_directory = "#{home_directory}/.zillabyte/usage"
FileUtils.mkdir_p(usage_directory)
usage_file = usage_directory << "/#{Zillabyte::CLI::VERSION}"
usage = if File.exists?(usage_file)
json_decode(File.read(usage_file))
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 zillabyte command.",
"See `zillabyte help` for a list of available commands."
].compact.join("\n"))
end
end
|
.register_command(command) ⇒ Object
47
48
49
|
# File 'lib/zillabyte/command.rb', line 47
def self.register_command(command)
commands[command[:command]] = command
end
|
.register_namespace(namespace) ⇒ Object
57
58
59
|
# File 'lib/zillabyte/command.rb', line 57
def self.register_namespace(namespace)
namespaces[namespace[:name]] = namespace
end
|
.run(cmd, arguments = []) ⇒ Object
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# File 'lib/zillabyte/command.rb', line 283
def self.run(cmd, arguments=[])
require("excon/errors")
require("zillabyte/helpers")
extend(Zillabyte::Helpers)
begin
object, method = prepare_run(cmd, arguments.dup)
object.send(method)
rescue Excon::Errors::SocketError => e
if ENV['BUBBLE_EXCEPTIONS']
raise e
else
error "remote server error: #{e.message}"
end
rescue Errno::ECONNREFUSED => e
if ENV['BUBBLE_EXCEPTIONS']
raise e
else
error "remote server error: #{e.message}"
end
end
end
|
.shift_argument ⇒ Object
85
86
87
88
|
# File 'lib/zillabyte/command.rb', line 85
def self.shift_argument
@invalid_arguments.shift.dup rescue nil
end
|
.tracker(cmd, arguments = []) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
# File 'lib/zillabyte/command.rb', line 226
def self.tracker(cmd,arguments=[])
options = {}
require("netrc")
default_path = Netrc.default_path
if File.exists?(default_path)
encrypted = default_path + ".gpg"
if File.exists?(encrypted)
default_path = encrypted
end
if Netrc.read(Netrc.default_path)["api.zillabyte.com"]
options[:user_id] = Netrc.read(Netrc.default_path)["api.zillabyte.com"][1]
options[:email] = Netrc.read(Netrc.default_path)["api.zillabyte.com"][0]
end
end
location = "#{Dir.home}/.zillabyte/.install_id"
unless File.file?(location)
require "securerandom"
uuid_home = "#{Dir.home}/.zillabyte/"
FileUtils.mkdir_p(uuid_home)
File.open(location,'w') { |f| f.write(SecureRandom.uuid)}
end
options[:install_id] = File.read(location)
options[:cli_command] = cmd
options[:date] = Time.now
options[:args] = arguments.to_s
begin
require 'excon'
Excon.new(
"http://tracking.zillabyte.com",
:headers => {
'Content-Type' => "application/json",
}
).request(
:expects => 200,
:method => :post,
:path => "/track",
:body => options.to_json
)
rescue Exception => e
end
end
|
.unregister_commands_made_private_after_the_fact ⇒ Object
51
52
53
54
55
|
# File 'lib/zillabyte/command.rb', line 51
def self.unregister_commands_made_private_after_the_fact
commands.values \
.select { |c| c[:klass].private_method_defined? c[:method] } \
.each { |c| commands.delete c[:command] }
end
|
.validate_arguments! ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/zillabyte/command.rb', line 90
def self.validate_arguments!
unless invalid_arguments.empty?
arguments = invalid_arguments.map {|arg| "\"#{arg}\""}
if arguments.length == 1
message = "Invalid argument: #{arguments.first}"
elsif arguments.length > 1
message = "Invalid arguments: "
message << arguments[0...-1].join(", ")
message << " and "
message << arguments[-1]
end
$stderr.puts(format_with_bang(message))
run(current_command, ["--help"])
exit(1)
end
end
|
.warnings ⇒ Object
107
108
109
|
# File 'lib/zillabyte/command.rb', line 107
def self.warnings
@warnings ||= []
end
|