module GreenHat::ArchiveLoader

Archive Manipulator

Public Class Methods

archive?(file_name) click to toggle source
# File lib/greenhat/archive.rb, line 71
def self.archive?(file_name)
  archive_types.any? do |type|
    file_name.match?(/(\.#{type})$/)
  end
end
archive_types() click to toggle source
# File lib/greenhat/archive.rb, line 77
def self.archive_types
  %w[s gz tar]
end
load(archive_path) click to toggle source
# 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) click to toggle source

Ignore No Commands rubocop:disable Style/SymbolProc

# 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) click to toggle source

Handle Different Types of Archives

# 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