relay 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,6 +37,12 @@ You can also add your public key to a remote server's authorized keys file:
37
37
 
38
38
  $ relay identify myserver
39
39
 
40
+ Relay exposes the `execute` method, which returns the output of the command:
41
+
42
+ >> require "relay"
43
+ >> Relay.execute "echo foo", "myserver"
44
+ => ["foo\n"]
45
+
40
46
  Installation
41
47
  ------------
42
48
 
@@ -4,8 +4,10 @@ require "thor"
4
4
  require "open3"
5
5
 
6
6
  class Ssh
7
- def initialize(server)
7
+ def initialize(server, quiet = false)
8
8
  @server = server
9
+ @output = []
10
+ @quiet = quiet
9
11
  end
10
12
 
11
13
  def start
@@ -16,30 +18,44 @@ class Ssh
16
18
 
17
19
  threads << Thread.new do
18
20
  while line = stderr.gets
19
- $stderr.puts(line)
21
+ $stderr.puts(line) unless @quiet
20
22
  end
21
23
  end
22
24
 
23
25
  threads << Thread.new do
24
26
  while line = stdout.gets
25
- $stdout.puts(line)
27
+ @output << line
28
+ $stdout.puts(line) unless @quiet
26
29
  end
27
30
  end
28
31
 
29
32
  yield(self)
30
33
  stdin.close
31
34
  threads.each {|t| t.join }
35
+ @output
32
36
  end
33
37
  end
34
38
 
35
39
  def run(command)
36
- puts "\e[1m\e[33m$ #{command.strip}\e[0m"
40
+ puts "\e[1m\e[33m$ #{command.strip}\e[0m" unless @quiet
37
41
  @stdin.puts(command)
38
42
  end
39
43
  end
40
44
 
41
45
  class Relay < Thor
42
46
 
47
+ # If you require the relay library, you can issue commands
48
+ # with the execute method:
49
+ #
50
+ # @example
51
+ #
52
+ # >> require "relay"
53
+ # >> Relay.execute "echo foo", "myserver"
54
+ # => ["foo\n"]
55
+ def self.execute(*args)
56
+ start args.unshift("execute").push("--quiet")
57
+ end
58
+
43
59
  desc "identify SERVER", "Copies your public key to a remote server"
44
60
  method_option :key, :type => :string, :aliases => "-k"
45
61
  method_option :path, :type => :string, :aliases => "-p"
@@ -63,8 +79,9 @@ class Relay < Thor
63
79
  end
64
80
 
65
81
  desc "execute COMMAND SERVER", "Execute COMMAND in the context of SERVER"
82
+ method_option :quiet, :type => :boolean, :aliases => "-q"
66
83
  def execute(command, server)
67
- Ssh.new(server).start do |session|
84
+ Ssh.new(server, options[:quiet]).start do |session|
68
85
  session.run(command)
69
86
  end
70
87
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "relay"
3
- s.version = "0.0.2"
3
+ s.version = "0.0.3"
4
4
  s.summary = "Relay commands over SSH"
5
5
  s.description = "Relay allows you to execute remote commands via SSH with ease."
6
6
  s.authors = ["Damian Janowski", "Michel Martens"]
@@ -38,5 +38,10 @@ class TestRelay < Test::Unit::TestCase
38
38
  assert out["recipe.sh"]
39
39
  end
40
40
  end
41
+
42
+ should "return the output when used programmatically" do
43
+ assert_equal ["foo\n"], Relay.execute("echo foo", "localhost")
44
+ assert_equal ["foo\n", "bar\n", "baz\n"], Relay.execute("echo foo; echo bar; echo baz", "localhost")
45
+ end
41
46
  end
42
47
  end
@@ -1,6 +1,7 @@
1
1
  require "rubygems"
2
2
  require "contest"
3
3
  require "fileutils"
4
+ require File.join(File.dirname(__FILE__), "..", "lib", "relay")
4
5
 
5
6
  ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
6
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Janowski