Class: Hocho::InventoryProviders::File

Inherits:
Base
  • Object
show all
Defined in:
lib/hocho/inventory_providers/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ File

Returns a new instance of File.



9
10
11
# File 'lib/hocho/inventory_providers/file.rb', line 9

def initialize(path:)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/hocho/inventory_providers/file.rb', line 13

def path
  @path
end

Instance Method Details

#filesObject



15
16
17
18
19
20
21
22
# File 'lib/hocho/inventory_providers/file.rb', line 15

def files
  @files ||= case
  when ::File.directory?(path)
    Dir[::File.join(path, "*.yml")]
  else
    [path]
  end
end

#hostsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hocho/inventory_providers/file.rb', line 24

def hosts
  @hosts ||= files.flat_map do |file|
    content = Hocho::Utils::Symbolize.keys_of(YAML.load_file(file))
    content.map do |name, value|
      Host.new(
        name.to_s,
        providers: self.class,
        properties: value[:properties] || {},
        tags: value[:tags] || {},
        ssh_options: value[:ssh_options],
      )
    end
  end
end