Sha256: 1fd098ca05d8062ba15b0b4b122ce888fe4b9289f92c54757de52631e4abd70e

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module Facter
  module Util
    class FileHelper
      @log = Log.new(self)

      class << self
        DEBUG_MESSAGE = 'File at: %s is not accessible.'

        def safe_read(path, default_return = '')
          return File.read(path, encoding: Encoding::UTF_8) if File.readable?(path)

          log_failed_to_read(path)
          default_return
        end

        def safe_readlines(path, default_return = [])
          return File.readlines(path, encoding: Encoding::UTF_8) if File.readable?(path)

          log_failed_to_read(path)
          default_return
        end

        # This previously acted as a helper method for versions of Ruby older
        # than 2.5, before Dir.children was added. As it isn't a private
        # method, we can't remove it entirely until the next major Facter
        # release (presumably Facter 5).
        def dir_children(path)
          children = Dir.children(path)
          children
        end

        private

        def log_failed_to_read(path)
          @log.debug(DEBUG_MESSAGE % path)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facter-4.5.2 lib/facter/util/file_helper.rb
facter-4.5.1 lib/facter/util/file_helper.rb
facter-4.5.0 lib/facter/util/file_helper.rb