summaryrefslogtreecommitdiff
path: root/chef-config/lib/chef-config/config.rb
diff options
context:
space:
mode:
authordanielsdeleo <dan@chef.io>2016-10-18 15:39:15 -0700
committerdanielsdeleo <dan@chef.io>2016-10-18 16:13:14 -0700
commit466bfbf2db3c02c0f5263f0e93e12baf1de69832 (patch)
treefe21e3d81554bcfb30b50033ab9d5921f69c5355 /chef-config/lib/chef-config/config.rb
parent488dc6137ec3ce77e35ffd865fb1758c8403ad09 (diff)
downloadchef-466bfbf2db3c02c0f5263f0e93e12baf1de69832.tar.gz
Implement `--config-option` CLI opt for knifedan/config-option-in-knife
The `--config-option` CLI option allows setting any `Chef::Config` setting on the command line, which allows applications to be used without a configuration file even if a desired config option does not have a corresponding CLI option. This CLI option was present in the help output for knife but was not actually implemented. Moving the behavior into chef-config allows us to use it for both `chef-client` and `knife`. Signed-off-by: Daniel DeLeo <dan@chef.io>
Diffstat (limited to 'chef-config/lib/chef-config/config.rb')
-rw-r--r--chef-config/lib/chef-config/config.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb
index f2db54aa17..d0fa87a5ad 100644
--- a/chef-config/lib/chef-config/config.rb
+++ b/chef-config/lib/chef-config/config.rb
@@ -32,6 +32,7 @@ require "mixlib/shellout"
require "uri"
require "addressable/uri"
require "openssl"
+require "yaml"
module ChefConfig
@@ -70,6 +71,25 @@ module ChefConfig
event_handlers << logger
end
+ def self.apply_extra_config_options(extra_config_options)
+ if extra_config_options
+ extra_parsed_options = extra_config_options.inject({}) do |memo, option|
+ # Sanity check value.
+ if option.empty? || !option.include?("=")
+ raise UnparsableConfigOption, "Unparsable config option #{option.inspect}"
+ end
+ # Split including whitespace if someone does truly odd like
+ # --config-option "foo = bar"
+ key, value = option.split(/\s*=\s*/, 2)
+ # Call to_sym because Chef::Config expects only symbol keys. Also
+ # runs a simple parse on the string for some common types.
+ memo[key.to_sym] = YAML.safe_load(value)
+ memo
+ end
+ merge!(extra_parsed_options)
+ end
+ end
+
# Config file to load (client.rb, knife.rb, etc. defaults set differently in knife, chef-client, etc.)
configurable(:config_file)