Class: Cucumber::Chef::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/chef/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout = STDOUT, stderr = STDERR, stdin = STDIN) ⇒ Command

Returns a new instance of Command.



32
33
34
35
36
37
38
# File 'lib/cucumber/chef/command.rb', line 32

def initialize(stdout=STDOUT, stderr=STDERR, stdin=STDIN)
  @stdout, @stderr, @stdin = stdout, stderr, stdin
  @stdout.sync = true if @stdout.respond_to?(:sync=)

  @knife = (Cucumber::Chef.locate(:file, "bin", "knife") rescue nil)
  @knife = "/usr/bin/env knife" unless @knife
end

Instance Attribute Details

#stderrObject

Returns the value of attribute stderr.



28
29
30
# File 'lib/cucumber/chef/command.rb', line 28

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



28
29
30
# File 'lib/cucumber/chef/command.rb', line 28

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



28
29
30
# File 'lib/cucumber/chef/command.rb', line 28

def stdout
  @stdout
end

Instance Method Details

#knife(command, options = {}) ⇒ Object



66
67
68
69
# File 'lib/cucumber/chef/command.rb', line 66

def knife(command, options={})
  knife_rb = File.expand_path(File.join(Dir.pwd, ".cucumber-chef", "knife.rb"))
  run("#{@knife} #{command.flatten.compact.join(" ")} -c #{knife_rb}  --color -n", options)
end

#run(command, options = {}) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cucumber/chef/command.rb', line 42

def run(command, options={})
  options = { :exit_code => 0, :silence => false }.merge(options)
  exit_code = options[:exit_code]
  silence = options[:silence]
  $logger.debug { "options(#{options.inspect})" }

  command = "#{command} 2>&1"
  $logger.debug { "command(#{command})" }
  output = %x( #{command} )
  $logger.debug { "exit_code(#{$?})" }

  $logger.debug { "--------------------------------------------------------------------------------" }
  $logger.debug { output }
  $logger.debug { "--------------------------------------------------------------------------------" }

  @stdout.puts(output) if !silence

  raise CommandError, "run(#{command}) failed! [#{$?}]" if ($? != exit_code)

  output
end