Module: GreenHat::ArchiveLoader

Defined in:
lib/greenhat/archive.rb

Overview

Archive Manipulator

Class Method Summary collapse

Class Method Details

.archive?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/greenhat/archive.rb', line 71

def self.archive?(file_name)
  archive_types.any? do |type|
    file_name.match?(/(\.#{type})$/)
  end
end

.archive_typesObject



77
78
79
# File 'lib/greenhat/archive.rb', line 77

def self.archive_types
  %w[s gz tar]
end

.load(archive_path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/greenhat/archive.rb', line 5

def self.load(archive_path)
  path = "#{$TMP}/#{SecureRandom.uuid}"
  Dir.mkdir path

  # Initially Copy file into tmpdir
  FileUtils.cp(archive_path, "#{path}/")

  # Extract Everything
  loop do
    archive_list = Find.find(path).reject { |x| File.directory? x }.select { |y| archive? y }
    break if archive_list.empty?

    archive_list.each do |archive|
      # Different File Type Handler
      unpack(archive, path)
    end
  end

  # Ignore Directories
  list = Find.find(path).reject { |x| File.directory? x }

  # Ignore Empty Files
  list.reject! { |x| File.size(x).zero? }

  archive = Archive.new(name: archive_path, path: path)
  archive.save
  # file = list[2]
  # thing = archive.things_create(file: file)
  # thing.setup

  list.each do |file|
    # Ensure Valid Content
    next if missing_command(file)

    # Thread.new do
    thing = archive.things_create(file: file)
    thing.setup
    # end
  end
end

.missing_command(file) ⇒ Object

Ignore No Commands rubocop:disable Style/SymbolProc



48
49
50
51
# File 'lib/greenhat/archive.rb', line 48

def self.missing_command(file)
  first_line = File.open(file) { |f| f.readline }
  ['command not found', ': not found'].any? { |x| first_line.include? x }
end

.unpack(archive_path, path) ⇒ Object

Handle Different Types of Archives



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/greenhat/archive.rb', line 55

def self.unpack(archive_path, path)
  case File.extname archive_path
  when '.tar'
    `bsdtar -xf "#{archive_path}" -C #{path}`
    FileUtils.rm(archive_path)
  when '.gz'
    `gzip -d "#{archive_path}"`
  when '.s'
    # Find Original Directory, Split Path, Rename to .gz
    base_path = archive_path.gsub(File.basename(archive_path), '')
    FileUtils.mv(archive_path, "#{base_path}/#{File.basename(archive_path, '.s')}.gz")
  else
    FileUtils.cp(archive_path, "#{path}/#{archive_path}")
  end
end