Sha256: ccfc194ee0fd80d3a8e64ae2c93a0bf7ef52c3b3a95c08ace708bb57edc36171

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

require 'rubygems'
require 'mixlib/cli'
require 'rest_client'
require 'json'

class Veslo
  include Mixlib::CLI
  SUPPORTED_METHODS = ["put", "get"]
  SUPPORTED_RESOURCES = ["configurations"]

  option :server_url,
    :short => "-s SERVER",
    :long  => "--server SERVER",
    :description => "The Noah server to work against"

  def run!(*arguments)
    argv = parse_options(arguments)
    @server = RestClient::Resource.new(config[:server_url], :headers => {:accept => "application/octet"})
    parse_commands(argv)
    send(:"resource_#{@method}")
  end

  def parse_commands(commands)
    raise ArgumentError.new("Not the right ammount of arguments") unless (3..4).include?(commands.size)
    @resource = commands.shift
    @method = commands.shift
    @name = commands.shift
    @file = commands.shift
    validate_input
  end

  def validate_input
    raise NotImplementedError.new("method #{@method} not supported") unless SUPPORTED_METHODS.include?(@method)
    raise NotImplementedError.new("resource #{@resource} not supported") unless SUPPORTED_RESOURCES.include?(@resource)
  end

  def resource_get
    result = @server["#{@resource}/#{@name}"].get
    $stdout.puts result.to_str
    return 0
  rescue RestClient::ExceptionWithResponse => e
    case e.response.code
    when 404
      $stderr.puts("Requested resource not found")
      return 1
    else
      $stderr.puts("Request failed with status: #{e.response.code}")
      return 2
    end
  end

  def resource_put
    raise NotImplementedError, "No STDIN yet" unless @file
    file_content = File.open(@file, 'r').read
    put_data = "{\"format\":\"app/octet\", \"body\":#{file_content.to_json}}"
    result = @server["#{@resource}/#{@name}"].put(put_data)
    $stdout.puts "Config uploaded"
    return 0
  rescue Errno::ENOENT
    $stderr.puts "File not found: #{@file}"
    return 3
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
veslo-0.1.0 lib/veslo.rb
veslo-0.0.5 lib/veslo.rb
veslo-0.0.4 lib/veslo.rb