Class: Knife::Helper::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
16
17
# File 'lib/knife/helper/config.rb', line 11

def initialize(file=nil)
  file ||= default_file
  conf = load_config(file)
  @settings = conf['settings']
  @option_sets = conf['option_sets']
  @commands = conf['commands']
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



9
10
11
# File 'lib/knife/helper/config.rb', line 9

def commands
  @commands
end

#option_setsObject (readonly)

Returns the value of attribute option_sets.



9
10
11
# File 'lib/knife/helper/config.rb', line 9

def option_sets
  @option_sets
end

#settingsObject (readonly)

Returns the value of attribute settings.



9
10
11
# File 'lib/knife/helper/config.rb', line 9

def settings
  @settings
end

Instance Method Details

#default_fileObject



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

def default_file
  ::File.join(Dir.pwd, ".knife.helper.yml")
end

#get_content(str) ⇒ Object



42
43
44
# File 'lib/knife/helper/config.rb', line 42

def get_content(str)
  ::ERB.new(str).result
end

#load_config(file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/knife/helper/config.rb', line 19

def load_config(file)
  conf = load_yml(get_content(read_file(file)))
  Array(conf['includes']).each do |inc|
    inc_path = nil
    rel_path = File.expand_path("../#{inc}", file)
    [inc, rel_path].each {|f| inc_path = f if File.exists?(f) }
    raise "'#{inc}' is not exists." if inc_path.nil?
    conf = Chef::Mixin::DeepMerge.deep_merge(
      load_yml(get_content(read_file(inc_path))),
      conf
    )
  end
  conf
end

#load_yml(str) ⇒ Object



46
47
48
# File 'lib/knife/helper/config.rb', line 46

def load_yml(str)
  ::SafeYAML.load(str) || Hash.new
end

#read_file(file) ⇒ Object



38
39
40
# File 'lib/knife/helper/config.rb', line 38

def read_file(file)
  ::File.exist?(file.to_s) ? IO.read(file) : ""
end