require 'trollop' module RightPublish module CLI REPOSITORY_TYPES = RepoManager.repo_types() SUB_COMMANDS = %w(publish pull add annotate push) def self.run() options = parse_args begin profile = RightPublish::Profile.instance profile.load(options[:profile] || options[:repo_type]) rescue LoadError => e puts e exit -1 end # These options override profile attributes. profile.settings[:local_storage][:cache_dir] = options[:local_cache] if options[:local_cache] profile.settings[:verbose] = true if options[:verbose] # We already know this is a valid type, parse_args checked repo = RepoManager.get_repository(REPOSITORY_TYPES[options[:repo_type]]) begin case options[:cmd] when 'publish' repo.publish(options[:files], options[:dist]) when 'pull' repo.pull when 'add' repo.add(options[:files], options[:dist]) when 'annotate' repo.annotate when 'push' repo.push else raise ArgumentError, "Unknown command '#{options[:cmd]}'" end rescue RuntimeError => e RightPublish::Profile.log("Fatal Error:\n\t#{e}", :error) exit -1 end exit 0 end def self.parse_args() options = Trollop.options do version "RightPublish (c) 2013 RightScale Inc" banner <<-EOS RightPublish can manage a YUM/APT/RubyGem repository in remote storage, e.g. S3. Usage: right_publish [options] [file1, file2, ...] basic commands: publish advanced commands: pull add annotate push options: EOS opt :local_cache, "Local cache location", :type => String opt :profile, "Publish profile", :type => String, :default => nil opt :repo_type, "Repository type: #{REPOSITORY_TYPES.keys.inspect}", :type => String opt :dist, "Target distribution. Required for binary packages. If unspecified for noarch and source packages, will copy to all distributions specified in profile.", :type => String opt :verbose, "Verbose output" end options[:cmd]= ARGV.shift options[:files] = expand_argv_globs Trollop.die "invalid repository type: #{options[:repo_type]}" unless REPOSITORY_TYPES[options[:repo_type]] options end def self.expand_argv_globs files = [] ARGV.each do |glob| files += Dir.glob(glob) end files end end end