Sha256: 7165602a1ff51eecd91969daf3e569dc07ecaee3cc17571102f96af46230b9db

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

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

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

      return if user_queries.count < 1
      return format_for_multiple_user_queries(user_queries, resolved_facts) if user_queries.count > 1

      user_query = user_queries.first
      return format_for_no_query(resolved_facts) if user_query.empty?

      format_for_single_user_query(user_queries.first, resolved_facts) unless user_query.empty?
    end

    private

    def format_for_no_query(resolved_facts)
      @log.debug('Formatting for no user query')
      fact_collection = FormatterHelper.retrieve_fact_collection(resolved_facts)
      hash_to_hocon(fact_collection)
    end

    def format_for_multiple_user_queries(user_queries, resolved_facts)
      @log.debug('Formatting for multiple user queries')

      facts_to_display = FormatterHelper.retrieve_facts_to_display_for_user_query(user_queries, resolved_facts)
      hash_to_hocon(facts_to_display)
    end

    def format_for_single_user_query(user_query, resolved_facts)
      @log.debug('Formatting for single user query')

      fact_value = FormatterHelper.retrieve_fact_value_for_single_query(user_query, resolved_facts)

      return '' unless fact_value

      fact_value.instance_of?(Hash) ? hash_to_hocon(fact_value) : fact_value
    end

    def hash_to_hocon(fact_collection)
      render_opts = Hocon::ConfigRenderOptions.new(false, false, true, false)
      Hocon::ConfigFactory.parse_string(fact_collection.to_json).root.render(render_opts)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
facter-4.10.0 lib/facter/framework/formatters/hocon_fact_formatter.rb
facter-4.9.0 lib/facter/framework/formatters/hocon_fact_formatter.rb
facter-4.8.0 lib/facter/framework/formatters/hocon_fact_formatter.rb
facter-4.7.1 lib/facter/framework/formatters/hocon_fact_formatter.rb
facter-4.7.0 lib/facter/framework/formatters/hocon_fact_formatter.rb
facter-4.6.1 lib/facter/framework/formatters/hocon_fact_formatter.rb
facter-4.6.0 lib/facter/framework/formatters/hocon_fact_formatter.rb