Module: JustOneDB::Command

Defined in:
lib/justonedb/command.rb,
lib/justonedb/command/heroku/base.rb,
lib/justonedb/command/heroku/migrate.rb,
lib/justonedb/command/heroku/promote.rb,
lib/justonedb/command/heroku/purge-old.rb,
lib/justonedb/command/heroku/make-default.rb,
lib/justonedb/command/heroku/reset-default.rb

Defined Under Namespace

Classes: Base, Heroku, InvalidAppConfig, InvalidCommand, InvalidPlatform

Class Method Summary collapse

Class Method Details

.commandsObject



18
19
20
# File 'lib/justonedb/command.rb', line 18

def commands
	@commands ||= JustOneDB::Command::Base.commands
end

.helpObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/justonedb/command.rb', line 43

def help
	prefix = "Usage: " + File.basename($PROGRAM_NAME)
	puts "#{prefix} <platform> <command>"

	main_options.each do |option|
		puts " " * prefix.length + "[ #{option[:short]} #{option[:long]} (#{option[:description]}) ]"
	end

	# Show all platforms and commands
	puts "\nPlatforms and Commands:"

	commands.each do |platform, command_hash|
		puts " " * prefix.length + "#{platform}"
		indent = prefix.length + platform.length

		max_length = 0
		command_hash.each_key { |command| max_length = command.length if command.length > max_length }

		command_hash.each do |command, cmd_details|
			printf("%*s%-*s - %s\n", indent, " ", max_length, command, cmd_details[:summary])
			#puts " " * indent + "#{command}"
		end
	end
end

.loadObject



12
13
14
15
16
# File 'lib/justonedb/command.rb', line 12

def load
	require_all File.join(File.dirname(__FILE__), "command", '*.rb')

	JustOneDB::Command::Base.load
end

.main_optionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/justonedb/command.rb', line 26

def main_options
	@main_options ||=	[
							{
								:option_id => "app",
								:short => '-a',
								:long => '--app APP',
								:description => 'Heroku application'
							},
							{
								:option_id => "help",
								:short => '-h',
								:long => '--help',
								:description => 'Help'
							}
						]
end

.platformsObject



22
23
24
# File 'lib/justonedb/command.rb', line 22

def platforms
	@platforms ||= JustOneDB::Command::Base.platforms
end

.run(platform, command, args) ⇒ Object

Raises:



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
# File 'lib/justonedb/command.rb', line 68

def run(platform, command, args)
	# Check the platform
	raise InvalidPlatform unless platforms.include?(platform)
	
	# See if the command exists
	raise InvalidCommand unless commands[platform] and commands[platform][command]

	cmd_details = commands[platform][command]
	options = {}

	# Check any options available
	optparse = OptionParser.new do |parser|
		main_options.each do |option|
			parser.on("#{option[:short]}", "#{option[:long]}", option[:description]) do |value|
				options[option[:option_id].to_sym] = value
			end
		end

		if cmd_details[:options]
			cmd_details[:options].each do |option|
				parser.on("#{option[:short]}", "#{option[:long]}", option[:description]) do |value|
					options[option[:option_id].to_sym] = value
				end
			end
		end
	end

	begin
		optparse.parse!(args)
		
		rescue OptionParser::ParseError
			prefix = "Usage: #{$PROGRAM_NAME} "
			puts "#{prefix} <platform> <command>"

			main_options.each do |option|
				puts " " * prefix.length + "[ #{option[:short]} #{option[:long]} (#{option[:description]}) ]"
			end

			cmd_details[:options].each do |option|
				puts " " * prefix.length + "[ #{option[:short]} #{option[:long]} (#{option[:description]}) ]"
			end
	end
	
	cmd = cmd_details[:cmd_class].new
	bound = cmd_details[:method].bind(cmd)
	bound.call(args, options)
end