-
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.
Compatibility with the new SSHKit::Command API
An upcoming version of SSHKit changes its API for accessing stdout and stderr data. This commit introduces a facade to normalize the API differences in the various SSHKit versions, so both the new and old APIs can be supported. Also, since SSHKit now `clear`s stdout/stderr data instead of assigning a new blank value, our old technique of a simple `Command#dup` does not protect us from mutations to the output strings. Use `Marshal` to do a deep copy instead.
- Loading branch information
1 parent
e5236dc
commit ec3122b
Showing
3 changed files
with
51 additions
and
13 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
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 @@ | ||
module Airbrussh | ||
# A facade that provides access to stdout and stderr command output of | ||
# sshkit commands. This is needed to normalize the API differences in | ||
# various sshkit versions. | ||
class CommandOutput | ||
def self.for(command) | ||
if command.respond_to?(:clear_stdout_lines) | ||
Modern.new(command) | ||
else | ||
Legacy.new(command) | ||
end | ||
end | ||
|
||
attr_reader :command | ||
|
||
def initialize(command) | ||
@command = command | ||
end | ||
end | ||
|
||
class Legacy < CommandOutput | ||
# The stderr/stdout methods provided by the command object have the current | ||
# "chunk" as received over the wire. Since there may be more chunks | ||
# appended and we don't want to print duplicates, clear the current data. | ||
def each_line(stream, &block) | ||
output = command.public_send(stream) | ||
return if output.empty? | ||
output.lines.to_a.each(&block) | ||
command.public_send("#{stream}=", "") | ||
end | ||
end | ||
|
||
class Modern < CommandOutput | ||
# Newer versions of sshkit take care of clearing the output with the | ||
# clear_stdout_lines/clear_stderr_lines methods. | ||
def each_line(stream, &block) | ||
lines = command.public_send("clear_#{stream}_lines") | ||
return if lines.join.empty? | ||
lines.each(&block) | ||
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