Skip to content

Commit fa5423d

Browse files
committed
fixing chef-cli env
Signed-off-by: nikhil2611 <[email protected]>
1 parent ce5a35a commit fa5423d

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

lib/chef-cli/helpers.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,28 @@ def git_windows_bin_dir
155155
# environment vars for habitat
156156
#
157157
def habitat_env
158-
@habitat_env ||= begin
159-
# Determine package name based on habitat_chef_dke?
160-
bin_pkg_name = habitat_chef_dke? ? ChefCLI::Dist::CHEF_DKE_PKG_NAME : ChefCLI::Dist::HAB_PKG_NAME
158+
@habitat_env ||=
159+
begin
160+
# Check if the Chef DKE package exists
161+
dke_pkg_prefix = get_pkg_prefix(ChefCLI::Dist::CHEF_DKE_PKG_NAME)
161162

162-
# Get versioned package if CHEF_CLI_VERSION is set, otherwise fallback to default package
163+
# Check for a versioned package if the environment variable is set
163164
versioned_pkg_prefix = fetch_chef_cli_version_pkg
164-
bin_pkg_prefix = versioned_pkg_prefix || get_pkg_prefix(bin_pkg_name)
165165

166-
# Get vendor package path, falling back to base package if needed
167-
vendor_pkg_prefix = versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
168-
# Print warning only if CHEF_CLI_VERSION is set but package isn't found
166+
# Print warning **only if CHEF_CLI_VERSION is set but package isn't found**
169167
if ENV["CHEF_CLI_VERSION"] && !versioned_pkg_prefix
170168
ChefCLI::UI.new.msg("Warning: Habitat package '#{ChefCLI::Dist::HAB_PKG_NAME}' with version '#{ENV["CHEF_CLI_VERSION"]}' not found.")
171169
end
172170

171+
# Determine the correct bin_pkg_prefix:
172+
# - If DKE package exists, use it.
173+
# - Otherwise, if versioned package exists, use that.
174+
# - Otherwise, fall back to HAB_PKG_NAME.
175+
bin_pkg_prefix = dke_pkg_prefix || versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
176+
177+
# Get vendor package path, falling back to base package if needed
178+
vendor_pkg_prefix = versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
179+
173180
# Set vendor_dir only if it exists
174181
vendor_dir = File.join(vendor_pkg_prefix, "vendor") if vendor_pkg_prefix && Dir.exist?(File.join(vendor_pkg_prefix, "vendor"))
175182

@@ -209,7 +216,9 @@ def omnibus_env
209216

210217
def get_pkg_prefix(pkg_name)
211218
path = `hab pkg path #{pkg_name} 2>/dev/null`.strip
212-
path if $?.success? && Dir.exist?(path)
219+
return nil if path.empty? || !Dir.exist?(path)
220+
221+
path
213222
end
214223

215224
def omnibus_expand_path(*paths)

0 commit comments

Comments
 (0)