Class: Tarchiver::Helpers
- Inherits:
-
Object
- Object
- Tarchiver::Helpers
- Defined in:
- lib/tarchiver/helpers.rb
Class Method Summary collapse
- .cleanup(archive_input, tar_path, options) ⇒ Object
- .determine_archive_name(input, input_type, options) ⇒ Object
- .determine_archive_type(archive) ⇒ Object
- .prepare_for_tarchiving(archive_path) ⇒ Object
- .sanitize_input(input, options) ⇒ Object
- .sanitize_options(options) ⇒ Object
- .terminate(error = nil, options) ⇒ Object
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, ) ::File.delete(tar_path) if tar_path && ::File.exists?(tar_path) FileUtils.rm_rf(archive_input) if [: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, ) if [:custom_archive_name] return [:add_timestamp] ? "#{[:custom_archive_name]}-#{Time.now.to_i}" : [:custom_archive_name] end if input_type == :enumerable name = Tarchiver::Constants::DEFAULT_ARCHIVE_NAME else name = ::File.basename(input) end [: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, ) if input.is_a? Enumerable archive_name = self.determine_archive_name(input, :enumerable, ) to_archive = input relative_to = nil elsif ::File.file?(input) archive_name = self.determine_archive_name(input, :file, ) to_archive = [input] relative_to = ::File.basename(input) elsif ::File.directory?(input) archive_name = self.determine_archive_name(input, :directory, ) 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]), ) 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.() # Ensure that blocksize is an integer [:blocksize] = Integer([:blocksize]) # Ensure valid archive extension terminate(ArgumentError.new, ) unless Tarchiver::Constants::EXTENSIONS.include?([:archive_type]) end |
.terminate(error = nil, options) ⇒ Object
61 62 63 64 |
# File 'lib/tarchiver/helpers.rb', line 61 def self.terminate(error=nil, ) raise error if error && [:raise_errors] nil end |