Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text-only formatter option #120

Merged
merged 1 commit into from
Apr 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/sshkit/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

require_relative 'formatters/abstract'
require_relative 'formatters/black_hole'
require_relative 'formatters/simple_text'
require_relative 'formatters/pretty'
require_relative 'formatters/dot'

Expand Down
58 changes: 58 additions & 0 deletions lib/sshkit/formatters/simple_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

module SSHKit

module Formatter

class SimpleText < Abstract

def write(obj)
return if obj.verbosity < SSHKit.config.output_verbosity
case obj
when SSHKit::Command then write_command(obj)
when SSHKit::LogMessage then write_log_message(obj)
else
original_output << "Output formatter doesn't know how to handle #{obj.class}\n"
end
end
alias :<< :write

private

def write_command(command)
unless command.started?
original_output << "Running #{String(command)} on #{command.host.to_s}\n"
if SSHKit.config.output_verbosity == Logger::DEBUG
original_output << "Command: #{command.to_command}" + "\n"
end
end

if SSHKit.config.output_verbosity == Logger::DEBUG
unless command.stdout.empty?
command.stdout.lines.each do |line|
original_output << "\t" + line
original_output << "\n" unless line[-1] == "\n"
end
end

unless command.stderr.empty?
command.stderr.lines.each do |line|
original_output << "\t" + line
original_output << "\n" unless line[-1] == "\n"
end
end
end

if command.finished?
original_output << "Finished in #{sprintf('%5.3f seconds', command.runtime)} with exit status #{command.exit_status} (#{ command.failure? ? 'failed' : 'successful' }).\n"
end
end

def write_log_message(log_message)
original_output << log_message.to_s + "\n"
end

end

end

end