Module: Kanrisuru::Core::Archive

Extended by:
OsPackage::Define
Defined in:
lib/kanrisuru/core/archive.rb,
lib/kanrisuru/core/archive/types.rb,
lib/kanrisuru/core/archive/commands/tar.rb

Defined Under Namespace

Classes: FilePath

Instance Method Summary collapse

Methods included from OsPackage::Define

extended, os_define

Instance Method Details

#tar(action, file, opts = {}) ⇒ Object



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kanrisuru/core/archive/commands/tar.rb', line 6

def tar(action, file, opts = {})
  paths       = opts[:paths]
  exclude     = opts[:exclude]

  command = Kanrisuru::Command.new('tar --restrict')

  directory = opts[:directory] ? realpath(opts[:directory], strip: true).path : nil
  command.append_arg('-C', directory)
  command.append_arg('-f', file)

  set_compression(command, opts[:compress]) if opts[:compress]

  case action
  when 'list', 't'
    command.append_flag('-t')
    command.append_arg('--occurrence', opts[:occurrence])
    command.append_flag('--label', opts[:label])
    command.append_flag('--multi-volume', opts[:multi_volume])

    execute_shell(command)
    Kanrisuru::Result.new(command) do |cmd|
      items = cmd.to_a

      items.map do |item|
        FilePath.new(item)
      end
    end
  when 'extract', 'get', 'x'
    command.append_flag('-x')
    command.append_arg('--occurrence', opts[:occurrence])

    command.append_flag('--no-same-owner', opts[:no_same_owner])
    command.append_flag('--no-same-permissions', opts[:no_same_permissions])
    command.append_flag('--no-selinux', opts[:no_selinux])
    command.append_flag('--no-xattrs', opts[:no_xattrs])
    command.append_flag('--preserve-permissions', opts[:preserve_permissions])
    command.append_flag('--same-owner', opts[:same_owners])
    command.append_flag('--multi-volume', opts[:multi_volume])
    command.append_flag('--label', opts[:label])
    command.append_flag('--one-file-system', opts[:one_file_system])
    command.append_flag('--keep-old-files', opts[:keep_old_files])
    command.append_flag('--skip-old-files', opts[:skip_old_files])
    command.append_flag('--overwrite', opts[:overwrite])
    command.append_flag('--overwrite-dir', opts[:overwrite_dir])
    command.append_flag('--unlink-first', opts[:unlink_first])
    command.append_flag('--recursive-unlink', opts[:recursive_unlink])

    command.append_array(paths)

    execute_shell(command)
    Kanrisuru::Result.new(command)
  when 'create', 'c'
    command.append_flag('-c')
    command.append_flag('--multi-volume', opts[:multi_volume])

    if Kanrisuru::Util.present?(exclude)
      options = exclude.instance_of?(String) ? [exclude] : exclude
      options.each do |option|
        command << "--exclude=#{option}"
      end
    end

    command.append_array(paths)

    execute_shell(command)
    Kanrisuru::Result.new(command)
  when 'append', 'r'
    command.append_flag('-r')
    command.append_array(paths)

    execute_shell(command)
    Kanrisuru::Result.new(command)
  when 'catenate', 'concat', 'A'
    command.append_flag('-A')
    command.append_array(paths)

    execute_shell(command)
    Kanrisuru::Result.new(command)
  when 'update', 'u'
    command.append_flag('-u')
    command.append_array(paths)

    execute_shell(command)
    Kanrisuru::Result.new(command)
  when 'diff', 'compare', 'd'
    command.append_flag('-d')
    command.append_arg('--occurrence', opts[:occurrence])

    execute_shell(command)
    Kanrisuru::Result.new(command)
  when 'delete'
    command.append_flag('--delete')
    command.append_arg('--occurrence', opts[:occurrence])
    command.append_array(paths)

    execute_shell(command)
    Kanrisuru::Result.new(command)
  else
    raise ArgumentError, 'Invalid argument action'
  end
end