Class: MyPKI::Configuration

Inherits:
Hash
  • Object
show all
Includes:
Prompter
Defined in:
lib/mypki/configuration.rb

Defined Under Namespace

Modules: Loader

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Prompter

#file_prompt, #pass_prompt, #prompter

Constructor Details

#initialize(path, **options) ⇒ Configuration

Returns a new instance of Configuration.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mypki/configuration.rb', line 9

def initialize path, **options
  @options = options
  @path = File.expand_path path
  @loaders = loaders.map{|loader| loader.new options}
  
  if File.exist? @path        
    begin 
      merge! MultiJson.load(File.read(@path))
      @loaders.each {|l| l.load self}
    rescue => ex
      raise "Bad MyPKI configuration (#{ex.message})"
    end
  else      
    retriable on_retry: proc { warn $! } do
      clear 
      create
    end
  end
end

Class Attribute Details

.loadersObject

Returns the value of attribute loaders.



49
50
51
# File 'lib/mypki/configuration.rb', line 49

def loaders
  @loaders
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/mypki/configuration.rb', line 7

def options
  @options
end

Instance Method Details

#createObject



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

def create
  pki = file_prompt 'Path to your PKI: '
  
  @loaders.each do |loader| 
    loader.configure self, pki
    loader.load self unless options[:no_verify]
  end
  
  if Instance.cert.nil? and Instance.key.nil? and not options[:no_verify]
    fail 'Unknown PKI type - add an extension or try another file'
  end
  
  if File.writable? File.dirname(@path)
    File.write @path, MultiJson.dump(Hash[keys.zip(values)], :pretty => true)
  else
    warn "warning: #{File.dirname(@path)} is not writable! MyPKI will not save a configuration."
  end
end

#loadersObject



52
53
54
# File 'lib/mypki/configuration.rb', line 52

def loaders
  self.class.loaders ||= []
end