summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc A. Paradise <marc.paradise@gmail.com>2019-05-01 10:22:24 -0400
committerMarc A. Paradise <marc.paradise@gmail.com>2019-05-03 17:30:08 -0400
commit3e3d4b1ac9c2aefd12eae60982eb321c1029ab44 (patch)
tree3a39230b475b007837d41f2f31c203cef6287e98
parentba50ae6d20ef86ab39b9fa2eee982f929cd70c7e (diff)
downloadchef-3e3d4b1ac9c2aefd12eae60982eb321c1029ab44.tar.gz
Make config_source aware of cli defaults
If a value comes from CLI defaults, it will now return :cli_default. This supports verifying deprecations when we need to know if a value was actually supplied from the CLI, or if it was defaulted from CLI options. Since we have all of the original sources still available, this also makes it so that we don't keep a separate hash for tracking config source - we're just checking in the original config sources in order that matches merge priority. Signed-off-by: Marc A. Paradise <marc.paradise@gmail.com>
-rw-r--r--lib/chef/knife.rb25
-rw-r--r--spec/unit/knife_spec.rb21
2 files changed, 21 insertions, 25 deletions
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb
index da9c1ab1e9..4cd7cdb73c 100644
--- a/lib/chef/knife.rb
+++ b/lib/chef/knife.rb
@@ -344,13 +344,8 @@ 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|
- 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]
end
end
@@ -361,16 +356,28 @@ class Chef
# config_file_settings - Chef::Config[:knife] sub-hash
# config - mixlib-cli settings (accessor from the mixin)
def merge_configs
+ # This is the config after user CLI options have been evaluated, and it contains only
+ # user-supplied values.
+ @original_config = config.dup
# 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))
end
- # Return where a config key has been sourced,
- # :cli, :config, or nil if the key is not set.
+ #
+ # Determine the source of a given configuration key
+ #
+ # @argument key [Symbol] a configuration key
+ # @return [Symbol,NilClass] return the source of the config key,
+ # one of:
+ # - :cli - this was explicitly provided on the CLI
+ # - :config - this came from Chef::Config[:knife]
+ # - :cli_default - came from a declared CLI `option`'s `default` value.
+ # - nil - if they key does not exist
def config_source(key)
- return :cli if @key_sources[:cli].include? key
- return :config if @key_sources[:config].include? key
+ return :cli if @original_config.include? key
+ return :config if config_file_settings.key? key
+ return :cli_default if default_config.include? key
nil
end
diff --git a/spec/unit/knife_spec.rb b/spec/unit/knife_spec.rb
index 7e05bec8f7..6fcb831531 100644
--- a/spec/unit/knife_spec.rb
+++ b/spec/unit/knife_spec.rb
@@ -299,37 +299,26 @@ 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 and reports the source as default" do
+ it "prefers the default value from option definition 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")
+ expect(knife_command.config_source(:opt_with_default)).to eq(:cli_default)
end
- it "prefers a value in Chef::Config[:knife] to the default" do
+ it "prefers a value in Chef::Config[:knife] to the default and reports the source as config" 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[:opt_with_default]).to eq("from-knife-config")
- expect(knife_command.config_source(:opt_with_default)).to eq (:config)
- end
-
- 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)
+ 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)
+ expect(knife_command.config_source(:opt_with_default)).to eq(:cli)
end
it "merges `listen` config to Chef::Config" do