Skip to content

Commit

Permalink
(FACT-2723) --list-*-groups also displays external facts
Browse files Browse the repository at this point in the history
When using --list-cache-groups or --list-block-groups, both custom and
external facts are displayed
  • Loading branch information
sebastian-miclea committed Aug 12, 2020
1 parent 6b97bb4 commit 23ea170
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/facter/framework/cli/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,26 @@ def version

desc '--list-block-groups', 'List block groups', hide: true
map ['--list-block-groups'] => :list_block_groups
def list_block_groups(*_args)
puts Facter::FactGroups.new.groups.to_yaml.lines[1..-1].join
def list_block_groups(*args)
options = @options.map { |(k, v)| [k.to_sym, v] }.to_h
Facter::Options.init_from_cli(options, args)

block_groups = Facter::FactGroups.new.groups.to_yaml.lines[1..-1].join
block_groups.gsub!(/:\s*\n/, "\n")

puts block_groups
end

desc '--list-cache-groups', 'List cache groups', hide: true
map ['--list-cache-groups'] => :list_cache_groups
def list_cache_groups(*_args)
puts Facter::FactGroups.new.groups.to_yaml.lines[1..-1].join
def list_cache_groups(*args)
options = @options.map { |(k, v)| [k.to_sym, v] }.to_h
Facter::Options.init_from_cli(options, args)

cache_groups = Facter::FactGroups.new.groups.to_yaml.lines[1..-1].join
cache_groups.gsub!(/:\s*\n/, "\n")

puts cache_groups
end

def self.exit_on_failure?
Expand Down
13 changes: 13 additions & 0 deletions lib/facter/framework/config/fact_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(group_list_path = nil)
@groups_file_path = group_list_path || default_path
@groups ||= File.readable?(@groups_file_path) ? Hocon.load(@groups_file_path) : {}
load_groups
load_groups_from_options
end

# Breakes down blocked groups in blocked facts
Expand Down Expand Up @@ -42,6 +43,18 @@ def get_group_ttls(group_name)

private

def load_groups_from_options
Options.external_dir.each do |dir|
next unless Dir.exist?(dir)

ext_facts = Dir.entries(dir)
ext_facts.reject! { |ef| ef =~ /^(\.|\.\.)$/ }
ext_facts.each do |ef|
@groups[ef] = nil
end
end
end

def load_groups
config = ConfigReader.init(Options[:config])
@block_list = config.block_list || {}
Expand Down
10 changes: 10 additions & 0 deletions spec/framework/config/fact_groups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
expect(fct_grp.instance_variable_get(:@groups)).to include('foo' => 'bar')
end

it 'merges external facts' do
external_path = '/path/to/external'
allow(Facter::Options).to receive(:external_dir).and_return([external_path])
allow(Dir).to receive(:exist?).with(external_path).and_return true
allow(Dir).to receive(:entries).with(external_path).and_return(['.', '..', 'external.sh'])
fct_grp = fact_groups.new

expect(fct_grp.instance_variable_get(:@groups)).to include('external.sh' => nil)
end

it 'merges groups from facter.conf with default group override' do
allow(Hocon).to receive(:load)
.with(File.join(ROOT_DIR, 'lib', 'facter', 'framework', 'config', '..', '..', 'fact_groups.conf'))
Expand Down

0 comments on commit 23ea170

Please sign in to comment.