Skip to content

Commit

Permalink
Fall back to ssh_options[:user] if host.user.nil?
Browse files Browse the repository at this point in the history
Up until now Airbrussh has displayed the username using `host.name`. However,
this may be `nil` in favor of using `ssh_options[:user]`. Use the SSH options
in this case.

If for some reason no user is specified with either method, then omit the `@`
symbol and just print the hostname instead of `@hostname`.

Addresses #65.
  • Loading branch information
mattbrictson committed Oct 30, 2015
1 parent 0ff3244 commit 1d662dc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Airbrussh is in a pre-1.0 state. This means that its APIs and behavior are subje
## [Unreleased]

* Your contribution here!
* Airbrussh now displays the correct user@host output in the following edge-cases:
* Inside an SSHKit `as(:user => "...")` block
* When a user is specified using `:ssh_options => { :user => "..." }` ([see #65](https://github.com/mattbrictson/airbrussh/issues/65))

## [0.7.0][] (2015-08-08)

Expand Down
6 changes: 3 additions & 3 deletions lib/airbrussh/command_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def exit_message(log_file=nil)
private

def user_at_host
user_str = user { host.user }
host_str = host.to_s
[user_str, host_str].join("@")
user_str = host.user || (host.ssh_options || {})[:user]
host_str = host.hostname
[user_str, host_str].compact.join("@")
end

def runtime
Expand Down
34 changes: 29 additions & 5 deletions test/airbrussh/command_formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

class Airbrussh::CommandFormatterTest < Minitest::Test
def setup
sshkit_command = OpenStruct.new(
:host => "12.34.56.78",
:user => "deployer",
@sshkit_command = OpenStruct.new(
:host => host("deployer", "12.34.56.78"),
:options => { :user => "override" },
:runtime => 1.23456,
:failure? => false
)
def sshkit_command.to_s
def @sshkit_command.to_s
"/usr/bin/env echo hello"
end
@command = Airbrussh::CommandFormatter.new(sshkit_command, 0)
@command = Airbrussh::CommandFormatter.new(@sshkit_command, 0)
end

def test_format_output
Expand All @@ -39,4 +39,28 @@ def test_exit_message_failure
@command.exit_message("out.log"))
end
end

def test_uses_ssh_options_if_host_user_is_absent
@sshkit_command.host = host(nil, "12.34.56.78", :user => "sshuser")
assert_equal(
"\e[0;32;49m✔ 01 sshuser@12.34.56.78\e[0m \e[0;90;49m1.235s\e[0m",
@command.exit_message)
end

def test_shows_hostname_only_if_no_user
@sshkit_command.host = host(nil, "12.34.56.78")
assert_equal(
"\e[0;32;49m✔ 01 12.34.56.78\e[0m \e[0;90;49m1.235s\e[0m",
@command.exit_message)
end

private

def host(user, hostname, ssh_options={})
SSHKit::Host.new(
:user => user,
:hostname => hostname,
:ssh_options => ssh_options
)
end
end

0 comments on commit 1d662dc

Please sign in to comment.