Module: ChefConfig::Mixin::Credentials

Included in:
WorkstationConfigLoader
Defined in:
lib/chef-config/mixin/credentials.rb

Instance Method Summary collapse

Instance Method Details

#load_credentials(profile = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef-config/mixin/credentials.rb', line 25

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