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(config) ⇒ Commands

Returns a new instance of Commands.



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

def initialize(config)
  @base = config['settings']['command_base']
  @commands = config['commands']
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



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

def commands
  @commands
end

Instance Method Details

#build(name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/knife/helper/commands.rb', line 13

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']
      if c['options'].is_a?(Hash)
        c['options'].each do |k,v|
          cmd << " #{complete_option(k)}"
          cmd << " #{v}" if v
        end
      end
      break
    end
  end
  cmd
end

#complete_option(opt) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/knife/helper/commands.rb', line 36

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

#exec(name) ⇒ Object



32
33
34
# File 'lib/knife/helper/commands.rb', line 32

def exec(name)
  system(build(name))
end