From dfdf2ce2c4c62dd11e10dfb9cfa570634276261b Mon Sep 17 00:00:00 2001 From: "M. Simon Borg" Date: Tue, 25 Jul 2017 12:32:50 -0400 Subject: [PATCH] fix refactoring bugs --- lib/knife/api.rb | 55 ++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/lib/knife/api.rb b/lib/knife/api.rb index edc9c0a..8f2da6f 100644 --- a/lib/knife/api.rb +++ b/lib/knife/api.rb @@ -30,48 +30,43 @@ def knife(command, args = []) end def knife_capture(command, args = [], input = nil) - @input = input - set_null + null = set_null - assign_io_channels + assign_io_channels(input, null) status = Chef::Knife::API::Support.run_knife(command, args) return STDOUT.string, STDERR.string, status ensure - revert_io_channels + revert_io_channels(null) end end - end - def set_null - if Gem.win_platform? == null - File.open('NUL:', 'r') - else - File.open('/dev/null', 'r') + def set_null + Gem.win_platform? ? File.open('NUL:', 'r') : File.open('/dev/null', 'r') end - end - def assign_io_channels - @warn = $VERBOSE - $VERBOSE = nil - @stderr = STDERR - @stdout = STDOUT - @stdin = STDIN + def assign_io_channels(input, null) + @warn = $VERBOSE + $VERBOSE = nil + @stderr = STDERR + @stdout = STDOUT + @stdin = 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 - end + 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 + end - def revert_io_channels - @warn = $VERBOSE - $VERBOSE = nil - Object.const_set('STDERR', @stderr) - Object.const_set('STDOUT', @stdout) - Object.const_set('STDIN', @stdin) - $VERBOSE = @warn - null.close + def revert_io_channels(null) + @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 end