Module: Heroku::Command

Defined in:
lib/heroku/command.rb,
lib/heroku/commands/db.rb,
lib/heroku/commands/ps.rb,
lib/heroku/commands/app.rb,
lib/heroku/commands/ssl.rb,
lib/heroku/commands/auth.rb,
lib/heroku/commands/base.rb,
lib/heroku/commands/help.rb,
lib/heroku/commands/keys.rb,
lib/heroku/commands/logs.rb,
lib/heroku/commands/addons.rb,
lib/heroku/commands/config.rb,
lib/heroku/commands/account.rb,
lib/heroku/commands/bundles.rb,
lib/heroku/commands/domains.rb,
lib/heroku/commands/plugins.rb,
lib/heroku/commands/service.rb,
lib/heroku/commands/sharing.rb,
lib/heroku/commands/version.rb,
lib/heroku/commands/maintenance.rb

Defined Under Namespace

Classes: Account, Addons, App, Auth, Base, BaseWithApp, Bundles, CommandFailed, Config, Db, Domains, Help, InvalidCommand, Keys, Logs, Maintenance, Plugins, Ps, Service, Sharing, Ssl, Version

Class Method Summary collapse

Class Method Details

.error(msg) ⇒ Object



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

def error(msg)
	STDERR.puts(msg)
	exit 1
end

.extract_error(body) ⇒ Object



77
78
79
80
# File 'lib/heroku/command.rb', line 77

def extract_error(body)
	msg = parse_error_xml(body) || parse_error_json(body) || 'Internal server error'
	msg.split("\n").map { |line| ' !   ' + line }.join("\n")
end

.extract_not_found(body) ⇒ Object



73
74
75
# File 'lib/heroku/command.rb', line 73

def extract_not_found(body)
	body =~ /^[\w\s]+ not found$/ ? body : "Resource not found"
end

.parse(command) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/heroku/command.rb', line 53

def parse(command)
	parts = command.split(':')
	case parts.size
		when 1
			begin
				return eval("Heroku::Command::#{command.capitalize}"), :index
			rescue NameError, NoMethodError
				return Heroku::Command::App, command
			end
		when 2
			begin
				return Heroku::Command.const_get(parts[0].capitalize), parts[1]
			rescue NameError
				raise InvalidCommand
			end
		else
			raise InvalidCommand
	end
end

.parse_error_json(body) ⇒ Object



89
90
91
92
93
# File 'lib/heroku/command.rb', line 89

def parse_error_json(body)
	json = JSON.parse(body)
	json['error']
rescue JSON::ParserError
end

.parse_error_xml(body) ⇒ Object



82
83
84
85
86
87
# File 'lib/heroku/command.rb', line 82

def 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

.run(command, args, retries = 0) ⇒ Object



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
39
# File 'lib/heroku/command.rb', line 13

def run(command, args, retries=0)
	Heroku::Plugin.load!
	begin
		run_internal 'auth:reauthorize', args.dup if retries > 0
		run_internal(command, args.dup)
	rescue InvalidCommand
		error "Unknown command. Run 'heroku help' for usage information."
	rescue RestClient::Unauthorized
		if retries < 3
			STDERR.puts "Authentication failure"
			run(command, args, retries+1)
		else
			error "Authentication failure"
		end
	rescue RestClient::ResourceNotFound => e
		error extract_not_found(e.http_body)
	rescue RestClient::RequestFailed => e
		error extract_error(e.http_body) unless e.http_code == 402
		retry if run_internal('account:confirm_billing', args.dup)
	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 Interrupt => e
		error "\n[canceled]"
	end
end

.run_internal(command, args, heroku = nil) ⇒ Object

Raises:



41
42
43
44
45
46
# File 'lib/heroku/command.rb', line 41

def run_internal(command, args, heroku=nil)
	klass, method = parse(command)
	runner = klass.new(args, heroku)
	raise InvalidCommand unless runner.respond_to?(method)
	runner.send(method)
end