Module: Ey::Core::Cli::Helpers::Archive

Included in:
Recipes::Download, Recipes::Upload
Defined in:
lib/ey-core/cli/helpers/archive.rb

Instance Method Summary collapse

Instance Method Details

#archive_directory(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ey-core/cli/helpers/archive.rb', line 21

def archive_directory(path)
  tarfile = StringIO.new("")
  Gem::Package::TarWriter.new(tarfile) do |tar|
    Dir[File.join(path, "**/*")].each do |file|
      mode          = File.stat(file).mode
      relative_file = "cookbooks/#{file.sub(/^#{Regexp::escape path}\/?/, '')}"

      if File.directory?(file)
        tar.mkdir relative_file, mode
      else
        tar.add_file relative_file, mode do |tf|
          File.open(file, "rb") { |f| tf.write f.read }
        end
      end
    end
  end

  tarfile.rewind
  gzip(tarfile)
end

#gzip(tarfile) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/ey-core/cli/helpers/archive.rb', line 10

def gzip(tarfile)
  gz = StringIO.new("")
  zipper = Zlib::GzipWriter.new(gz)
  zipper.write tarfile.string
  zipper.close # this is necessary!

  # z was closed to write the gzip footer, so
  # now we need a new StringIO
  StringIO.new gz.string
end

#ungzip(tarfile) ⇒ Object



42
43
44
45
46
47
# File 'lib/ey-core/cli/helpers/archive.rb', line 42

def ungzip(tarfile)
  zipped = Zlib::GzipReader.new(tarfile)
  unzipped = StringIO.new(zipped.read)
  zipped.close
  unzipped
end

#untar(io, destination) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ey-core/cli/helpers/archive.rb', line 49

def untar(io, destination)
  Gem::Package::TarReader.new io do |tar|
    tar.each do |tarfile|
      destination_file = File.join destination, tarfile.full_name

      if tarfile.directory?
        FileUtils.mkdir_p destination_file
      else
        destination_directory = File.dirname(destination_file)
        FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory)
        File.open destination_file, "wb" do |file|
          file.print tarfile.read
        end
      end
    end
  end
end