Module: KubeclientExec::Copy::Tar

Included in:
KubeclientExec::Copy
Defined in:
lib/kubeclient_exec/copy/tar.rb

Instance Method Summary collapse

Instance Method Details

#single_untar(io) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/kubeclient_exec/copy/tar.rb', line 81

def single_untar(io)
  Gem::Package::TarReader.new(io) do |tar|
    tar.each do |tarfile|
      return tarfile.read
    end
  end
end

#tar(src, dst, copy_file) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kubeclient_exec/copy/tar.rb', line 31

def tar(src, dst, copy_file)
  tar_file = StringIO.new

  Gem::Package::TarWriter.new(tar_file) do |tar|
    if copy_file
      relative_file = dst.split('/').last

      tar.add_file relative_file, File.stat(src).mode do |tf|
        File.open(src, "rb") { |f| tf.write f.read }
      end
    else
      Dir[File.join(src, "**/*")].each do |file|
        mode = File.stat(file).mode
        relative_file = file.sub /^#{Regexp::escape src}\/?/, ''

        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
  end

  tar_file.rewind
  tar_file
end

#untar(io, destination) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kubeclient_exec/copy/tar.rb', line 61

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 |f|
          f.write(tarfile.read)
        end
      end
    end
  end
end