summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Kantrowitz <noah@coderanger.net>2018-06-23 21:38:32 -0700
committerNoah Kantrowitz <noah@coderanger.net>2018-06-23 21:38:32 -0700
commitc3bda57fcef745b1c570a654e9a633dcf842b96f (patch)
tree8949c1a6cbe6a6873eac164bdf7a15e877e29460
parent36695a792c910feb9d834adb43faea372376cadf (diff)
downloadchef-c3bda57fcef745b1c570a654e9a633dcf842b96f.tar.gz
Add support for a $CHEF_HOME environment variable and improve support for the existing $KNIFE_HOME.
Signed-off-by: Noah Kantrowitz <noah@coderanger.net>
-rw-r--r--chef-config/lib/chef-config/path_helper.rb18
-rw-r--r--lib/chef/application/knife.rb1
2 files changed, 18 insertions, 1 deletions
diff --git a/chef-config/lib/chef-config/path_helper.rb b/chef-config/lib/chef-config/path_helper.rb
index 6341ffe4e6..fbb413578a 100644
--- a/chef-config/lib/chef-config/path_helper.rb
+++ b/chef-config/lib/chef-config/path_helper.rb
@@ -172,6 +172,18 @@ module ChefConfig
Pathname.new(cleanpath(to)).relative_path_from(Pathname.new(cleanpath(from)))
end
+ # Set the project-specific home directory environment variable.
+ #
+ # This can be used to allow per-tool home directory aliases like $KNIFE_HOME.
+ #
+ # @param [env_var] Key for an environment variable to use.
+ # @return [nil]
+ def self.per_tool_home_environment=(env_var)
+ @@per_tool_home_environment = env_var
+ # Reset this in case .home was already called.
+ @@home_dir = nil
+ end
+
# Retrieves the "home directory" of the current user while trying to ascertain the existence
# of said directory. The path returned uses / for all separators (the ruby standard format).
# If the home directory doesn't exist or an error is otherwise encountered, nil is returned.
@@ -185,7 +197,9 @@ module ChefConfig
# Home-path discovery is performed once. If a path is discovered, that value is memoized so
# that subsequent calls to home_dir don't bounce around.
#
- # See self.all_homes.
+ # @see all_homes
+ # @param args [Array<String>] Path components to look for under the home directory.
+ # @return [String]
def self.home(*args)
@@home_dir ||= all_homes { |p| break p }
if @@home_dir
@@ -203,6 +217,8 @@ module ChefConfig
# if no block is provided.
def self.all_homes(*args)
paths = []
+ paths << ENV[@@per_tool_home_environment] if defined?(@@per_tool_home_environment) && @@per_tool_home_environment && ENV[@@per_tool_home_environment]
+ paths << ENV["CHEF_HOME"] if ENV["CHEF_HOME"]
if ChefConfig.windows?
# By default, Ruby uses the the following environment variables to determine Dir.home:
# HOME
diff --git a/lib/chef/application/knife.rb b/lib/chef/application/knife.rb
index c972e9313e..fe0159873c 100644
--- a/lib/chef/application/knife.rb
+++ b/lib/chef/application/knife.rb
@@ -154,6 +154,7 @@ class Chef::Application::Knife < Chef::Application
# Run knife
def run
+ ChefConfig::PathHelper.per_tool_home_environment = "KNIFE_HOME"
Mixlib::Log::Formatter.show_time = false
validate_and_parse_options
quiet_traps