Class: Berkshelf::Config

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

Defined Under Namespace

Modules: BerksConfig

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = self.class.path) ⇒ Config

Returns a new instance of Config.

Parameters:

  • path (String) (defaults to: self.class.path)


98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/berkshelf/config.rb', line 98

def initialize(path = self.class.path)
  # this is a bit tricky, mixlib-config wants to extend a class and create effectively a global config object while
  # what we want to do is use an instance, so we create an anonymous class and shove it into an instance variable.
  # this is actually similar to what mixlib-config itself does to create config contexts.
  @klass = Class.new
  @klass.extend(Mixlib::Config)
  @klass.extend(BerksConfig)

  @path = File.expand_path(path)
  @klass.from_file(@path) if File.exist?(@path)
  # yeah, if !File.exist?() you just get back an empty config object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



111
112
113
# File 'lib/berkshelf/config.rb', line 111

def method_missing(method, *args, &block)
  @klass.send(method, *args, &block)
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



95
96
97
# File 'lib/berkshelf/config.rb', line 95

def path
  @path
end

Class Method Details

.coerce_sslConfig

force proper X509 types from any configuration strings

Returns:



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

def coerce_ssl
  ssl = @instance[:ssl]
  ssl[:ca_cert] = OpenSSL::X509::Certificate.new(File.read(ssl[:ca_cert])) if ssl[:ca_cert] && ssl[:ca_cert].is_a?(String)
  ssl[:client_cert] = OpenSSL::X509::Certificate.new(File.read(ssl[:client_cert])) if ssl[:client_cert] && ssl[:client_cert].is_a?(String)
  ssl[:client_key] = OpenSSL::PKey::RSA.new(File.read(ssl[:client_key])) if ssl[:client_key] && ssl[:client_key].is_a?(String)
  @instance
end

.fileString?

Returns the contents of the file.

Returns:

  • (String, nil)

    the contents of the file



45
46
47
# File 'lib/berkshelf/config.rb', line 45

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

.from_file(path) ⇒ Object



82
83
84
# File 'lib/berkshelf/config.rb', line 82

def from_file(path)
  new(path)
end

.from_hash(hash) ⇒ Object



90
91
92
# File 'lib/berkshelf/config.rb', line 90

def from_hash(hash)
  new.from_hash(hash)
end

.from_json(json) ⇒ Object



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

def from_json(json)
  new.from_json(json)
end

.instanceConfig

Instantiate and return or just return the currently instantiated Berkshelf configuration

Returns:



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

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

.local_locationString

Returns:



23
24
25
# File 'lib/berkshelf/config.rb', line 23

def local_location
  ENV["BERKSHELF_CONFIG"] || File.join(".", ".berkshelf", "config.json")
end

.pathString

Returns:



28
29
30
31
# File 'lib/berkshelf/config.rb', line 28

def path
  path = File.exist?(local_location) ? local_location : store_location
  File.expand_path(path)
end

.reloadConfig

Reload the currently instantiated Berkshelf configuration

Returns:



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

def reload
  @instance = nil
  instance
end

.set_config(config) ⇒ Object

Parameters:



34
35
36
# File 'lib/berkshelf/config.rb', line 34

def set_config(config)
  @instance = config
end

.set_path(new_path) ⇒ Object

Parameters:



39
40
41
# File 'lib/berkshelf/config.rb', line 39

def set_path(new_path)
  @instance = nil
end

.store_locationString

Returns:



18
19
20
# File 'lib/berkshelf/config.rb', line 18

def store_location
  File.join(Berkshelf.berkshelf_path, "config.json")
end