Class: Berkshelf::Config

Inherits:
Chozo::Config::JSON
  • Object
show all
Defined in:
lib/berkshelf/config.rb

Overview

Author:

Constant Summary collapse

FILENAME =
"config.json".freeze
KNIFE_LOCATIONS =

List taken from: wiki.opscode.com/display/chef/Chef+Configuration+Settings Listed in order of preferred preference

[
  './.chef/knife.rb',
  '~/.chef/knife.rb',
  '/etc/chef/solo.rb',
  '/etc/chef/client.rb'
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = self.class.path, options = {}) ⇒ Config

Returns a new instance of Config.

Parameters:

  • path (String) (defaults to: self.class.path)
  • options (Hash) (defaults to: {})

    @see Chozo::Config::JSON



93
94
95
# File 'lib/berkshelf/config.rb', line 93

def initialize(path = self.class.path, options = {})
  super(path, options)
end

Class Method Details

.chef_configChef::Config

Returns:

  • (Chef::Config)


54
55
56
57
58
59
60
61
# File 'lib/berkshelf/config.rb', line 54

def chef_config
  @chef_config ||= begin
    Chef::Config.from_file(File.expand_path(chef_config_path))
    Chef::Config
  rescue
    Chef::Config
  end
end

.chef_config_pathString?

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/berkshelf/config.rb', line 30

def chef_config_path
  @chef_config_path ||= begin
    possibles = KNIFE_LOCATIONS.dup

    unless ENV['BERKSHELF_CHEF_CONFIG'].nil?
      possibles.unshift(ENV['BERKSHELF_CHEF_CONFIG'])
    end

    location = possibles.find do |location|
      File.exists?(File.expand_path(location))
    end
    location ||= "~/.chef/knife.rb"

    File.expand_path(location)
  end
end

.chef_config_path=(value) ⇒ Object

Parameters:



48
49
50
51
# File 'lib/berkshelf/config.rb', line 48

def chef_config_path=(value)
  @chef_config = nil
  @chef_config_path = value
end

.fileString?

Returns the contents of the file.

Returns:

  • (String, nil)

    the contents of the file



65
66
67
# File 'lib/berkshelf/config.rb', line 65

def file
  File.read(path) if File.exists?(path)
end

.instanceConfig

Instantiate and return or just return the currently instantiated Berkshelf configuration

Returns:



73
74
75
76
77
78
79
# File 'lib/berkshelf/config.rb', line 73

def instance
  @instance ||= if file
    from_json file
  else
    new
  end
end

.pathString

Returns:



20
21
22
# File 'lib/berkshelf/config.rb', line 20

def path
  @path || File.join(Berkshelf.berkshelf_path, FILENAME)
end

.path=(new_path) ⇒ Object

Parameters:



25
26
27
# File 'lib/berkshelf/config.rb', line 25

def path=(new_path)
  @path = File.expand_path(new_path)
end

.reloadConfig

Reload the currently instantiated Berkshelf configuration

Returns:



84
85
86
87
# File 'lib/berkshelf/config.rb', line 84

def reload
  @instance = nil
  self.instance
end