Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You have twenty seconds to comply. #229

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ jobs:
env:
BUNDLE_WITHOUT: release
- name: Run tests
run: bundle exec rake spec
run: bundle exec rake rubocop spec
- name: Build gem
run: gem build *.gemspec
12 changes: 12 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
inherit_gem:
voxpupuli-test: rubocop.yml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekohl ehat do you think about this? at the moment we only use it in modules. other gems have their own rubocop config which is isually way shorter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think our Rubocop in vp-test is intended for Puppet modules, not real Ruby code. It would be better to come up with a shared config for real gems. It also would prevent this from pulling in a LOT of unrelated code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly sure that as-is is a bad idea. My goal with this PR was to simply get rubocop working and merged before the style changes bit-rot. Then additional PRs can be made as cop configuration is agreed upon. Configuring rubocop is like ordering a pizza... I was concerned that starting from scratch was going to take a long time to build consensus. I have two suggestions to resolve the circular dependency and I honestly don't have much of a preference.

A) move rubocop.yml out of voxpupuli-test: into a new gem
B) copy the current voxpupuli-test rubocop.yml into this module with minimal modifications

Are either of those acceptable solutions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those are all points I can agree with. I'm fine with either solution. My suggestion there was roughly option A and then use that gem in all Ruby projects maintained by Vox Pupuli. I'd also be happy with option B short term and option A long term. That would at least allow this to move forward and we need to align a lot of projects anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekohl If I copy the rubcop.yml from voxpupuli-test, would this PR be ready for merge?

Naming/MethodParameterName:
Enabled: true
MinNameLength: 1
RSpec/MultipleMemoizedHelpers:
Enabled: true
Max: 6
Style/OptionalBooleanParameter:
Enabled: false
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ end

group :development do
gem 'github_changelog_generator', '>= 1.16.4'
# provides the rubocop config and an awesome circular dependency to boot!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤨 Wat? CI seems happy with this…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

voxpupuli-test has a dep on facterdb. I'm not sure if this would work at all if the Gemfile was in a released version of facterdb. We all use constraint resolvers every day but does anyone actually understand them? :)

gem 'voxpupuli-test', '~> 5.2'
end
8 changes: 7 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'facterdb/version'
require 'rubocop/rake_task'

RuboCop::RakeTask.new

begin
require 'rspec/core/rake_task'
Expand Down Expand Up @@ -149,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