dcp 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 443a8aeede841ab51e1599ad08b8fb4e905de9cebc7b1b66caf135f1ef46864f
4
- data.tar.gz: 0c11715af33f809bad76c6c7d0959b14ed56756982a9d7007e13b07dc5ecfe86
3
+ metadata.gz: 8ae9bca889c34738a0c2ea78783c973b8d325280251def8ecd31c6fad8aaa16e
4
+ data.tar.gz: ee80f7c7f1eea995da97a767ed200ec40612c05b7d41d64500728cff0faab1b3
5
5
  SHA512:
6
- metadata.gz: 10afb67747d77d9a1fe3f49daa3c6913e31a80df837ae575264070b0631c1c2d62d9cb377721cfd84402369cb9df08bd074379a8540509c75e0417bc2176254d
7
- data.tar.gz: aaa4de559daf52fa455b14b7b8ea33234c6453708867d641d3818206a1e6d1ebd41ac5ba3115257ba4a5cf147eaf9425416a776619b7e86ccd356eaed704b9c0
6
+ metadata.gz: 9c629121e4db91bf98395c7b22a889c538be69d169194e252714ce6f470871b13c81865d8ff065ebbdc0011605c23fff6cd68be1f312feaf1cb80a5aebbcd632
7
+ data.tar.gz: ba481ad8eed77700f4701fb5aad056d9ad6d2696904c956d25e2bf43495fac4d1c08014b878a9916581eb84ea3724b8ad4d406e5a26c2e23352c4672b19f927e
data/Gemfile.lock CHANGED
@@ -1,13 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dcp (0.1.1)
4
+ dcp (0.2.0)
5
5
  dropbox_api
6
6
  pit
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
+ coderay (1.1.2)
11
12
  diff-lcs (1.3)
12
13
  dropbox_api (0.1.10)
13
14
  faraday (~> 0.9, ~> 0.8)
@@ -15,6 +16,7 @@ GEM
15
16
  faraday (0.12.2)
16
17
  multipart-post (>= 1.2, < 3)
17
18
  jwt (1.5.6)
19
+ method_source (0.9.0)
18
20
  multi_json (1.12.2)
19
21
  multi_xml (0.6.0)
20
22
  multipart-post (2.0.0)
@@ -25,6 +27,9 @@ GEM
25
27
  multi_xml (~> 0.5)
26
28
  rack (>= 1.2, < 3)
27
29
  pit (0.0.7)
30
+ pry (0.11.3)
31
+ coderay (~> 1.1.0)
32
+ method_source (~> 0.9.0)
28
33
  rack (2.0.3)
29
34
  rake (10.5.0)
30
35
  rspec (3.7.0)
@@ -47,6 +52,7 @@ PLATFORMS
47
52
  DEPENDENCIES
48
53
  bundler (~> 1.16.a)
49
54
  dcp!
55
+ pry
50
56
  rake (~> 10.0)
51
57
  rspec (~> 3.0)
52
58
 
data/dcp.gemspec CHANGED
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "bundler", "~> 1.16.a"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "pry"
29
30
  end
data/exe/dcp CHANGED
@@ -10,38 +10,68 @@ require 'optparse'
10
10
  require 'pit'
11
11
 
12
12
  module DCP
13
+ class MultiError < StandardError
14
+ attr_reader :messages
15
+ def initialize(messages)
16
+ @messages = messages
17
+ end
18
+ end
19
+
13
20
  class CLI
14
21
  def run
15
22
  opts, args = parse_options
16
23
  dst = args.pop
17
24
  begin
25
+ precheck(args, dst)
26
+ rescue MultiError => e
27
+ e.messages.each{|m| error m}
28
+ exit 1
29
+ rescue => e
30
+ error e.message
31
+ exit 1
32
+ end
33
+ begin
34
+ dst = dst.chop if dst =~ /\/\z/ # root directory => ''
18
35
  args.each do |src|
19
36
  open(src, 'r') do |r|
20
- open(dst, 'w') do |w|
21
- w.write(r.read(10_000_000))
37
+ d = directory?(dst) ? "#{dst}/#{File.basename(src)}" : dst
38
+ open(d, 'w') do |w|
39
+ while data = r.read(10_000_000) do
40
+ w.write(data)
41
+ print '.' unless opts[:quiet]
42
+ end
22
43
  end
