diff --git a/lib/remote_ruby/connection_adapter.rb b/lib/remote_ruby/connection_adapter.rb index 7dc18c7..1b3cf1d 100644 --- a/lib/remote_ruby/connection_adapter.rb +++ b/lib/remote_ruby/connection_adapter.rb @@ -13,6 +13,10 @@ def initialize(**args); end def open(_code) raise NotImplementedError end + + def connection_name + "#{self.class.name} " + end end end diff --git a/lib/remote_ruby/connection_adapter/cache_adapter.rb b/lib/remote_ruby/connection_adapter/cache_adapter.rb index 84ddfdc..a0f59b6 100644 --- a/lib/remote_ruby/connection_adapter/cache_adapter.rb +++ b/lib/remote_ruby/connection_adapter/cache_adapter.rb @@ -4,9 +4,12 @@ module RemoteRuby # An adapter which takes stdout and stderr from files and ignores # all stdin. Only used to read from cache. class CacheAdapter < ConnectionAdapter - def initialize(cache_path:) + attr_reader :connection_name + + def initialize(cache_path:, connection_name:) super @cache_path = cache_path + @connection_name = connection_name end def open(_code) diff --git a/lib/remote_ruby/connection_adapter/caching_adapter.rb b/lib/remote_ruby/connection_adapter/caching_adapter.rb index 3647505..561a928 100644 --- a/lib/remote_ruby/connection_adapter/caching_adapter.rb +++ b/lib/remote_ruby/connection_adapter/caching_adapter.rb @@ -29,6 +29,10 @@ def with_result_stream end end + def connection_name + adapter.connection_name + end + private attr_reader :cache_path, :adapter diff --git a/lib/remote_ruby/connection_adapter/ssh_adapter.rb b/lib/remote_ruby/connection_adapter/ssh_adapter.rb index d970e5d..8bc4feb 100644 --- a/lib/remote_ruby/connection_adapter/ssh_adapter.rb +++ b/lib/remote_ruby/connection_adapter/ssh_adapter.rb @@ -21,7 +21,7 @@ def open(code) t = Thread.new do ssh.open_channel do |channel| - channel.exec("cd #{working_dir} && ruby #{fname}") do |ch, success| + channel.exec("cd '#{working_dir}' && ruby #{fname}") do |ch, success| raise 'Could not execute command' unless success ssh.listen_to(stdin_r) do |io| @@ -60,6 +60,10 @@ def with_result_stream yield StringIO.new(@result) end + def connection_name + "#{user}@#{host}:#{working_dir || '~'}> " + end + def with_temp_file(code, ssh) fname = '' code_channel = ssh.open_channel do |channel| diff --git a/lib/remote_ruby/connection_adapter/text_mode_adapter.rb b/lib/remote_ruby/connection_adapter/text_mode_adapter.rb index e73ab55..2e30254 100644 --- a/lib/remote_ruby/connection_adapter/text_mode_adapter.rb +++ b/lib/remote_ruby/connection_adapter/text_mode_adapter.rb @@ -3,11 +3,18 @@ module RemoteRuby class TextModeAdapter < ConnectionAdapter + DEFAULT_SETTINGS = { + stdout_mode: { color: :green, mode: :italic }, + stderr_mode: { color: :red, mode: :italic }, + cache_mode: { color: :blue, mode: :bold }, + cache_prefix: '[C] ' + } + attr_reader :adapter, :stdout_prefix, :stderr_prefix, :cache_prefix, :cache_used, :stdout_mode, :stderr_mode, :cache_mode def initialize( - adapter:, + adapter, stdout_prefix:, stderr_prefix:, cache_prefix:, @@ -16,7 +23,7 @@ def initialize( stderr_mode:, cache_mode: ) - super + super() @adapter = adapter @stdout_prefix = stdout_prefix @stderr_prefix = stderr_prefix diff --git a/lib/remote_ruby/execution_context.rb b/lib/remote_ruby/execution_context.rb index 2fed083..eb87452 100644 --- a/lib/remote_ruby/execution_context.rb +++ b/lib/remote_ruby/execution_context.rb @@ -24,6 +24,7 @@ def initialize(**params) @out_stream = params.delete(:out_stream) || $stdout @err_stream = params.delete(:err_stream) || $stderr @adapter_klass = params.delete(:adapter) || ::RemoteRuby::SSHAdapter + @text_mode = params.delete(:text_mode) || nil @params = params FileUtils.mkdir_p(@cache_dir) @@ -44,7 +45,7 @@ def execute(locals = nil, &block) private attr_reader :params, :adapter_klass, :use_cache, :save_cache, :cache_dir, - :in_stream, :out_stream, :err_stream, :flavours + :in_stream, :out_stream, :err_stream, :flavours, :text_mode def assign_locals(local_names, values, block) local_names.each do |local| @@ -107,20 +108,39 @@ def execute_code(ruby_code, client_locals = {}) end def adapter(code_hash) - actual_adapter = adapter_klass.new(**params) - - if use_cache && cache_exists?(code_hash) - cache_adapter(code_hash) - elsif save_cache - caching_adapter(actual_adapter, code_hash) - else - actual_adapter - end + res = adapter_klass.new(**params) + + cache_mode = use_cache && cache_exists?(code_hash) + + res = if cache_mode + cache_adapter(code_hash, res.connection_name) + elsif save_cache + caching_adapter(res, code_hash) + else + res + end + + wrap_text_mode(res, cache_mode) end - def cache_adapter(code_hash) + def wrap_text_mode(ad, cache_mode) + return ad unless text_mode + + params = ::RemoteRuby::TextModeAdapter::DEFAULT_SETTINGS.merge( + stdout_prefix: ad.connection_name, + stderr_prefix: ad.connection_name, + cache_used: cache_mode + ) + + params = params.merge(text_mode) if text_mode.is_a? Hash + + ::RemoteRuby::TextModeAdapter.new(ad, **params) + end + + def cache_adapter(code_hash, connection_name) ::RemoteRuby::CacheAdapter.new( - cache_path: cache_path(code_hash) + cache_path: cache_path(code_hash), + connection_name: connection_name ) end diff --git a/spec/remote_ruby/connection_adapter/cache_adapter_spec.rb b/spec/remote_ruby/connection_adapter/cache_adapter_spec.rb index fc8b67d..04db440 100644 --- a/spec/remote_ruby/connection_adapter/cache_adapter_spec.rb +++ b/spec/remote_ruby/connection_adapter/cache_adapter_spec.rb @@ -3,7 +3,8 @@ describe RemoteRuby::CacheAdapter do subject(:adapter) do described_class.new( - cache_path: cache_path + cache_path: cache_path, + connection_name: 'conn' ) end diff --git a/spec/remote_ruby/connection_adapter/text_mode_adapter_spec.rb b/spec/remote_ruby/connection_adapter/text_mode_adapter_spec.rb index 866e68b..aa6901e 100644 --- a/spec/remote_ruby/connection_adapter/text_mode_adapter_spec.rb +++ b/spec/remote_ruby/connection_adapter/text_mode_adapter_spec.rb @@ -15,7 +15,7 @@ let(:result) { '' } subject(:adapter) do described_class.new( - adapter: base_adapter, + base_adapter, stdout_prefix: stdout_prefix, stderr_prefix: stderr_prefix, cache_prefix: cache_prefix,