Class: Tarchiver::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/tarchiver/helpers.rb

Class Method Summary collapse

Class Method Details

.cleanup(archive_input, tar_path, options) ⇒ Object



56
57
58
59
# File 'lib/tarchiver/helpers.rb', line 56

def self.cleanup(archive_input, tar_path, options)
  ::File.delete(tar_path) if tar_path && ::File.exists?(tar_path)
  FileUtils.rm_rf(archive_input) if options[:delete_input_on_success]
end

.determine_archive_name(input, input_type, options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tarchiver/helpers.rb', line 35

def self.determine_archive_name(input, input_type, options)
  if options[:custom_archive_name]
    return options[:add_timestamp] ? "#{options[:custom_archive_name]}-#{Time.now.to_i}" : options[:custom_archive_name]
  end
  if input_type == :enumerable
    name = Tarchiver::Constants::DEFAULT_ARCHIVE_NAME
  else
    name = ::File.basename(input)
  end
  options[:add_timestamp] ? "#{name}-#{Time.now.to_i}" : name
end

.determine_archive_type(archive) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/tarchiver/helpers.rb', line 47

def self.determine_archive_type(archive)
  if archive.match(/tar$/)
    :tar
  elsif archive.match(/gz$/)
    :compressed
  end
  
end

.prepare_for_tarchiving(archive_path) ⇒ Object



31
32
33
# File 'lib/tarchiver/helpers.rb', line 31

def self.prepare_for_tarchiving(archive_path)
  FileUtils.mkdir_p(File.dirname(archive_path), verbose: false) unless ::File.directory?(::File.dirname(archive_path))
end

.sanitize_input(input, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tarchiver/helpers.rb', line 4

def self.sanitize_input(input, options)
  if input.is_a? Enumerable
    archive_name = self.determine_archive_name(input, :enumerable, options)
    to_archive = input
    relative_to = nil
  elsif ::File.file?(input)
    archive_name = self.determine_archive_name(input, :file, options)
    to_archive = [input]
    relative_to = ::File.basename(input)
  elsif ::File.directory?(input)
    archive_name = self.determine_archive_name(input, :directory, options)
    to_archive = ::Dir.glob(File.join(input, '**', '*'), ::File::FNM_DOTMATCH)
    relative_to = ::File.basename(input)
  else
    terminate(ArgumentError.new(Tarchiver::Constants::MESSAGES[:input_not_sane]), options)
  end
  return archive_name, relative_to, to_archive
end

.sanitize_options(options) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/tarchiver/helpers.rb', line 23

def self.sanitize_options(options)
  # Ensure that blocksize is an integer
  options[:blocksize] = Integer(options[:blocksize])
  # Ensure valid archive extension
  terminate(ArgumentError.new, options) unless Tarchiver::Constants::EXTENSIONS.include?(options[:archive_type])
  options
end

.terminate(error = nil, options) ⇒ Object



61
62
63
64
# File 'lib/tarchiver/helpers.rb', line 61

def self.terminate(error=nil, options)
  raise error if error && options[:raise_errors]
  nil
end