Skip to content

Commit

Permalink
fix rubocop errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoblitt committed Jan 28, 2022
1 parent 9dc8354 commit 5d49e38
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 77 deletions.
12 changes: 9 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
---
# Managed by modulesync - DO NOT EDIT
# https://voxpupuli.org/docs/updating-files-managed-with-modulesync/

inherit_gem:
voxpupuli-test: rubocop.yml

Naming/MethodParameterName:
Enabled: true
MinNameLength: 1
RSpec/MultipleMemoizedHelpers:
Enabled: true
Max: 6
Style/OptionalBooleanParameter:
Enabled: false
5 changes: 4 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,7 @@ task :table do
end
end

task default: 'spec'
task default: %w[
rubocop
spec
]
1 change: 1 addition & 0 deletions bin/facterdb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'facterdb/bin'

Expand Down
49 changes: 29 additions & 20 deletions lib/facterdb.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'facter'
require 'jgrep'

Expand Down Expand Up @@ -34,9 +36,11 @@ def self.inject_source?
def self.read_json_file(f)
content = File.read(f)
return content unless inject_source?

# Find the opening brace
first_brace = content.index('{')
return content if first_brace.nil?

# Inject source file information
json_injection = "\"_facterdb_filename\": #{File.basename(f).to_json}, "
json_injection += "\"_facterdb_path\": #{File.expand_path(f).to_json}, "
Expand All @@ -47,16 +51,18 @@ def self.read_json_file(f)
# @return [Array[String]] - list of all files found in the default facterdb facts path
def self.default_fact_files
return [] unless use_defaultdb?

proj_root = File.join(File.dirname(File.dirname(__FILE__)))
facts_dir = File.expand_path(File.join(proj_root, 'facts'))
Dir.glob(File.join(facts_dir, "**", '*.facts'))
Dir.glob(File.join(facts_dir, '**', '*.facts'))
end

# @return [Array[String]] - list of all files found in the user supplied facterdb facts path
# @param fact_paths [String] - a comma separated list of paths to search for fact files
def self.external_fact_files(fact_paths = ENV['FACTERDB_SEARCH_PATHS'])
fact_paths ||= ''
return [] if fact_paths.empty?

paths = fact_paths.split(File::PATH_SEPARATOR).map do |fact_path|
unless File.directory?(fact_path)
warn("[FACTERDB] Ignoring external facts path #{fact_path} as it is not a directory")
Expand All @@ -74,23 +80,25 @@ def self.facterdb_fact_files
(external_fact_files + default_fact_files).uniq
end

def self.get_os_facts(facter_version='*', filter=[])
def self.get_os_facts(facter_version = '*', filter = [])
if facter_version == '*'
if filter.is_a?(Array)
filter_str = filter.map { |f| f.map { |k,v | "#{k}=#{v}" }.join(' and ') }.join(' or ')
elsif filter.is_a?(Hash)
filter_str = filter.map { |k,v | "#{k}=#{v}" }.join(' and ')
elsif filter.is_a?(String)
case filter
when Array
filter_str = filter.map { |f| f.map { |k, v| "#{k}=#{v}" }.join(' and ') }.join(' or ')
when Hash
filter_str = filter.map { |k, v| "#{k}=#{v}" }.join(' and ')
when String
filter_str = filter
else
raise 'filter must be either an Array a Hash or a String'
end
else
if filter.is_a?(Array)
filter_str = "facterversion=/^#{facter_version}/ and (#{filter.map { |f| f.map { |k,v | "#{k}=#{v}" }.join(' and ') }.join(' or ')})"
elsif filter.is_a?(Hash)
filter_str = "facterversion=/^#{facter_version}/ and (#{filter.map { |k,v | "#{k}=#{v}" }.join(' and ')})"
elsif filter.is_a?(String)
case filter
when Array
filter_str = "facterversion=/^#{facter_version}/ and (#{filter.map { |f| f.map { |k, v| "#{k}=#{v}" }.join(' and ') }.join(' or ')})"
when Hash
filter_str = "facterversion=/^#{facter_version}/ and (#{filter.map { |k, v| "#{k}=#{v}" }.join(' and ')})"
when String
filter_str = "facterversion=/^#{facter_version}/ and (#{filter})"
else
raise 'filter must be either an Array a Hash or a String'
Expand All @@ -104,12 +112,14 @@ def self.get_os_facts(facter_version='*', filter=[])

# @return [String] - the string filter
# @param filter [Object] - The filter to convert to jgrep string
def self.generate_filter_str(filter=nil)
def self.generate_filter_str(filter = nil)
case filter
when ::Array
'(' + filter.map { |f| f.map { |k,v | "#{k}=#{v}" }.join(' and ') }.join(') or (') + ')'
# rubocop:disable Style/StringConcatenation
'(' + filter.map { |f| f.map { |k, v| "#{k}=#{v}" }.join(' and ') }.join(') or (') + ')'
# rubocop:enable Style/StringConcatenation
when ::Hash
filter.map { |k,v | "#{k}=#{v}" }.join(' and ')
filter.map { |k, v| "#{k}=#{v}" }.join(' and ')
when ::String
filter
when ::NilClass
Expand All @@ -130,12 +140,11 @@ def self.valid_filters?(filters)

# @return [Array] - array of hashes of facts
# @param filter [Object] - The filter to convert to jgrep string
def self.get_facts(filter=nil, cache=true)
if cache && filter && filter == Thread.current[:facterdb_last_filter_seen]
return Thread.current[:facterdb_last_facts_seen]
end
def self.get_facts(filter = nil, cache = true)
return Thread.current[:facterdb_last_facts_seen] if cache && filter && filter == Thread.current[:facterdb_last_filter_seen]

filter_str = generate_filter_str(filter)
result = JGrep.jgrep(database, filter_str).map { |hash| Hash[hash.map{ |k, v| [k.to_sym, v] }] }
result = JGrep.jgrep(database, filter_str).map { |hash| hash.transform_keys(&:to_sym) }
if cache
Thread.current[:facterdb_last_filter_seen] = filter
Thread.current[:facterdb_last_facts_seen] = result
Expand Down
4 changes: 3 additions & 1 deletion lib/facterdb/bin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'facterdb'
require 'json'

Expand All @@ -8,7 +10,7 @@ def initialize(args)
end

def run
puts JSON.pretty_generate(FacterDB::get_os_facts('*', @args[0]))
puts JSON.pretty_generate(FacterDB.get_os_facts('*', @args[0]))
end
end
end
2 changes: 2 additions & 0 deletions lib/facterdb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module FacterDB
module Version
STRING = '1.12.2'
Expand Down
Loading

0 comments on commit 5d49e38

Please sign in to comment.