Class: Knife::Helper::Commands

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, commands, option_sets) ⇒ Commands

Returns a new instance of Commands.



9
10
11
12
13
# File 'lib/knife/helper/commands.rb', line 9

def initialize(base, commands, option_sets)
  @base = base
  @commands = commands
  @option_sets = option_sets
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



7
8
9
# File 'lib/knife/helper/commands.rb', line 7

def commands
  @commands
end

Instance Method Details

#build(name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/knife/helper/commands.rb', line 15

def build(name)
  cmd = ""
  @commands.each do |c|
    if c['name'] == name
      cmd = @base
      cmd << " #{c['command']}" if c.has_key?('command')
      cmd << " '#{c['condition']}'" if c.has_key?('condition') && c['condition']
      options = {}
      option_sets = {}
      c['option_sets'] = Array(c['option_sets']) if c['option_sets'].is_a?(String)
      if c['option_sets'].is_a?(Array)
        c['option_sets'].each do |opts|
          option_set(opts).each do |k,v|
            option_sets[k] = v
          end
        end
      end
      if c['options'].is_a?(Hash)
        c['options'].each do |k,v|
          options[k] = v
        end
      end
      options = Chef::Mixin::DeepMerge.deep_merge(options, option_sets)
      options.each do |k,v|
        cmd << " #{complete_option(k)}"
        cmd << " #{v}" if v
      end
      break
    end
  end
  cmd
end

#complete_option(opt) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/knife/helper/commands.rb', line 52

def complete_option(opt)
  if opt.length > 1
    "--#{opt}"
  else
    "-#{opt}"
  end
end

#exec(name) ⇒ Object



48
49
50
# File 'lib/knife/helper/commands.rb', line 48

def exec(name)
  raise "Helper exec finished with non-zero exit code" unless system(build(name))
end

#option_set(name) ⇒ Object



60
61
62
63
# File 'lib/knife/helper/commands.rb', line 60

def option_set(name)
  @option_sets.each {|opts| return opts['options'] if name == opts['name'] }
  Hash.new
end