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

Add support for passing multiple plugin directories on CLI/client.rb #1221

Merged
merged 5 commits into from
Aug 7, 2018
Merged
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
11 changes: 11 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# UNRELEASED

## Multiple plugin directories

You can now specify more than one directory to load additional Ohai plugins from by using the `--directory` / `-d` flag more than once.

Example:
```bash
ohai -d /path/to/more/plugins -d /another/path/to/more/plugins
```

# Ohai Release Notes 14.3

## Detection of Amazon Linux 2.0
Expand Down
7 changes: 5 additions & 2 deletions lib/ohai/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ class Ohai::Application
option :directory,
short: "-d DIRECTORY",
long: "--directory DIRECTORY",
description: "A directory to add to the Ohai plugin search path",
proc: lambda { |path| Ohai::Config.platform_specific_path(path) }
description: "A directory to add to the Ohai plugin search path. If passing multiple directories use this option more than once.",
proc: lambda { |path, path_array|
(path_array ||= []) << Ohai::Config.platform_specific_path(path)
path_array
}

option :log_level,
short: "-l LEVEL",
Expand Down
10 changes: 7 additions & 3 deletions lib/ohai/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ def attributes_print(a)
def configure_ohai
Ohai.config.merge!(@config)

if Ohai.config[:directory] &&
!Ohai.config[:plugin_path].include?(Ohai.config[:directory])
Ohai.config[:plugin_path] << Ohai.config[:directory]
# add any additional CLI passed directories to the plugin path excluding duplicates
unless Ohai.config[:directory].nil?
# make sure the directory config is an array since it could be a string set in client.rb
Array(Ohai.config[:directory]).each do |dir|
next if Ohai.config[:plugin_path].include?(dir)
Ohai.config[:plugin_path] << dir
end
end

logger.debug("Running Ohai with the following configuration: #{Ohai.config.configuration}")
Expand Down
2 changes: 1 addition & 1 deletion ohai.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |s|

s.add_dependency "systemu", "~> 2.6.4"
s.add_dependency "ffi-yajl", "~> 2.2"
s.add_dependency "mixlib-cli"
s.add_dependency "mixlib-cli", ">= 1.7.0" # 1.7+ needed to support passing multiple options
s.add_dependency "mixlib-config", "~> 2.0"
s.add_dependency "mixlib-log", "~> 2.0", ">= 2.0.1"
s.add_dependency "mixlib-shellout", "~> 2.0"
Expand Down
17 changes: 14 additions & 3 deletions spec/unit/system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
it "merges provided configuration options into the ohai config context" do
config = {
disabled_plugins: [ :Foo, :Baz ],
directory: "/some/extra/plugins",
directory: ["/some/extra/plugins"],
critical_plugins: [ :Foo, :Bar ],
}
Ohai::System.new(config)
Expand All @@ -47,13 +47,24 @@
end
end

context "when directory is configured" do
context "when a single directory is configured as a string" do
let(:directory) { "/some/fantastic/plugins" }

it "adds directory to plugin_path" do
Ohai.config[:directory] = directory
Ohai::System.new({ invoked_from_cli: true })
expect(Ohai.config[:plugin_path]).to include(directory)
expect(Ohai.config[:plugin_path]).to include("/some/fantastic/plugins")
end
end

context "when multiple directories are configured" do
let(:directory) { ["/some/fantastic/plugins", "/some/other/plugins"] }

it "adds directories to plugin_path" do
Ohai.config[:directory] = directory
Ohai::System.new({ invoked_from_cli: true })
expect(Ohai.config[:plugin_path]).to include("/some/fantastic/plugins")
expect(Ohai.config[:plugin_path]).to include("/some/other/plugins")
end
end

Expand Down