Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
erikh committed Oct 29, 2012
0 parents commit dba1164
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Rakefile.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'knife/dsl'

task :list_nodes do
knife :node_list
end
59 changes: 59 additions & 0 deletions lib/knife/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'chef/application/knife'
require 'stringio'

module Chef::Knife::DSL
module Chef::Knife::DSL::Support
def self.run_knife(command, args)
unless command.kind_of?(Array)
command = command.to_s.split(/[\s_]+/)
end

command += args

if ENV["CHEF_CONFIG"]
command += ['-c', ENV["CHEF_CONFIG"]]
end

opts = Chef::Application::Knife.new.options
Chef::Knife.run(command, opts)
end
end

def knife(command, args=[])
Chef::Knife::DSL::Support.run_knife(command, args)
end

def knife_capture(command, args=[], input=nil)
null = Gem.win_platform? ? File.open('NUL:', 'r') : File.open('/dev/null', 'r')

warn = $VERBOSE
$VERBOSE = nil
stderr, stdout, stdin = STDERR, STDOUT, STDIN

Object.const_set("STDERR", StringIO.new('', 'r+'))
Object.const_set("STDOUT", StringIO.new('', 'r+'))
Object.const_set("STDIN", input ? StringIO.new(input, 'r') : null)
$VERBOSE = warn

Chef::Knife::DSL::Support.run_knife(command, args)
return STDOUT.string, STDERR.string
ensure
warn = $VERBOSE
$VERBOSE = nil
Object.const_set("STDERR", stderr)
Object.const_set("STDOUT", stdout)
Object.const_set("STDIN", stdin)
$VERBOSE = warn
null.close
end
end

class << eval("self", TOPLEVEL_BINDING)
include Chef::Knife::DSL
end

if defined? Rake::DSL
module Rake::DSL
include Chef::Knife::DSL
end
end

0 comments on commit dba1164

Please sign in to comment.