qwandry/lib/qwandry.rb

70 lines
1.8 KiB
Ruby
Raw Normal View History

#!/usr/bin/env ruby
require 'optparse'
2010-08-06 00:49:12 +00:00
# Informal Spec:
#
# A User may have multiple Repositories
# A Repositories contains Packages
#
# A User will search for a repository giving a name and optional version
# Each Repository will be scanned for matching Packages
# If only one Package matches, that Package will be opened
# If more than one Package matches, then the user will be prompted to pick one
# While any two Packages share the same name their parent dir is appended
2010-08-17 04:23:42 +00:00
# If no Repository matches, then qwandry will exit with a 404 (repo not found)
2010-08-06 00:49:12 +00:00
#
module Qwandry
2010-08-17 05:53:40 +00:00
autoload :Launcher, "qwandry/launcher"
autoload :Repository, "qwandry/repository"
autoload :FlatRepository, "qwandry/flat_repository"
autoload :Package, "qwandry/package"
end
2010-08-06 00:49:12 +00:00
if __FILE__ == $0
2010-08-17 05:53:40 +00:00
@qwandry = Qwandry::Launcher.new
load('~/.qwandry/repositories.rb') if File.exists?('~/.qwandry/repositories.rb')
2010-08-09 04:15:20 +00:00
2010-08-06 00:49:12 +00:00
opts = OptionParser.new do |opts|
opts.banner = "Usage: qwandry [options] name [version]"
opts.separator ""
2010-08-17 04:23:42 +00:00
2010-08-17 05:53:40 +00:00
opts.separator "Known Repositories: #{@qwandry.repositories.keys.join(", ")}"
2010-08-17 04:23:42 +00:00
opts.on("-e", "--editor EDITOR", "Use EDITOR to open the package") do |editor|
2010-08-17 05:53:40 +00:00
@qwandry.editor = editor
2010-08-09 04:15:20 +00:00
end
2010-08-17 04:23:42 +00:00
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
2010-08-06 00:49:12 +00:00
opts.parse! ARGV
if ARGV.length != 1
puts opts
2010-08-17 04:23:42 +00:00
exit(1)
2010-08-06 00:49:12 +00:00
end
name = ARGV.pop
2010-08-17 05:53:40 +00:00
packages = @qwandry.find(name)
package = nil
2010-08-17 04:06:58 +00:00
case packages.length
when 0
puts "No packages matched '#{name}'"
2010-08-17 04:23:42 +00:00
exit 404 # Package not found -- hehe, super lame.
2010-08-17 04:06:58 +00:00
when 1
package = packages.first
else
packages.each_with_index do |package, index|
puts "%3d. %s" % [index+1, package.name]
end
print ">> "
index = gets.to_i-1
package = packages[index]
2010-08-06 00:49:12 +00:00
end
2010-08-17 05:53:40 +00:00
@qwandry.launch(package) if package
2010-08-06 00:49:12 +00:00
end