Class: SevenZip

Inherits:
CLIChef::Cookbook show all
Defined in:
lib/cli_chef/apps/sevenzip.rb,
lib/cli_chef/apps/sevenzip/dir.rb,
lib/cli_chef/apps/sevenzip/file.rb,
lib/cli_chef/apps/sevenzip/item.rb,
lib/cli_chef/apps/sevenzip/archive.rb

Defined Under Namespace

Classes: Archive

Constant Summary collapse

SUPPORTED_ARCHIVES =
[
  '001', '7z', 'a', 'apm', 'ar', 'arj', 'bz2', 'bzip2', 'cab', 'chi', 'chm',
  'chq', 'chw', 'cpio', 'cramfs', 'deb', 'dmg', 'doc', 'docx', 'epub', 'esd',
  'ext', 'ext2', 'ext3', 'ext4', 'fat', 'gz', 'gzip', 'hfs', 'hfsx', 'hxi',
  'hxq', 'hxr', 'hxs', 'hxw', 'ihex', 'img', 'iso', 'jar', 'lha', 'lib',
  'lit', 'lzh', 'lzma', 'mbr', 'msi', 'mslz', 'msp', 'mub', 'nsis', 'ntfs',
  'ods', 'odt', 'pkg', 'ppmd', 'ppt', 'qcow', 'qcow2', 'qcow2c', 'r00', 'rar',
  'rpm', 'scap', 'squashfs', 'swm', 'tar', 'taz', 'tbz', 'tbz2', 'tgz', 'txz',
  'udf', 'uefif', 'vdi', 'vhd', 'vmdk', 'wim', 'xar', 'xls', 'xlsx', 'xpi',
  'xz', 'z', 'zip', 'zipx'
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CLIChef::Cookbook

#execute, #execute!, ingredient, #menu, method_missing, #prepare, #prepare_args, prototype, #ready?, respond_to_missing?, #run, #run!

Class Method Details

.archive(path) ⇒ Object



80
81
82
# File 'lib/cli_chef/apps/sevenzip.rb', line 80

def self.archive(path)
  SevenZip::Archive.new(path: path)
end

Instance Method Details

#extract(archive, **opts) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/cli_chef/apps/sevenzip.rb', line 106

def extract(archive, **opts)
  type = opts[:full_path] == false ? :extract : :extract_full_paths
  args = { type => true, file: archive, yes: true, show_progress: true }.merge(opts.except(type, :file, :yes))
  run(**args) do |line, stream, job|
    job.percent = line.extract_numbers.first if line =~ /\d+\%/
  end
end

#extract!(archive, **opts) ⇒ Object



114
115
116
# File 'lib/cli_chef/apps/sevenzip.rb', line 114

def extract!(archive, **opts)
  extract(archive, opts.merge(synchronous: true))
end

#helpObject



72
73
74
# File 'lib/cli_chef/apps/sevenzip.rb', line 72

def help
  run!(help: true).body
end

#list(archive, **opts) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cli_chef/apps/sevenzip.rb', line 86

def list(archive, **opts)
  args = { list: true, archive: archive, show_technical: true }.merge(opts.except(:list, :archive, :show_technical))
  run!(args).body.split('----------', 2).last.split("\n\n").map do |details|
    hash = details.split("\n").hmap do |attribute|
      next if attribute.empty?
      key, value = attribute.split(' = ', 2)
      [key.downcase.gsub(/\s+/, '_').to_sym, value]
    end
    Archive::Item.new(hash)
  end
end

#list_dirs(archive, **opts) ⇒ Object



102
103
104
# File 'lib/cli_chef/apps/sevenzip.rb', line 102

def list_dirs(archive, **opts)
  list(archive, **opts).select { |i| i.is_a?(SevenZip::Archive::Dir) }
end

#list_files(archive, **opts) ⇒ Object



98
99
100
# File 'lib/cli_chef/apps/sevenzip.rb', line 98

def list_files(archive, **opts)
  list(archive, **opts).select { |i| i.is_a?(SevenZip::Archive::File) }
end

#test(archive, **opts) ⇒ Object

TODO Have test return something better



130
131
132
133
# File 'lib/cli_chef/apps/sevenzip.rb', line 130

def test(archive, **opts)
  args = { test: true, file: archive, yes: nil }.merge(opts.except(:test, :file, :yes))
  run(**args)
end

#test!(archive, **opts) ⇒ Object



135
136
137
# File 'lib/cli_chef/apps/sevenzip.rb', line 135

def test!(archive, **opts)
  test(archive, opts.merge(synchronous: true))
end

#versionObject



76
77
78
# File 'lib/cli_chef/apps/sevenzip.rb', line 76

def version
  help.scan(/(?<=7-zip )\d+\.\d+\s?\w*/i).first
end