Module: Facter::Util::FileRead
- Defined in:
- lib/facter/util/file_read.rb
Overview
FileRead is a utility module intended to provide easily mockable methods that delegate to simple file read methods. The intent is to avoid the need to execute the ‘cat` system command or `File.read` directly in Ruby, as mocking these behaviors can have wide-ranging effects.
All Facter facts are encouraged to use this method instead of File.read or Facter::Core::Execution.exec(‘cat …’)
Class Method Summary collapse
-
.read(path) ⇒ String?
read returns the raw content of a file as a string.
- .read_binary(path) ⇒ Object
Class Method Details
.read(path) ⇒ String?
read returns the raw content of a file as a string. If the file does not exist, or the process does not have permission to read the file then nil is returned.
25 26 27 28 29 30 |
# File 'lib/facter/util/file_read.rb', line 25 def self.read(path) File.read(path) rescue Errno::ENOENT, Errno::EACCES => detail Facter.debug "Could not read #{path}: #{detail.}" nil end |
.read_binary(path) ⇒ Object
32 33 34 |
# File 'lib/facter/util/file_read.rb', line 32 def self.read_binary(path) File.open(path, "rb") { |contents| contents.read } end |