lib/duracloud/cli.rb in duracloud-client-0.8.0 vs lib/duracloud/cli.rb in duracloud-client-0.9.0
- old
+ new
@@ -2,12 +2,13 @@
require 'active_model'
module Duracloud
class CLI
include ActiveModel::Model
+ include Commands
- COMMANDS = %w( sync validate manifest properties )
+ COMMANDS = Commands.public_instance_methods.map(&:to_s)
USAGE = <<-EOS
Usage: duracloud [COMMAND] [options]
Commands:
@@ -15,135 +16,58 @@
Options:
EOS
HELP = "Type 'duracloud -h/--help' for usage."
- attr_accessor :command, :user, :password, :host, :port,
- :space_id, :store_id, :content_id,
- :content_type, :md5,
- :content_dir, :format, :infile,
- :logging
+ 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
- validates_presence_of :space_id, message: "-s/--space-id option is required."
+ 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)
$stderr.puts HELP
end
exit(false)
end
def self.call(*args)
- options = {}
-
- parser = OptionParser.new do |opts|
- opts.banner = USAGE
-
- opts.on("-h", "--help",
- "Prints help") do
- puts opts
- exit
- end
-
- opts.on("-H", "--host HOST",
- "DuraCloud host") do |v|
- options[:host] = v
- end
-
- opts.on("-P", "--port PORT",
- "DuraCloud port") do |v|
- options[:port] = v
- end
-
- opts.on("-u", "--user USER",
- "DuraCloud user") do |v|
- options[:user] = v
- end
-
- opts.on("-p", "--password PASSWORD",
- "DuraCloud password") do |v|
- options[:password] = v
- end
-
- opts.on("-l", "--[no-]logging",
- "Enable/disable logging to STDERR") do |v|
- options[:logging] = v
- end
-
- opts.on("-s", "--space-id SPACE_ID",
- "DuraCloud space ID") do |v|
- options[:space_id] = v
- end
-
- opts.on("-i", "--store-id STORE_ID",
- "DuraCloud store ID") do |v|
- options[:store_id] = v
- end
-
- opts.on("-c", "--content-id CONTENT_ID",
- "DuraCloud content ID") do |v|
- options[:content_id] = v
- end
-
- opts.on("-m", "--md5 MD5",
- "MD5 digest of content to store or retrieve") do |v|
- options[:md5] = v
- end
-
- opts.on("-b", "--bagit",
- "Get manifest in BAGIT format (default is TSV)") do
- options[:format] = Manifest::BAGIT_FORMAT
- end
-
- opts.on("-d", "--content-dir CONTENT_DIR",
- "Local content directory") do |v|
- options[:content_dir] = v
- end
-
- opts.on("-f", "--infile FILE",
- "Input file") do |v|
- options[:infile] = v
- end
- end
-
- command = args.shift if COMMANDS.include?(args.first)
- parser.parse!(args)
-
- cli = new(options)
+ 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(command)
+ cli.execute
rescue => e
error!(e)
end
- def execute(command)
+ def execute
configure_client
- send(command).call(self)
+ send(command, self)
end
- protected
-
- def sync
- Commands::Sync
- end
-
- def validate
- Commands::Validate
- end
-
- def manifest
- Commands::DownloadManifest
- end
-
- def properties
- Commands::GetProperties
- end
-
private
def configure_client
Client.configure do |config|
config.user = user if user
@@ -155,7 +79,5 @@
end
end
end
end
-
-Dir[File.expand_path("../commands/*.rb", __FILE__)].each { |m| require m }