diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-09-06 22:25:00 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2016-09-06 22:25:00 -0700 |
commit | 1cf51d091db00d4e89c12b6ae0f6bcc98805e6b0 (patch) | |
tree | 5d151ebf70fac1944714d0fe2b26ca2d4a5961a4 | |
parent | 1782483ea52f347b0564fbdfb44b23693824d2e2 (diff) | |
download | chef-1cf51d091db00d4e89c12b6ae0f6bcc98805e6b0.tar.gz |
readability improvements to options parsing codelcg/knife-options-readability
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/knife.rb | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb index c8b344b5f1..2d64798bda 100644 --- a/lib/chef/knife.rb +++ b/lib/chef/knife.rb @@ -329,31 +329,29 @@ class Chef exit(1) end - # Returns a subset of the Chef::Config[:knife] Hash that is relevant to the - # currently executing knife command. This is used by #configure_chef to - # apply settings from knife.rb to the +config+ hash. + # keys from mixlib-cli options + def cli_keys + self.class.options.keys + end + + # extracts the settings from the Chef::Config[:knife] sub-hash that correspond + # to knife cli options -- in preparation for merging config values with cli values def config_file_settings - config_file_settings = {} - self.class.options.keys.each do |key| - config_file_settings[key] = Chef::Config[:knife][key] if Chef::Config[:knife].has_key?(key) + cli_keys.each_with_object({}) do |key, memo| + memo[key] = Chef::Config[:knife][key] if Chef::Config[:knife].has_key?(key) end - config_file_settings end - # Apply Config in this order: - # defaults from mixlib-cli - # settings from config file, via Chef::Config[:knife] - # config from command line + # config is merged in this order (inverse of precedence) + # default_config - mixlib-cli defaults (accessor from the mixin) + # config_file_settings - Chef::Config[:knife] sub-hash + # config - mixlib-cli settings (accessor from the mixin) def merge_configs - # Apply config file settings on top of mixlib-cli defaults - combined_config = default_config.merge(config_file_settings) - # Apply user-supplied options on top of the above combination - combined_config = combined_config.merge(config) - # replace the config hash from mixlib-cli with our own. - # Need to use the mutate-in-place #replace method instead of assigning to - # the instance variable because other code may have a reference to the - # original config hash object. - config.replace(combined_config) + # 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 # Catch-all method that does any massaging needed for various config |