From c3bda57fcef745b1c570a654e9a633dcf842b96f Mon Sep 17 00:00:00 2001 From: Noah Kantrowitz Date: Sat, 23 Jun 2018 21:38:32 -0700 Subject: Add support for a $CHEF_HOME environment variable and improve support for the existing $KNIFE_HOME. Signed-off-by: Noah Kantrowitz --- chef-config/lib/chef-config/path_helper.rb | 18 +++++++++++++++++- lib/chef/application/knife.rb | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) 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] 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 -- cgit v1.2.1