Sha256: 744ff65bd542fe57d7266608063016e4515c753f30f97716707411edc744d18e

Contents?: true

Size: 888 Bytes

Versions: 1

Compression:

Stored size: 888 Bytes

Contents

module Caco::Facter
  class KeyNotFoundError < StandardError; end

  module ClassMethods
    @@fake_data = nil

    def set_fake_data=(data)
      @@fake_data = data
    end

    def use_fake(data, &block)
      old_fake_data = @@fake_data
      @@fake_data = data

      yield

      @@fake_data = old_fake_data
    end

    def call(*items)
      value = json_data(*items).dig(*items)
      raise KeyNotFoundError.new("#{items.join(":")} not found") unless value
      value
    end

    private
      def json_data(*items)
        return @@fake_data unless @@fake_data.nil?

        @@parsed_data ||= JSON.parse(external_facter_data)
      end

      def external_facter_data
        facter_path = Caco::Executer.(command: "which facter")[:output].chomp!
        result = Caco::Executer.(command: "#{facter_path} -j")
        result[:output]
      end
  end

  extend ClassMethods
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
caco-0.1.0 lib/caco/facter.rb