Sha256: 3d6d6e8024d4178397d6b9544fbc984a59aa6342370071232edf2561a5dc84d4

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

require 'wordlist/exceptions'

require 'shellwords'

module Wordlist
  module Compression
    #
    # Handles reading compressed files.
    #
    # @since 1.0.0
    #
    module Reader
      # Mapping of compression formats to the commands to read them.
      COMMANDS = {
        gzip:  'zcat',
        bzip2: 'bzcat',
        xz:    'xzcat'
      }

      #
      # Returns the command to read the compressed wordlist.
      #
      # @param [String] path
      #   The path to the file.
      #
      # @param [:gzip, :bzip2, :xz] format
      #   The compression format of the file.
      #
      # @return [String]
      #   The shellescaped command string.
      #
      # @raise [UnknownFormat]
      #   The given format was not `:gzip`, `:bzip2`, or `:xz`.
      #
      def self.command(path, format: )
        command = COMMANDS.fetch(format) do
          raise(UnknownFormat,"unsupported format: #{format.inspect}")
        end

        Shellwords.shelljoin([command, path])
      end

      #
      # Opens the compressed wordlist for reading.
      #
      # @param [String] path
      #   The path to the file.
      #
      # @param [Hash{Symbol => Object}] kwargs
      #   Additional keyword arguments for {command}.
      #
      # @return [IO]
      #   The uncompressed IO stream.
      #
      # @raise [ArgumentError]
      #   The given format was not `:gzip`, `:bzip2`, or `:xz`.
      #
      # @raise [CommandNotFound]
      #   The `zcat,` `bzcat`, or `xzcat` command could not be found.
      #
      def self.open(path,**kwargs,&block)
        command = self.command(path,**kwargs)

        begin
          IO.popen(command,&block)
        rescue Errno::ENOENT
          raise(CommandNotFound,"#{command.inspect} command not found")
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wordlist-1.0.2 lib/wordlist/compression/reader.rb
wordlist-1.0.1 lib/wordlist/compression/reader.rb
wordlist-1.0.0 lib/wordlist/compression/reader.rb