Class: LegacyFacter::Util::Loader
- Inherits:
-
Object
- Object
- LegacyFacter::Util::Loader
- Defined in:
- lib/facter/custom_facts/util/loader.rb
Instance Method Summary collapse
-
#initialize(environment_vars = ENV) ⇒ Loader
constructor
A new instance of Loader.
-
#load(fact) ⇒ Object
Load all resolutions for a single fact.
-
#load_all ⇒ Object
Load all facts from all directories.
-
#search_path ⇒ Array<String>
List directories to search for fact files.
Constructor Details
#initialize(environment_vars = ENV) ⇒ Loader
Returns a new instance of Loader.
7 8 9 10 |
# File 'lib/facter/custom_facts/util/loader.rb', line 7 def initialize(environment_vars = ENV) @loaded = [] @environment_vars = environment_vars end |
Instance Method Details
#load(fact) ⇒ Object
Load all resolutions for a single fact.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/facter/custom_facts/util/loader.rb', line 16 def load(fact) # Now load from the search path shortname = fact.to_s.downcase load_env(shortname) filename = shortname + '.rb' paths = search_path paths&.each do |dir| # Load individual files file = File.join(dir, filename) load_file(file) if FileTest.file?(file) end end |
#load_all ⇒ Object
Load all facts from all directories.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/facter/custom_facts/util/loader.rb', line 35 def load_all return if defined?(@loaded_all) load_env paths = search_path paths&.each do |dir| # clean the search path of wrong slashes and backslashes dir = dir.gsub(%r{[\/\\]+}, File::SEPARATOR) # dir is already an absolute path Dir.glob(File.join(dir, '*.rb')).each do |path| # exclude dirs that end with .rb load_file(path) if FileTest.file?(path) end end @loaded_all = true end |
#search_path ⇒ Array<String>
List directories to search for fact files.
Search paths are gathered from the following sources:
-
$LOAD_PATH entries are expanded to absolute paths
-
ENV is split and used verbatim
-
Entries from Facter.search_path are used verbatim
A warning will be generated for paths in Facter.search_path that are not absolute directories.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/facter/custom_facts/util/loader.rb', line 67 def search_path search_paths = [] search_paths += $LOAD_PATH.map { |path| File.('facter', path) } if @environment_vars.include?('FACTERLIB') search_paths += @environment_vars['FACTERLIB'].split(File::PATH_SEPARATOR) end search_paths.delete_if { |path| !valid_search_path?(path) } Facter::Options.custom_dir.each do |path| if valid_search_path?(path) search_paths << path else LegacyFacter.warn "Excluding #{path} from search path. Fact file paths must be an absolute directory" end end search_paths.delete_if { |path| !File.directory?(path) } search_paths.uniq end |