Sha256: 475ba33fae6befacb71b29765b3ccb928cc3c1a4ed7b4f6cf21bab7aeaffac80

Contents?: true

Size: 1.46 KB

Versions: 14

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Facter
  class OsHierarchy
    def initialize
      @log = Log.new(self)
      json_file_path = File.join(File.dirname(__FILE__), '../../os_hierarchy.json')
      json_file = Util::FileHelper.safe_read(json_file_path)
      begin
        @json_os_hierarchy = JSON.parse(json_file)
      rescue JSON::ParserError => _e
        @log.error('Could not parse os_hierarchy json')
      end
    end

    def construct_hierarchy(searched_os)
      return [] if searched_os.nil?

      searched_os = searched_os.to_s.capitalize
      if @json_os_hierarchy.nil?
        @log.debug("There is no os_hierarchy, will fall back to: #{searched_os}")
        return [searched_os]
      end

      @searched_path = []
      search(@json_os_hierarchy, searched_os, [])

      @searched_path.map { |os_name| os_name.to_s.capitalize }
    end

    private

    def search(json_data, searched_element, path)
      # we hit a dead end, the os was not found on this branch
      # and we cannot go deeper
      return unless json_data

      json_data.each do |tree_node|
        # we found the searched OS, so save the path from the tree
        @searched_path = path.dup << tree_node if tree_node == searched_element

        next unless tree_node.is_a?(Hash)

        tree_node.each do |k, v|
          return @searched_path = path.dup << k if k == searched_element

          search(v, searched_element, path << k)
          path.pop
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
facter-4.0.43 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.42 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.41 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.40 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.39 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.38 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.37 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.36 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.35 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.34 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.33 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.32 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.31 lib/facter/framework/detector/os_hierarchy.rb
facter-4.0.30 lib/facter/framework/detector/os_hierarchy.rb