Skip to content

Commit

Permalink
Extracted common behaviour from SSHStdinAdapter and LocalStdinAdapter…
Browse files Browse the repository at this point in the history
… to ExternalProcessAdapter class
  • Loading branch information
Nu-hin committed Aug 30, 2018
1 parent df1f2ac commit 29bb715
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
3 changes: 2 additions & 1 deletion lib/remote_ruby/connection_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def open(_code)
end

require 'remote_ruby/connection_adapter/eval_adapter.rb'
require 'remote_ruby/connection_adapter/local_stdin_adapter'
require 'remote_ruby/connection_adapter/external_process_adapter'
require 'remote_ruby/connection_adapter/ssh_stdin_adapter'
require 'remote_ruby/connection_adapter/local_stdin_adapter'
require 'remote_ruby/connection_adapter/cache_adapter'
require 'remote_ruby/connection_adapter/caching_adapter'
31 changes: 31 additions & 0 deletions lib/remote_ruby/connection_adapter/external_process_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'open3'

module RemoteRuby
# Base class for adapters which launch an external process to execute
# Ruby code.
class ExternalProcessAdapter < ::RemoteRuby::ConnectionAdapter
include Open3

# Command to run an external process. Override in a child class.
def command
raise NotImplementedError
end

def open(code)
result = nil

popen3(command) do |stdin, stdout, stderr, wait_thr|
stdin.write(code)
stdin.close

yield stdout, stderr

result = wait_thr.value
end

return if result.success?

raise "Remote connection exited with code #{result}"
end
end
end
19 changes: 4 additions & 15 deletions lib/remote_ruby/connection_adapter/local_stdin_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
require 'open3'

module RemoteRuby
# An adapter to expecute Ruby code on the local macine
# inside a specified directory
class LocalStdinAdapter < ConnectionAdapter
include Open3
class LocalStdinAdapter < ::RemoteRuby::ExternalProcessAdapter
attr_reader :working_dir

def initialize(working_dir: '.')
Expand All @@ -15,18 +12,10 @@ def connection_name
working_dir
end

def open(code)
result = nil

popen3('ruby', chdir: working_dir) do |stdin, stdout, stderr, wait_thr|
stdin.write(code)
stdin.close
yield stdout, stderr
result = wait_thr.value
end
private

return if result.success?
raise "Remote connection exited with code #{result}"
def command
"cd \"#{working_dir}\" && ruby"
end
end
end
23 changes: 1 addition & 22 deletions lib/remote_ruby/connection_adapter/ssh_stdin_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require 'open3'

module RemoteRuby
# An adapter to execute Ruby code on the remote server via SSH
class SSHStdinAdapter < ConnectionAdapter
include Open3

class SSHStdinAdapter < ExternalProcessAdapter
attr_reader :server, :working_dir, :user, :key_file

def initialize(server:, working_dir: '~', user: nil, key_file: nil)
Expand All @@ -18,23 +14,6 @@ def connection_name
"#{server}:#{working_dir}"
end

def open(code)
result = nil

popen3(command) do |stdin, stdout, stderr, wait_thr|
stdin.write(code)
stdin.close

yield stdout, stderr

result = wait_thr.value
end

return if result.success?

raise "Remote connection exited with code #{result}"
end

private

def command
Expand Down

0 comments on commit 29bb715

Please sign in to comment.