Module: Pebbles::Command
- Extended by:
- Helpers
- Defined in:
- lib/pebbles/command.rb,
lib/pebbles/command/base.rb
Defined Under Namespace
Classes: Apps, Auth, Base, CommandFailed, Config, Help
Constant Summary
collapse
- BaseWithApp =
Base
Class Method Summary
collapse
Methods included from Helpers
action, ask, confirm_command, create_git_remote, debug, debugging?, display, error, error_with_failure, error_with_failure=, flatten_hash, format_bytes, format_error, format_with_bang, git, has_git?, has_git_remote?, has_http_git_entry_in_netrc, home_directory, hputs, json_decode, launchy, line_formatter, longest, output_with_bang, running_on_a_mac?, running_on_windows?, styled_array, styled_error, styled_hash, styled_header, update_git_remote, with_tty
Class Method Details
.command_aliases ⇒ Object
22
23
24
|
# File 'lib/pebbles/command.rb', line 22
def self.command_aliases
@@command_aliases ||= {}
end
|
.commands ⇒ Object
18
19
20
|
# File 'lib/pebbles/command.rb', line 18
def self.commands
@@commands ||= {}
end
|
.current_args ⇒ Object
56
57
58
|
# File 'lib/pebbles/command.rb', line 56
def self.current_args
@current_args
end
|
.current_command ⇒ Object
48
49
50
|
# File 'lib/pebbles/command.rb', line 48
def self.current_command
@current_command
end
|
.current_command=(new_current_command) ⇒ Object
52
53
54
|
# File 'lib/pebbles/command.rb', line 52
def self.current_command=(new_current_command)
@current_command = new_current_command
end
|
.current_options ⇒ Object
60
61
62
|
# File 'lib/pebbles/command.rb', line 60
def self.current_options
@current_options ||= {}
end
|
.display_warnings ⇒ Object
98
99
100
101
102
|
# File 'lib/pebbles/command.rb', line 98
def self.display_warnings
unless warnings.empty?
$stderr.puts(warnings.uniq.map {|warning| " ! #{warning}"}.join("\n"))
end
end
|
234
235
236
237
|
# File 'lib/pebbles/command.rb', line 234
def self.(body, options={})
default_error = block_given? ? yield : "Internal server error."
parse_error_json(body) || parse_error_plain(body) || default_error
end
|
.files ⇒ Object
26
27
28
|
# File 'lib/pebbles/command.rb', line 26
def self.files
@@files ||= Hash.new {|hash,key| hash[key] = File.readlines(key).map {|line| line.strip}}
end
|
.global_option(name, *args, &blk) ⇒ Object
104
105
106
107
|
# File 'lib/pebbles/command.rb', line 104
def self.global_option(name, *args, &blk)
global_options << { :name => name.to_s, :args => args.sort.reverse, :proc => blk }
end
|
.global_options ⇒ Object
64
65
66
|
# File 'lib/pebbles/command.rb', line 64
def self.global_options
@global_options ||= []
end
|
.handle_auth_error(e) ⇒ Object
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/pebbles/command.rb', line 219
def self.handle_auth_error(e)
if ENV['PEBBLES_API_KEY']
puts "Authentication failure with PEBBLES_API_KEY"
exit 1
else
puts "Authentication failure"
run "login"
true
end
end
|
.invalid_arguments ⇒ Object
68
69
70
|
# File 'lib/pebbles/command.rb', line 68
def self.invalid_arguments
@invalid_arguments
end
|
.load ⇒ Object
11
12
13
14
15
16
|
# File 'lib/pebbles/command.rb', line 11
def self.load
Dir[File.join(File.dirname(__FILE__), "command", "*.rb")].each do |file|
require file
end
unregister_commands_made_private_after_the_fact
end
|
.namespaces ⇒ Object
30
31
32
|
# File 'lib/pebbles/command.rb', line 30
def self.namespaces
@@namespaces ||= {}
end
|
.parse(cmd) ⇒ Object
230
231
232
|
# File 'lib/pebbles/command.rb', line 230
def self.parse(cmd)
commands[cmd] || commands[command_aliases[cmd]]
end
|
.parse_error_json(body) ⇒ Object
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/pebbles/command.rb', line 239
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'] || json['error_message'] || json['message'] else
nil
end
end
|
.parse_error_plain(body) ⇒ Object
251
252
253
254
|
# File 'lib/pebbles/command.rb', line 251
def self.parse_error_plain(body)
return unless body.respond_to?(:headers) && body.[:content_type].to_s.include?("text/plain")
body.to_s
end
|
.prepare_run(cmd, args = []) ⇒ Object
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
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/pebbles/command.rb', line 117
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
@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('|')})/
@normalized_args << "#{option[:args].last.split(' ', 2).first} _"
end
end
end
begin
parser.order!(args) do |nonopt|
invalid_options << nonopt
@normalized_args << '!'
end
rescue OptionParser::InvalidOption => ex
invalid_options << ex.args.first
@normalized_args << '!'
retry
end
args.concat(invalid_options)
@current_args = args
@current_options = opts
@invalid_arguments = invalid_options
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 pebbles command.",
"See `pebbles help` for a list of available commands."
].compact.join("\n"))
end
end
|
.register_command(command) ⇒ Object
34
35
36
|
# File 'lib/pebbles/command.rb', line 34
def self.register_command(command)
commands[command[:command]] = command
end
|
.register_namespace(namespace) ⇒ Object
44
45
46
|
# File 'lib/pebbles/command.rb', line 44
def self.register_namespace(namespace)
namespaces[namespace[:name]] = namespace
end
|
.run(cmd, arguments = []) ⇒ Object
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
|
# File 'lib/pebbles/command.rb', line 187
def self.run(cmd, arguments=[])
object, method = prepare_run(cmd, arguments.dup)
object.send(method)
rescue Pebbles::API::Errors::Unauthorized => e
retry_login = handle_auth_error(e)
retry if retry_login
rescue Pebbles::API::Errors::NotFound => e
error (e.response.body) {
e.response.body =~ /^([\w\s]+ not found).?$/ ? $1 : "Resource not found"
}
rescue Pebbles::API::Errors::Locked => e
app = e.response.[:x_confirmation_required]
if confirm_command(app, (e.response.body))
arguments << '--confirm' << app
retry
end
rescue Pebbles::API::Errors::Timeout
error "API request timed out. Please try again."
rescue Pebbles::API::Errors::Forbidden => e
error (e.response.body)
rescue Pebbles::API::Errors::ErrorWithResponse => e
error (e.response.body)
rescue CommandFailed => e
error e.message, false
rescue OptionParser::ParseError
commands[cmd] ? run("help", [cmd]) : run("help")
rescue Excon::Errors::SocketError, SocketError => e
error("Unable to connect to Pebblescape API, please check internet connectivity and try again.")
ensure
display_warnings
end
|
.shift_argument ⇒ Object
72
73
74
75
|
# File 'lib/pebbles/command.rb', line 72
def self.shift_argument
@invalid_arguments.shift.dup rescue nil
end
|
.unregister_commands_made_private_after_the_fact ⇒ Object
38
39
40
41
42
|
# File 'lib/pebbles/command.rb', line 38
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/pebbles/command.rb', line 77
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
94
95
96
|
# File 'lib/pebbles/command.rb', line 94
def self.warnings
@warnings ||= []
end
|