Skip to content

Commit

Permalink
Merge branch 'release-0.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dchandekstark committed Jul 27, 2017
2 parents 94cd33e + c88c078 commit 70b5ed0
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 62 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ D, [2016-04-29T12:15:12.593075 #28275] DEBUG -- : Duracloud::Client HEAD https:/

A `Duracloud::NotFoundError` exception is raised if the space does not exist.

NOTE: When the object count in a space exceeds 1000, Duracloud returns "1000+" as the count. Ruby's integer coercion `to_i`
turns that string into the integer 1000. Getting an exact count above 1000 requires (on the client side) enumerating the content_ids
(below, fixed in v0.7.2 when count is >= 1000) which can take a long time for a space with a lot of content items,
since a maxiumum of 1000 ids can be retrived at one time. If an up-to-the-minute
count is not required, the storage report for the space (not yet implemented in this library) shows an exact count on a daily basis.

#### Enumerate the content IDs of the space

```
Expand Down Expand Up @@ -322,7 +328,8 @@ D, [2016-05-19T15:39:33.538448 #29974] DEBUG -- : Duracloud::Client GET https://

*New in version 0.6.0*

The `bin/` directory of the gem now includes an executable `duracloud`. Use `-h/--help` to display usage. If the gem was installed with `bundler` you may need to run `bundle exec bin/duracloud`.
The `bin/` directory of the gem now includes an executable `duracloud`. Use `-h/--help` to display usage.
If the gem was installed with `bundler` you may need to run `bundle exec bin/duracloud`.

## Versioning

Expand Down
2 changes: 1 addition & 1 deletion bin/duracloud
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

require 'duracloud'

Duracloud::Command.call(*ARGV)
Duracloud::CLI.call(*ARGV)
3 changes: 1 addition & 2 deletions lib/duracloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ module Duracloud
autoload :BitIntegrityReport, "duracloud/bit_integrity_report"
autoload :ChunkedContent, "duracloud/chunked_content"
autoload :Client, "duracloud/client"
autoload :Command, "duracloud/command"
autoload :Commands, "duracloud/commands"
autoload :CLI, "duracloud/cli"
autoload :Configuration, "duracloud/configuration"
autoload :Connection, "duracloud/connection"
autoload :Content, "duracloud/content"
Expand Down
77 changes: 55 additions & 22 deletions lib/duracloud/command.rb → lib/duracloud/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@
require 'active_model'

module Duracloud
class Command
class CLI
include ActiveModel::Model
include Commands

COMMANDS = Commands.public_instance_methods.map(&:to_s)
USAGE = "Usage: duracloud [#{COMMANDS.join('|')}] [options]"
HELP = "Type 'duracloud --help' for usage."
COMMANDS = %w( sync validate manifest properties )

USAGE = <<-EOS
Usage: duracloud [COMMAND] [options]
Commands:
#{COMMANDS.sort.join("\n ")}
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,
:content_dir, :format, :infile,
:logging

def self.error!(reason)
STDERR.puts reason
STDERR.puts HELP
validates_presence_of :space_id, message: "-s/--space-id option is required."

def self.error!(exception)
$stderr.puts exception.message
if [ CommandError, OptionParser::ParseError ].include?(exception.class)
$stderr.puts HELP
end
exit(false)
end

Expand Down Expand Up @@ -88,27 +99,47 @@ def self.call(*args)
"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)

new(options).execute(command)
rescue CommandError, OptionParser::ParseError => e
error!(e.message)
cli = new(options)
if cli.invalid?
message = cli.errors.map { |k, v| "ERROR: #{v}" }.join("\n")
raise CommandError, message
end
cli.execute(command)
rescue => e
error!(e)
end

def execute(command)
unless COMMANDS.include?(command)
raise CommandError, "Invalid command: #{command}."
end
begin
configure_client
send(command)
rescue Error => e
STDERR.puts e.message
exit(false)
end
configure_client
send(command).call(self)
end

protected

def sync
Commands::Sync
end

def validate
Commands::Validate
end

def manifest
Commands::DownloadManifest
end

def properties
Commands::GetProperties
end

private
Expand All @@ -126,3 +157,5 @@ def configure_client

end
end

Dir[File.expand_path("../commands/*.rb", __FILE__)].each { |m| require m }
35 changes: 0 additions & 35 deletions lib/duracloud/commands.rb

This file was deleted.

11 changes: 11 additions & 0 deletions lib/duracloud/commands/command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'delegate'

module Duracloud::Commands
class Command < SimpleDelegator

def self.call(command)
new(command).call
end

end
end
13 changes: 13 additions & 0 deletions lib/duracloud/commands/download_manifest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative "command"

module Duracloud::Commands
class DownloadManifest < Command

def call
Duracloud::Manifest.download(space_id, store_id, format: format) do |chunk|
print chunk
end
end

end
end
27 changes: 27 additions & 0 deletions lib/duracloud/commands/get_properties.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require_relative "command"

module Duracloud::Commands
class GetProperties < Command

def call
proplist = content_id ? content_properties : space_properties
puts proplist
end

private

def content_properties
content = Duracloud::Content.find(space_id: space_id, store_id: store_id, content_id: content_id, md5: md5)
proplist = content.properties.map { |k, v| "#{k}: #{v}" }
proplist << "MD5: #{content.md5}"
proplist << "Size: #{content.size} (#{content.human_size})"
proplist << "Chunked?: #{content.chunked?}"
end

def space_properties
space = Duracloud::Space.find(space_id, store_id)
space.properties.map { |k, v| "#{k}: #{v}" }
end

end
end
18 changes: 18 additions & 0 deletions lib/duracloud/commands/sync.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require_relative 'command'

module Duracloud::Commands
class Sync < Command

def call
if infile
File.open(infile, "rb") do |f|
self.content_id ||= infile # XXX relativize to cwd?
Duracloud::Content.create(space_id: space_id, store_id: store_id, content_id: content_id, md5: md5, body: f)
end
else
Duracloud::Content.create(space_id: space_id, store_id: store_id, content_id: content_id, md5: md5, body: $stdin)
end
end

end
end
11 changes: 11 additions & 0 deletions lib/duracloud/commands/validate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative "command"

module Duracloud::Commands
class Validate < Command

def call
Duracloud::SyncValidation.call(space_id: space_id, store_id: store_id, content_dir: content_dir)
end

end
end
2 changes: 1 addition & 1 deletion lib/duracloud/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Duracloud
VERSION = "0.7.2"
VERSION = "0.8.0"
end
43 changes: 43 additions & 0 deletions spec/unit/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Duracloud
RSpec.describe CLI do

subject { described_class.new(**opts) }

describe "properties" do
let(:opts) { {space_id: "foo", content_id: "bar"} }
let(:command) { "properties" }
specify {
expect(Commands::GetProperties).to receive(:call).with(subject) { nil }
subject.execute(command)
}
end

describe "sync" do
let(:opts) { {space_id: "foo", content_id: "bar", infile: "foo/bar"} }
let(:command) { "sync" }
specify {
expect(Commands::Sync).to receive(:call).with(subject) { nil }
subject.execute(command)
}
end

describe "validate" do
let(:opts) { {space_id: "foo", content_dir: "/tmp"} }
let(:command) { "validate" }
specify {
expect(Commands::Validate).to receive(:call).with(subject) { nil }
subject.execute(command)
}
end

describe "manifest" do
let(:opts) { {space_id: "foo"} }
let(:command) { "manifest" }
specify {
expect(Commands::DownloadManifest).to receive(:call).with(subject) { nil }
subject.execute(command)
}
end

end
end

0 comments on commit 70b5ed0

Please sign in to comment.