#!/usr/bin/env ruby # -*- ruby -*- $: << File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")) require 'pyrosoma/http' require 'optparse' require 'yaml' server = "default" opts = OptionParser.new("", 24, ' ') { |opts| opts.banner = "Usage: pyroctl [-s server] (get|post|list|query)" opts.separator "Options:" opts.on('-s', '--server URI', String, "Connect to the pyrosoma instance at URI") { |s| server = s } opts.separator "" opts.separator "Common options:" opts.on_tail("-h", "--help", "Show this message") do puts opts exit end opts.on_tail("--version", "Show version") do require 'pyrosoma' puts "pyroctl #{Pyrosoma::VERSION}" exit end opts.parse! ARGV } begin map = YAML.load_file(File.expand_path("~/.pyroctl")) if map[server] server = map[server] end rescue Errno::ENOENT end client = Pyrosoma::HTTP.new(server) begin case ARGV.first when /get/i puts client.get(ARGV[1]) when /post/i ARGV.pop doc = ARGF.read puts client.post(doc) when /list/i puts client.ids when /query/i puts client.search_each(ARGV[1]) else abort opts.to_s end rescue Pyrosoma::HTTP::ServerError => ex abort "#{ex.status}: #{ex.reply}" end