Sha256: a8893f4ae5d66256a409363faf7cc630fff706a67785d486dbbc9f85b5846121

Contents?: true

Size: 828 Bytes

Versions: 22

Compression:

Stored size: 828 Bytes

Contents

require 'open3'

module Kafo
  class BaseContext
    def facts
      self.class.facts
    end

    private

    def self.symbolize(data)
      case data
      when Hash
        Hash[data.map { |key, value| [key.to_sym, symbolize(value)] }]
      when Array
        data.map { |v| symbolize(v) }
      else
        data
      end
    end

    def self.clear_caches
      @facts = nil
      @facter_path = nil
    end

    def self.facts
      @facts ||= begin
        result = run_command("#{facter_path} --json")
        symbolize(JSON.load(result) || {})
      end
    end

    def self.facter_path
      @facter_path ||= PuppetCommand.search_puppet_path('facter')
    end

    def self.run_command(command)
      stdout, _stderr, _status = Open3.capture3(*PuppetCommand.format_command(command))
      stdout
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
kafo-5.0.0 lib/kafo/base_context.rb
kafo-4.1.0 lib/kafo/base_context.rb