2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2020-09-04 14:13:43 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-05-31 12:37:25 -07:00
|
|
|
require "system_command"
|
|
|
|
|
2020-09-04 14:13:43 -07:00
|
|
|
module Utils
|
|
|
|
# Helper functions for interacting with tar files.
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
module Tar
|
2020-10-10 17:53:31 +02:00
|
|
|
class << self
|
|
|
|
TAR_FILE_EXTENSIONS = %w[.tar .tb2 .tbz .tbz2 .tgz .tlz .txz .tZ].freeze
|
2020-10-10 15:53:33 +02:00
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
def available?
|
|
|
|
executable.present?
|
|
|
|
end
|
2020-09-04 14:13:43 -07:00
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
def executable
|
|
|
|
return @executable if defined?(@executable)
|
2020-09-04 14:13:43 -07:00
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
gnu_tar_gtar_path = HOMEBREW_PREFIX/"opt/gnu-tar/bin/gtar"
|
|
|
|
gnu_tar_gtar = gnu_tar_gtar_path if gnu_tar_gtar_path.executable?
|
|
|
|
@executable = which("gtar") || gnu_tar_gtar || which("tar")
|
|
|
|
end
|
2020-09-04 14:13:43 -07:00
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
def validate_file(path)
|
|
|
|
return unless available?
|
2020-09-04 14:13:43 -07:00
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
path = Pathname.new(path)
|
|
|
|
return unless TAR_FILE_EXTENSIONS.include? path.extname
|
2020-09-04 14:13:43 -07:00
|
|
|
|
2021-05-31 12:37:25 -07:00
|
|
|
stdout, _, status = system_command(executable, args: ["--list", "--file", path], print_stderr: false)
|
|
|
|
odie "#{path} is not a valid tar file!" if !status.success? || stdout.blank?
|
2020-10-10 17:53:31 +02:00
|
|
|
end
|
2020-09-04 14:13:43 -07:00
|
|
|
|
2020-10-10 17:53:31 +02:00
|
|
|
def clear_executable_cache
|
|
|
|
remove_instance_variable(:@executable) if defined?(@executable)
|
|
|
|
end
|
2020-09-04 14:13:43 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|