Class: Ronin::CLI::FileProcessorCommand Private

Inherits:
Command
  • Object
show all
Defined in:
lib/ronin/cli/file_processor_command.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Base class for all commands which process files.

Since:

  • 2.0.0

Instance Method Summary collapse

Instance Method Details

#open_file(path, mode = 'r') {|file| ... } ⇒ File?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Opens a file for reading.

Parameters:

  • path (String)

    The path to the file to open.

  • mode (String) (defaults to: 'r')

    The mode to open the file with.

Yields:

  • (file)

    If a block is given, the newly opened file will be yielded. Once the block returns the file will automatically be closed.

Yield Parameters:

  • file (File)

    The newly opened file.

Returns:

  • (File, nil)

    If no block is given, the newly opened file object will be returned. If no block was given, then nil will be returned.

Since:

  • 2.0.0



66
67
68
69
70
71
# File 'lib/ronin/cli/file_processor_command.rb', line 66

def open_file(path,mode='r',&block)
  File.open(path,mode,&block)
rescue Errno::ENOENT
  print_error "no such file or directory: #{path}"
  exit(1)
end

#process_file(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Processes a file.

Parameters:

  • path (String)

    The path to the file to read and process.

Since:

  • 2.0.0



79
80
81
# File 'lib/ronin/cli/file_processor_command.rb', line 79

def process_file(path)
  open_file(path,&method(:process_input))
end

#process_input(input) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is abstract.

Processes an input stream.

Parameters:

  • input (File, IO)

    The opened file or the stdin input stream.

Raises:

  • (NotImplementedError)

Since:

  • 2.0.0



91
92
93
# File 'lib/ronin/cli/file_processor_command.rb', line 91

def process_input(input)
  raise(NotImplementedError,"#{self.class}##{__method__} method was not implemented")
end

#run(*files) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the command and processes files or stdin.

Parameters:

  • files (Array<String>)

    Optional files to process.

Since:

  • 2.0.0



38
39
40
41
42
43
44
# File 'lib/ronin/cli/file_processor_command.rb', line 38

def run(*files)
  if files.empty?
    process_input(stdin)
  else
    files.each(&method(:process_file))
  end
end