Module: TerraformEnterprise::CommandLine::Util::Tar

Included in:
Command, Commands::PolicyChecksCommand
Defined in:
lib/terraform_enterprise/command_line/util.rb

Overview

Module to perform a tar and gz of a directory

Instance Method Summary collapse

Instance Method Details

#gzip(tarfile) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/terraform_enterprise/command_line/util.rb', line 31

def gzip(tarfile)
  gzip_string = StringIO.new('')
  gzip_writer = Zlib::GzipWriter.new(gzip_string)
  gzip_writer.write tarfile.string
  gzip_writer.close
  gzip_string.string
end

#tar(path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/terraform_enterprise/command_line/util.rb', line 10

def tar(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 = 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
  tarfile
end

#tarball(path) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/terraform_enterprise/command_line/util.rb', line 39

def tarball(path)
  full_path = File.expand_path(path)
  if File.directory?(full_path)
    gzip(tar(full_path))
  else
    File.read(full_path)
  end
end