44
+ puts unless opts[:quiet]
23
45
  end
24
46
  end
25
47
  rescue => e
26
- usage e.message
48
+ usage "#{e.message} (#{e.class})"
27
49
  end
28
50
  end
29
51
 
30
52
  private
53
+ def error(msg)
54
+ $stderr.puts "dcp: #{msg}"
55
+ end
56
+
31
57
  def parse_options(argv = ARGV)
32
58
  op = OptionParser.new
33
59
 
34
60
  self.class.module_eval do
35
61
  define_method(:usage) do |msg = nil|
36
62
  puts op.to_s
37
- puts "error: #{msg}" if msg
63
+ error msg if msg
38
64
  exit 1
39
65
  end
40
66
  end
41
67
 
42
68
  opts = {
69
+ quiet: false
43
70
  }
44
71
 
72
+ op.on('-q', '--quiet', "quiet mode") do
73
+ opts[:quiet] = true
74
+ end
45
75
  op.on('-h', '--help', "print this messages") do
46
76
  usage
47
77
  end
@@ -85,14 +115,53 @@ module DCP
85
115
  path =~ /\A(db|dropbox):/
86
116
  end
87
117
 
118
+ def dropbox_path(file)
119
+ raise StandardError.new("not a dropbox path: #{file}") unless dropbox_path?(file)
120
+ file.split(/:/, 2)[1]
121
+ end
122
+
123
+ def precheck(srcs, dst)
124
+ errors = []
125
+ srcs.each do |src|
126
+ begin
127
+ if dropbox_path?(src)
128
+ errors << "cannot copy from dropbox: #{src}"
129
+ else
130
+ if directory?(src)
131
+ errors << "cannot copy from directory: #{src}"
132
+ end
133
+ end
134
+ rescue Errno::ENOENT
135
+ errors << "file not found: #{src}"
136
+ end
137
+ end
138
+
139
+ begin
140
+ if directory?(dst) && srcs.size > 1
141
+ errors << "cannot copy multiple files into a file: #{dst}"
142
+ end
143
+ rescue Errno::ENOENT
144
+ errors << "file not found: #{dst}"
145
+ end
146
+
147
+ raise MultiError.new(errors) unless errors.empty?
148
+ end
149
+
88
150
  def open(file, opts)
89
151
  if dropbox_path?(file)
90
- _, path = file.split(/:/, 2)
91
- dropbox.open(path, opts){|f| yield f}
152
+ dropbox.open(dropbox_path(file), opts){|f| yield f}
92
153
  else
93
154
  File.open(file, opts){|f| yield f}
94
155
  end
95
156
  end
157
+
158
+ def directory?(file)
159
+ if dropbox_path?(file)
160
+ dropbox.directory?(dropbox_path(file))
161
+ else
162
+ File.stat(file).directory?
163
+ end
164
+ end
96
165
  end
97
166
  end
98
167
 
data/lib/dcp/dropbox.rb CHANGED
@@ -21,5 +21,23 @@ module DCP
21
21
  raise StandardError.new('read from dropbox does not implement.')
22
22
  end
23
23
  end
24
+
25
+ def file?(path)
26
+ return false if path.empty? || path == '/'
27
+ begin
28
+ @client.get_metadata(path).class == DropboxApi::Metadata::File
29
+ rescue ::DropboxApi::Errors::NotFoundError
30
+ raise Errno::ENOENT.new('file not found on dropbox')
31
+ end
32
+ end
33
+
34
+ def directory?(path)
35
+ return true if path.empty? || path == '/'
36
+ begin
37
+ @client.get_metadata(path).class == DropboxApi::Metadata::Folder
38
+ rescue ::DropboxApi::Errors::NotFoundError
39
+ raise Errno::ENOENT.new('file not found on dropbox')
40
+ end
41
+ end
24
42
  end
25
43
  end
data/lib/dcp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module DCP
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TADA Tadashi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-20 00:00:00.000000000 Z
11
+ date: 2017-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pit
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: copy files to/from Dropbox like scp command
84
98
  email:
85
99
  - t@tdtds.jp