summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc A. Paradise <marc.paradise@gmail.com>2019-04-08 11:10:56 -0400
committerMarc A. Paradise <marc.paradise@gmail.com>2019-04-24 13:25:58 -0400
commit958cfd999370387f6b7debd7bd725b2da27832bc (patch)
tree16fcff249c6647307cbad222c9d7a9abdd7feea3
parenta31851492fb2cd8d03d57fa774283cb8b6d0b102 (diff)
downloadchef-958cfd999370387f6b7debd7bd725b2da27832bc.tar.gz
Add Knife#config_source(key)
Subclasses can use this to determine where a given key has received its value from. This allows subclasses to handle deprecated CLI options that may still be valid in Chef config by determining if the option was provided on the CLI or via Chef::Config Signed-off-by: Marc A. Paradise <marc.paradise@gmail.com>
-rw-r--r--lib/chef/knife.rb22
-rw-r--r--spec/unit/knife_spec.rb20
2 files changed, 35 insertions, 7 deletions
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb
index b5b32cb193..b1f85386be 100644
--- a/lib/chef/knife.rb
+++ b/lib/chef/knife.rb
@@ -344,8 +344,15 @@ class Chef
# Chef::Config[:knife] would break the defaults in the cli that we would otherwise
# overwrite.
def config_file_settings
+ @key_sources = { cli: [], config: [] }
cli_keys.each_with_object({}) do |key, memo|
- memo[key] = Chef::Config[:knife][key] if Chef::Config[:knife].key?(key)
+ if config.key?(key)
+ @key_sources[:cli] << key
+ end
+ if Chef::Config[:knife].key?(key)
+ @key_sources[:config] << key
+ memo[key] = Chef::Config[:knife][key] if Chef::Config[:knife].key?(key)
+ end
end
end
@@ -356,9 +363,16 @@ class Chef
def merge_configs
# other code may have a handle to the config object, so use Hash#replace to deliberately
# update-in-place.
- config.replace(
- default_config.merge(config_file_settings).merge(config)
- )
+ config.replace(default_config.merge(config_file_settings).merge(config))
+ end
+
+ # Return where a config key has been sourced,
+ # :cli, :config, or nil if the key is not set.
+
+ def config_source(key)
+ return :cli if @key_sources[:cli].include? key
+ return :config if @key_sources[:config].include? key
+ nil
end
# Catch-all method that does any massaging needed for various config
diff --git a/spec/unit/knife_spec.rb b/spec/unit/knife_spec.rb
index ff89bc6f30..f48ab4019c 100644
--- a/spec/unit/knife_spec.rb
+++ b/spec/unit/knife_spec.rb
@@ -299,7 +299,7 @@ describe Chef::Knife do
expect(Chef::Config[:log_level]).to eql(:warn)
end
- it "prefers the default value if no config or command line value is present" do
+ it "prefers the default value if no config or command line value is present and reports the source as default" do
knife_command = KnifeSpecs::TestYourself.new([]) # empty argv
knife_command.configure_chef
expect(knife_command.config[:opt_with_default]).to eq("default-value")
@@ -310,18 +310,32 @@ describe Chef::Knife do
knife_command = KnifeSpecs::TestYourself.new([]) # empty argv
knife_command.configure_chef
expect(knife_command.config[:opt_with_default]).to eq("from-knife-config")
+ expect(knife_command.config_source(:opt_with_default)).to eq (:config)
end
- it "prefers a value from command line over Chef::Config and the default" do
+ it "correctly reports Chef::Config as the source when a a config entry comes from there" do
Chef::Config[:knife][:opt_with_default] = "from-knife-config"
+ knife_command = KnifeSpecs::TestYourself.new([]) # empty argv
+ knife_command.configure_chef
+ expect(knife_command.config_source(:opt_with_default)).to eq (:config)
+ end
+
+ it "prefers a value from command line over Chef::Config and the default and reports the source as CLI" do
knife_command = KnifeSpecs::TestYourself.new(["-D", "from-cli"])
knife_command.configure_chef
expect(knife_command.config[:opt_with_default]).to eq("from-cli")
+ expect(knife_command.config_source(:opt_with_default)).to eq (:cli)
+ end
+ it "correctly reports CLI as the source when a config entry comes from the CLI" do
+ knife_command = KnifeSpecs::TestYourself.new(["-D", "from-cli"])
+ knife_command.configure_chef
+ expect(knife_command.config_source(:opt_with_default)).to eq (:cli)
end
it "merges `listen` config to Chef::Config" do
- Chef::Knife.run(%w{test yourself --no-listen}, Chef::Application::Knife.options)
+ knife_command = Chef::Knife.run(%w{test yourself --no-listen}, Chef::Application::Knife.options)
expect(Chef::Config[:listen]).to be(false)
+ expect(knife_command.config_source(:listen)).to eq(:cli)
end
context "verbosity is one" do