file_discard 0.1.4 → 0.1.5

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
  SHA1:
3
- metadata.gz: baf2cacff4155deb6ead06ec337cd3cbade7b4ab
4
- data.tar.gz: e1cf01705708d7ad0d526191288874b705f00066
3
+ metadata.gz: d198ac94a7993ffcc15a2d4985561d1bee2b94d0
4
+ data.tar.gz: d2d4305716fc3c960a967c3d10fef7c665faaf63
5
5
  SHA512:
6
- metadata.gz: f769d69f5ccea663f6e6df22b1dc15a7f6f7851ba9fed5880462f0f9805bc09fd57fa0d4d1973220223e951bb5d1c7b85677435031c83a7a43efa83c4d053aa3
7
- data.tar.gz: 336a23836b05be91779c45265b535c71fd8d5cb25eff795fa5283910d11854dbbe56b6bb1e6745a074ca6d7e181c0608a5b0a1543e1531419af673d7e9044fe3
6
+ metadata.gz: 215102e583870fe1b07e3b14a477ba60e2dff2e29c7a7ca93f105d7f16e5e07b1e06288f69022408e30d410fd8048648f09d66876a6f99c034fd2fef0c2b2aa8
7
+ data.tar.gz: dfebe4588ecac6db54049c6193c6f02c56c576a4a02126af40686014a8b7ff8d502768702a5b89c54ccb89356ae370c3c00923333a3415f6f417929f243cab97
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ task default: :test
8
8
  task spec: :test
9
9
  task build: :package
10
10
 
11
- PKG_VERSION = '0.1.4'
11
+ PKG_VERSION = '0.1.5'
12
12
  NOW = Time.now.utc
13
13
 
14
14
  # delay updating the version file unless building the gem or package
data/bin/discard CHANGED
@@ -4,11 +4,15 @@ require 'optparse'
4
4
  require 'ostruct'
5
5
  require_relative '../lib/file_discard'
6
6
 
7
- options = OpenStruct.new
7
+ options = OpenStruct.new(force: 0)
8
8
  parser = OptionParser.new do |opts|
9
9
  Version = FileDiscard::VERSION
10
10
  Release = FileDiscard::RELEASE
11
11
 
12
+ opts.on('-f', '--force', 'when provided twice, permanently remove targets') do
13
+ options.force += 1
14
+ end
15
+
12
16
  opts.on('-d', '--dir', 'allow empty directories to be discarded') do |v|
13
17
  options.directory = v
14
18
  end
@@ -35,7 +39,7 @@ parser = OptionParser.new do |opts|
35
39
 
36
40
  opts.separator('')
37
41
  opts.separator('Options ignored to provide compatibility with "rm":')
38
- [?f,?i,?I].each {|o| opts.on("-#{o}")}
42
+ [?i,?I].each {|o| opts.on("-#{o}")}
39
43
 
40
44
  opts.banner << ' file ...'
41
45
  end
@@ -58,7 +62,7 @@ ARGV.each do |file|
58
62
  status = 1
59
63
  rescue Exception => ex
60
64
  $stderr.puts "#{parser.program_name}: #{file}: #{ex.class} #{ex.message}"
61
- $stderr.puts ex.backtrace if options.verbose
65
+ options.verbose and $stderr.puts ex.backtrace
62
66
  status = 2
63
67
  end
64
68
  end
data/lib/file_discard.rb CHANGED
@@ -21,6 +21,7 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'pathname'
24
+ require 'fileutils'
24
25
 
25
26
  module FileDiscard
26
27
 
@@ -112,18 +113,23 @@ module FileDiscard
112
113
  def discard(obj, options = {})
113
114
  pn = pathname_for obj
114
115
  if pn.directory?
115
- if SPECIAL_DIRS.include?(pn.basename.to_s)
116
+ SPECIAL_DIRS.include?(pn.basename.to_s) and
116
117
  raise Errno::EINVAL.new(SPECIAL_DIRS.join(' and ') << ' may not be removed')
117
- end
118
118
  unless options[:recursive]
119
- raise Errno::EISDIR.new(pn.to_s) unless options[:directory]
120
- raise Errno::ENOTEMPTY.new(pn.to_s) if pn.children.any?
119
+ options[:directory] or raise Errno::EISDIR.new(pn.to_s)
120
+ pn.children.any? and raise Errno::ENOTEMPTY.new(pn.to_s)
121
121
  end
122
122
  end
123
123
 
124
+ if options.key?(:force) && options[:force] > 1
125
+ $stderr.puts "Warning: Permanently removing #{pn}"
126
+ FileUtils.rm_rf(pn, {verbose: options[:verbose] || false})
127
+ return
128
+ end
129
+
124
130
  trash = find_trash_for pn
125
131
  unless trash.exist?
126
- raise TrashMissing.new(trash.to_s) unless FileDiscard.create_trash_when_missing
132
+ FileDiscard.create_trash_when_missing or raise TrashMissing.new(trash.to_s)
127
133
  trash.mkpath
128
134
  end
129
135
 
@@ -163,13 +169,13 @@ module FileDiscard
163
169
  def move(src, dst, options)
164
170
  src = src.expand_path
165
171
  dst = uniquify(dst.join(src.basename))
166
- puts "#{src} => #{dst}" if options[:verbose]
172
+ options[:verbose] and puts "#{src} => #{dst}"
167
173
  File.rename(src, dst)
168
- yield(src, dst) if block_given?
174
+ block_given? and yield(src, dst)
169
175
  end
170
176
 
171
177
  def uniquify(pn)
172
- return pn unless pn.exist?
178
+ pn.exist? or return pn
173
179
 
174
180
  dn = pn.dirname
175
181
  ext = pn.extname
@@ -180,7 +186,7 @@ module FileDiscard
180
186
  10.times do |i|
181
187
  ts = Time.now.strftime(fmt)
182
188
  pn = dn.join("#{base} #{ts}#{ext}")
183
- return pn unless pn.exist?
189
+ pn.exist? or return pn
184
190
  fmt = bfmt + ".%#{i}N" # use fractional seconds, with increasing precision
185
191
  end
186
192
 
@@ -204,7 +210,9 @@ module FileDiscard
204
210
  private
205
211
  def move(*args)
206
212
  super do |src, dst|
207
- dst.dirname.dirname.join('info',"#{dst.basename}.trashinfo").open('w') do |io|
213
+ infodir = dst.dirname.dirname.join('info')
214
+ infodir.directory? or infodir.mkpath
215
+ infodir.join("#{dst.basename}.trashinfo").open('w') do |io|
208
216
  io.write <<EOF
209
217
  [Trash Info]
210
218
  Path=#{src}
@@ -1,6 +1,6 @@
1
1
  module FileDiscard
2
2
  # :nodoc:
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
  # :nodoc:
5
- RELEASE = '72f0d7a:20141010011727'
5
+ RELEASE = '4441d85:20141102225117'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_discard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Robel-Forrest
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-10 00:00:00.000000000 Z
11
+ date: 2014-11-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple helper to move files to the trash folder.
14
14
  email: brad+filediscard@gigglewax.com
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  version: '0'
51
51
  requirements: []
52
52
  rubyforge_project:
53
- rubygems_version: 2.2.2
53
+ rubygems_version: 2.4.2
54
54
  signing_key:
55
55
  specification_version: 4
56
56
  summary: Move files to the trash.