lib/ztk/command.rb in ztk-1.0.0.rc.1 vs lib/ztk/command.rb in ztk-1.0.0

- old
+ new

@@ -21,11 +21,10 @@ require "timeout" module ZTK # ZTK::Command Error Class - # # @author Zachary Patten <zachary@jovelabs.net> class CommandError < Error; end # Command Execution Class # @@ -34,11 +33,12 @@ # cmd = ZTK::Command.new # # If we wanted to redirect STDOUT and STDERR to a StringIO we can do this: # # std_combo = StringIO.new - # cmd = ZTK::Command.new(:stdout => std_combo, :stderr => std_combo) + # ui = ZTK::UI.new(:stdout => std_combo, :stderr => std_combo) + # cmd = ZTK::Command.new(:ui => ui) # # @author Zachary Patten <zachary@jovelabs.net> class Command < ZTK::Base def initialize(configuration={}) @@ -60,10 +60,11 @@ # # @example Execute a command: # # cmd = ZTK::Command.new # puts cmd.exec("hostname -f").inspect + # def exec(command, options={}) options = OpenStruct.new({ :exit_code => 0, :silence => false }.merge(options)) config.ui.logger.debug { "config=#{config.send(:table).inspect}" } config.ui.logger.debug { "options=#{options.send(:table).inspect}" } @@ -173,14 +174,18 @@ end private + # Returns a string in the format of "user@hostname" for the current + # shell. def tag @hostname ||= %x(hostname -f).chomp "#{ENV['USER']}@#{@hostname}" end + # Formats a header suitable for writing to the direct logger when logging + # sessions. def log_header(what) count = 8 sep = ("=" * count) header = [sep, "[ #{what} ]", sep, "[ #{tag} ]", sep, "[ #{what} ]", sep].join "#{header}\n"