-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move most formatting into CommandFormatter class
- Loading branch information
1 parent
8809aa2
commit 743e5b6
Showing
3 changed files
with
136 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require "airbrussh/colors" | ||
# rubocop:disable Style/AsciiComments | ||
|
||
module Airbrussh | ||
# Decorates an SSHKit::Command to add string output helpers. | ||
class CommandFormatter < SimpleDelegator | ||
include Airbrussh::Colors | ||
|
||
# Prefixes the line with the command number and removes the newline. | ||
# | ||
# format_output("hello\n") # => "01 hello" | ||
# | ||
def format_output(line) | ||
"#{number} #{line.chomp}" | ||
end | ||
|
||
# Returns the abbreviated command (in yellow) with the number prefix. | ||
# | ||
# start_message # => "01 echo hello" | ||
# | ||
def start_message | ||
"#{number} #{yellow(abbreviated)}" | ||
end | ||
|
||
# Returns a green (success) or red (failure) message depending on the | ||
# exit status. | ||
# | ||
# exit_message # => "✔ 01 user@host 0.084s" | ||
# exit_message # => "✘ 01 user@host 0.084s" | ||
# | ||
# If `log_file` is specified, it is appended to the message | ||
# in the failure case. | ||
# | ||
# exit_message("out.log") | ||
# # => "✘ 01 user@host (see out.log for details) 0.084s" | ||
# | ||
def exit_message(log_file=nil) | ||
if failure? | ||
message = red(failure_message(log_file)) | ||
else | ||
message = green(success_message) | ||
end | ||
message << " #{gray(runtime)}" | ||
end | ||
|
||
private | ||
|
||
def user_at_host | ||
user_str = user { host.user } | ||
host_str = host.to_s | ||
[user_str, host_str].join("@") | ||
end | ||
|
||
def runtime | ||
format("%5.3fs", super) | ||
end | ||
|
||
def abbreviated | ||
to_s.sub(%r{^/usr/bin/env }, "") | ||
end | ||
|
||
def number | ||
format("%02d", position + 1) | ||
end | ||
|
||
def success_message | ||
"✔ #{number} #{user_at_host}" | ||
end | ||
|
||
def failure_message(log_file) | ||
message = "✘ #{number} #{user_at_host}" | ||
message << " (see #{log_file} for details)" if log_file | ||
message | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require "minitest_helper" | ||
require "ostruct" | ||
require "airbrussh/command_formatter" | ||
|
||
class Airbrussh::CommandFormatterTest < Minitest::Test | ||
def setup | ||
sshkit_command = OpenStruct.new( | ||
:host => "12.34.56.78", | ||
:user => "deployer", | ||
:runtime => 1.23456, | ||
:position => 0, | ||
:failure? => false | ||
) | ||
def sshkit_command.to_s | ||
"/usr/bin/env echo hello" | ||
end | ||
@command = Airbrussh::CommandFormatter.new(sshkit_command) | ||
end | ||
|
||
def test_format_output | ||
assert_equal("01 hello", @command.format_output("hello\n")) | ||
end | ||
|
||
def test_start_message | ||
assert_equal("01 \e[0;33;49mecho hello\e[0m", @command.start_message) | ||
end | ||
|
||
def test_exit_message_success | ||
assert_equal( | ||
"\e[0;32;49m✔ 01 [email protected]\e[0m \e[0;90;49m1.235s\e[0m", | ||
@command.exit_message) | ||
end | ||
|
||
def test_exit_message_failure | ||
@command.stub(:failure?, true) do | ||
assert_equal( | ||
"\e[0;31;49m✘ 01 [email protected] (see out.log for details)\e[0m "\ | ||
"\e[0;90;49m1.235s\e[0m", | ||
@command.exit_message("out.log")) | ||
end | ||
end | ||
end |