lib/duracloud/cli.rb in duracloud-client-0.9.1 vs lib/duracloud/cli.rb in duracloud-client-0.10.0

- old
+ new

@@ -1,11 +1,10 @@ require 'optparse' -require 'active_model' +require 'hashie' module Duracloud - class CLI - include ActiveModel::Model + class CLI < Hashie::Dash include Commands COMMANDS = Commands.public_instance_methods.map(&:to_s) USAGE = <<-EOS @@ -16,68 +15,64 @@ Options: EOS HELP = "Type 'duracloud -h/--help' for usage." - attr_accessor :all_spaces, - :command, - :content_dir, - :content_id, - :content_type, - :fast, - :format, - :host, - :infile, - :logging, - :md5, - :missing, - :password, - :port, - :prefix, - :space_id, - :store_id, - :user, - :work_dir + property :all_spaces + property :command, required: true + property :content_dir + property :content_id + property :content_type + property :fast + property :format + property :host + property :infile + property :logging + property :md5 + property :missing + property :password + property :port + property :prefix + property :space_id, required: -> { command != "get_storage_report" }, message: "-s/--space-id option is required." + property :store_id + property :user + property :work_dir - validates_presence_of :space_id, message: "-s/--space-id option is required.", unless: "command == 'get_storage_report'" - validates_inclusion_of :command, in: COMMANDS - def self.error!(exception) $stderr.puts exception.message - if [ CommandError, OptionParser::ParseError ].include?(exception.class) + if [ ArgumentError, CommandError, OptionParser::ParseError ].include?(exception.class) $stderr.puts HELP end exit(false) end def self.call(*args) - options = CommandOptions.new(*args) - cli = new(options) # .merge(command: command)) - if cli.invalid? - message = cli.errors.map { |k, v| "ERROR: #{v}" }.join("\n") - raise CommandError, message - end - cli.execute + new(*args).execute rescue => e error!(e) end + def initialize(*args) + super CommandOptions.parse(*args) + end + def execute + unless COMMANDS.include?(command) + raise CommandError, "Invalid command: #{command.inspect}." + end configure_client send(command, self) end private def configure_client - Client.configure do |config| - config.user = user if user - config.password = password if password - config.host = host if host - config.port = port if port + Duracloud.user = user if user + Duracloud.password = password if password + Duracloud.host = host if host + Duracloud.port = port if port - config.silence_logging! unless logging - end + Duracloud.silence_logging! unless logging end end end