module ChefConfig::Mixin::Credentials

def load_credentials(profile = nil)

def load_credentials(profile = nil)
  credentials_file = PathHelper.home(".chef", "credentials").freeze
  context_file = PathHelper.home(".chef", "context").freeze
  return unless File.file?(credentials_file)
  context = File.read(context_file) if File.file?(context_file)
  environment = ENV.fetch("CHEF_PROFILE", nil)
  profile = if !profile.nil?
              profile
            elsif !environment.nil?
              environment
            elsif !context.nil?
              context
            else
              "default"
            end
  config = Tomlrb.load_file(credentials_file)
  apply_credentials(config[profile], profile)
rescue ChefConfig::ConfigurationError
  raise
rescue => e
  # TOML's error messages are mostly rubbish, so we'll just give a generic one
  message = "Unable to parse Credentials file: #{credentials_file}\n"
  message << e.message
  raise ChefConfig::ConfigurationError, message
end