Sha256: 27ed7ac6b37aea93c1cf397b46bb617f9f8bfaa08eda719533663d576da4d279

Contents?: true

Size: 952 Bytes

Versions: 77

Compression:

Stored size: 952 Bytes

Contents

# frozen_string_literal: true

module Facter
  class JsonFactFormatter
    def initialize
      @log = Facter::Log.new(self)
    end

    def format(resolved_facts)
      user_queries = resolved_facts.uniq(&:user_query).map(&:user_query)

      if user_queries.count == 1 && user_queries.first.empty?
        format_for_no_query(resolved_facts)
      else
        format_for_user_queries(user_queries, resolved_facts)
      end
    end

    private

    def format_for_no_query(resolved_facts)
      @log.debug('No user query provided')

      fact_collection = FormatterHelper.retrieve_fact_collection(resolved_facts)
      JSON.pretty_generate(fact_collection)
    end

    def format_for_user_queries(user_queries, resolved_facts)
      @log.debug('User provided a query')

      facts_to_display = FormatterHelper.retrieve_facts_to_display_for_user_query(user_queries, resolved_facts)
      JSON.pretty_generate(facts_to_display)
    end
  end
end

Version data entries

77 entries across 77 versions & 1 rubygems

Version Path
facter-4.10.0 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.9.0 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.8.0 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.7.1 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.7.0 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.6.1 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.6.0 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.5.2 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.5.1 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.5.0 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.4.3 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.4.2 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.4.1 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.4.0 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.3.1 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.3.0 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.2.14 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.2.13 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.2.12 lib/facter/framework/formatters/json_fact_formatter.rb
facter-4.2.11 lib/facter/framework/formatters/json_fact_formatter.rb