Module: Decimate

Defined in:
lib/decimate.rb,
lib/decimate/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.dir!(path, opts = {}) ⇒ Object

Securely deletes given directory recursively using shred.

- Returns nil if directory does not exist
- Returns stdout from shred operation if dir exists and shredded successfully
- If optional regex sanity check is included, exception will be raised if match against given path fails
- Raises if shred or find command triggers any status code other than zero
- Raises if shred command not found

Usage: Decimate.dir! ‘my-unloved-dirctory’ Decimate.dir! ‘my-unloved-dirctory’, path_must_match: /unloved/



59
60
61
62
63
64
65
66
67
# File 'lib/decimate.rb', line 59

def self.dir! path, opts={}
  return unless Dir.exist?(path)
  fail_unless_shred
  validate_path path, opts[:path_must_match]
  
  stdout = run "find #{path} -type f -execdir #{shred_cmd} '{}' ';'"
  FileUtils.rm_rf path
  stdout
end

.fail_unless_shredObject



25
26
27
# File 'lib/decimate.rb', line 25

def self.fail_unless_shred
  raise if `which shred`.chomp.empty?
end

.file!(path, opts = {}) ⇒ Object

Securely deletes given file using shred.

- Returns nil if file does not exist
- Returns stdout from shred operation if file exists and shredded successfully
- If optional regex sanity check is included, exception will be raised if match against given path fails
- Raises if shred or find command triggers any status code other than zero
- Raises if shred command not found


38
39
40
41
42
43
44
# File 'lib/decimate.rb', line 38

def self.file! path, opts={}
  return unless File.exist?(path)
  fail_unless_shred
  validate_path path, opts[:path_must_match]
 
  run "#{shred_cmd} #{path}"
end

.run(cmd) ⇒ Object

Executes given command using Open3.capture3 Raises exception if non-zero status call returned, writes to error log



11
12
13
14
15
# File 'lib/decimate.rb', line 11

def self.run cmd
  stdout,stderr,status = Open3.capture3 cmd
  raise "Domination failed: #{cmd}" unless status.nil? || status == 0
  stdout
end

.shred_cmdObject



6
# File 'lib/decimate.rb', line 6

def self.shred_cmd; "shred -uv"; end

.validate_path(path, required_regex = nil) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/decimate.rb', line 17

def self.validate_path path, required_regex=nil
  raise ArgumentError.new("expected Regexp, given #{required_regex.class}") if required_regex && !required_regex.is_a?(Regexp)
  File.expand_path(path).tap do |path|
    raise ArgumentError.new("It looks like you're trying to remove root dir. :( Got #{path}") if path == '/'
    raise ArgumentError.new("Path #{path} does not match #{required_regex}") if required_regex && !path.match(required_regex)
  end
end