Skip to content

Commit

Permalink
Remove output prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nu-hin committed Dec 24, 2024
1 parent cf5956c commit f73363a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 57 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,13 @@ The list of general parameters:
| use_cache | Boolean | no | `false` | Specifies if the cache should be used for execution of the block (if the cache is available). Refer to the [Caching](#caching) section to find out more about caching. |
| save_cache | Boolean | no | `false` | Specifies if the result of the block execution (i.e. output and error streams) should be cached for the subsequent use. Refer to the [Caching](#caching) section to find out more about caching. |
| cache_dir | String | no | ./cache | Path to the directory on the local machine, where cache files should be saved. If the directory doesn't exist, RemoteRuby will try to create it. Refer to the [Caching](#caching) section to find out more about caching. |
| out_prefix | String | no | `nil` | Specifies a prefix to be added to each line of output stream |
| out_prefix | String | no | `'[CACHE] '` | Specifies a prefix to be added to each line of output stream, when the context is reading from cache |
when the context is reading from cache |
| stdout | Stream open for writing | no | `$stdout` | Redirection stream for server standard output |
| stderr | Stream open for writing | no | `$stderr` | Redirection stream for server standard error output |

### Output

Standard output and standard error streams from the remote process are captured, and then, depending on your parameters are either forwarded to local STOUT/STDERR or to the specified streams. RemoteRuby will add a prefix to each line of server output to distinguish between local and server output. STDOUT prefix is displayed in green, STDERR prefix is red. If output is read from cache, then `[CACHE]` prefix will also be added. The prefix may also depend on the adapter used.
Standard output and standard error streams from the remote process are captured, and then, depending on your parameters are either forwarded to local STOUT/STDERR or to the specified streams.

```ruby
remotely(server: 'my_ssh_server', working_dir: '/home/john') do
Expand All @@ -167,10 +166,6 @@ Standard output and standard error streams from the remote process are captured,
end
```

```bash
my_ssh_server:/home/john> This is an output
my_ssh_server:/home/john> This is a warning
```

### Local variables and return value

Expand Down
7 changes: 1 addition & 6 deletions lib/remote_ruby/execution_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ def initialize(**params)
@out_stream = params.delete(:out_stream) || $stdout
@err_stream = params.delete(:err_stream) || $stderr
@adapter_klass = params.delete(:adapter) || ::RemoteRuby::SSHAdapter
@out_prefix = params.delete(:out_prefix) || nil
@cache_prefix = params.delete(:cache_prefix) || '[CACHE] '
@params = params

FileUtils.mkdir_p(@cache_dir)
Expand All @@ -46,7 +44,7 @@ def execute(locals = nil, &block)
private

attr_reader :params, :adapter_klass, :use_cache, :save_cache, :cache_dir,
:out_prefix, :in_stream, :out_stream, :err_stream, :flavours, :cache_prefix
:in_stream, :out_stream, :err_stream, :flavours

def assign_locals(local_names, values, block)
local_names.each do |local|
Expand Down Expand Up @@ -96,13 +94,10 @@ def compiler(ruby_code, client_locals)

def execute_code(ruby_code, client_locals = {})
compiler = compiler(ruby_code, client_locals)
code_hash = compiler.code_hash
cp = use_cache && cache_exists?(code_hash) ? cache_prefix : nil

runner = ::RemoteRuby::Runner.new(
code: compiler.compiled_code,
adapter: adapter(compiler.code_hash),
prefix: "#{cp}#{out_prefix}",
in_stream: in_stream,
out_stream: out_stream,
err_stream: err_stream
Expand Down
7 changes: 3 additions & 4 deletions lib/remote_ruby/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ module RemoteRuby
# connection adapter, reading output and unmarshalling result and local
# variables values.
class Runner
def initialize(code:, adapter:, prefix: nil, in_stream: $stdin, out_stream: $stdout, err_stream: $stderr)
def initialize(code:, adapter:, in_stream: $stdin, out_stream: $stdout, err_stream: $stderr)
@code = code
@adapter = adapter
@in_stream = in_stream
@out_stream = out_stream
@err_stream = err_stream
@prefix = prefix
end

def run
Expand All @@ -33,7 +32,7 @@ def run

private

attr_reader :code, :adapter, :in_stream, :out_stream, :err_stream, :prefix
attr_reader :code, :adapter, :in_stream, :out_stream, :err_stream

def read_stream(read_from, write_to)
Thread.new do
Expand All @@ -43,7 +42,7 @@ def read_stream(read_from, write_to)
if line.start_with?('%%%MARSHAL')
Thread.current[:locals] ||= unmarshal(read_from)
else
write_to.puts "#{prefix}#{line}"
write_to.puts line
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions spec/integration/ssh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
end

context 'in caching mode' do
let(:cache_prefix) { '[C] ' }
let(:save_cache) { true }
let(:cec) do
RemoteRuby::ExecutionContext.new(
Expand All @@ -25,8 +24,7 @@
user: ssh_user,
working_dir: ssh_workdir,
use_cache: true,
save_cache: false,
cache_prefix: cache_prefix
save_cache: false
)
end

Expand All @@ -43,9 +41,9 @@
puts 'Hello, World!'
warn 'Something went wrong!'
end
end.to output("#{cache_prefix}Hello, World!\n")
end.to output("Hello, World!\n")
.to_stdout
.and output("#{cache_prefix}Something went wrong!\n").to_stderr
.and output("Something went wrong!\n").to_stderr
end

it 'ignores stdin on replay' do
Expand Down
36 changes: 1 addition & 35 deletions spec/remote_ruby/execution_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,6 @@
end.to output("#{s}\n").to_stdout
end

context 'with output prefix' do
let(:base_params) { { out_prefix: 'PREFIX' } }

it 'prints with prefix' do
s = 'Something'

expect do
execution_context.execute(s: s) do
# :nocov:
puts s
# :nocov:
end
end.to output("PREFIX#{s}\n").to_stdout
end
end

context 'with save_cache' do
let(:base_params) { { save_cache: true, cache_dir: cache_dir } }

Expand All @@ -72,7 +56,7 @@

context 'with use_cache' do
let(:base_params) do
{ save_cache: true, use_cache: true, cache_dir: cache_dir, cache_prefix: '[C] ' }
{ save_cache: true, use_cache: true, cache_dir: cache_dir }
end

let(:caching_context) { described_class.new(**params) }
Expand All @@ -95,24 +79,6 @@

expect(res).to eq(10)
end

it 'prints with prefix' do
expect do
caching_context.execute do
# :nocov:
puts 'Hello'
# :nocov:
end
end.to output("Hello\n").to_stdout

expect do
execution_context.execute do
# :nocov:
puts 'Hello'
# :nocov:
end
end.to output("[C] Hello\n").to_stdout
end
end

context 'when execution context is a local variable' do
Expand Down

0 comments on commit f73363a

Please sign in to comment.