unix_commander 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/unix_commander/version.rb +1 -1
- data/lib/unix_commander.rb +19 -18
- data/spec/runner_spec.rb +24 -0
- metadata +1 -1
data/lib/unix_commander.rb
CHANGED
@@ -30,8 +30,7 @@ module UnixCommander
|
|
30
30
|
# @return [String] *stdout* of the command
|
31
31
|
def out
|
32
32
|
return "" if @out==nil
|
33
|
-
|
34
|
-
@out.read
|
33
|
+
@out
|
35
34
|
end
|
36
35
|
|
37
36
|
# Returns the output (stderr) of the command
|
@@ -39,8 +38,7 @@ module UnixCommander
|
|
39
38
|
# @return [String] *stderr* of the command
|
40
39
|
def err
|
41
40
|
return "" if @err==nil
|
42
|
-
|
43
|
-
@err.read
|
41
|
+
@err
|
44
42
|
end
|
45
43
|
|
46
44
|
# Return a string with the output of the command
|
@@ -54,6 +52,8 @@ module UnixCommander
|
|
54
52
|
# @return [Runner] Returns itself
|
55
53
|
def run
|
56
54
|
@in, @out, @err = Open3.popen3("#{@command.cmd}")
|
55
|
+
@out = @out.read
|
56
|
+
@err = @err.read
|
57
57
|
self
|
58
58
|
end
|
59
59
|
|
@@ -103,20 +103,6 @@ module UnixCommander
|
|
103
103
|
cmd
|
104
104
|
end
|
105
105
|
|
106
|
-
# This is the main method of the library. Every unknown method you call on a Command object
|
107
|
-
# is interpreted as a unix command and its args are used as the args of the unix command.
|
108
|
-
# When the command already has some unix command inside, it pipes them together (|)
|
109
|
-
# @param [String] m name of the unix command you want to execute
|
110
|
-
# @param [Array] *args args for the aforementioned command
|
111
|
-
# @return [Command] new command with internal unix commands piped together
|
112
|
-
def method_missing(m, *args, &block)
|
113
|
-
if cmd == ""
|
114
|
-
Command.new("#{m} #{args.join(' ')}".strip)
|
115
|
-
else
|
116
|
-
Command.new("#{cmd} | #{m} #{args.join(' ')}".strip)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
106
|
# Redirects *stdout* to someplace (Using >). By default it uses destructive redirection.
|
121
107
|
# @param [String] place to redirect the output (e.g. /dev/null)
|
122
108
|
# @param [true,false] append if true uses append redirection (>>) it defaults to false.
|
@@ -167,6 +153,21 @@ module UnixCommander
|
|
167
153
|
def run_ssh(_username, _password = "", _address = "127.0.0.1")
|
168
154
|
Runner.new(self).run_ssh(_username,_password,_address)
|
169
155
|
end
|
156
|
+
|
157
|
+
# This is the main method of the library. Every unknown method you call on a Command object
|
158
|
+
# is interpreted as a unix command and its args are used as the args of the unix command.
|
159
|
+
# When the command already has some unix command inside, it pipes them together (|)
|
160
|
+
# @param [String] m name of the unix command you want to execute
|
161
|
+
# @param [Array] *args args for the aforementioned command
|
162
|
+
# @return [Command] new command with internal unix commands piped together
|
163
|
+
def method_missing(m, *args, &block)
|
164
|
+
if cmd == ""
|
165
|
+
Command.new("#{m} #{args.join(' ')}".strip)
|
166
|
+
else
|
167
|
+
Command.new("#{cmd} | #{m} #{args.join(' ')}".strip)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
170
171
|
end
|
171
172
|
|
172
173
|
end
|
data/spec/runner_spec.rb
CHANGED
@@ -36,7 +36,19 @@ describe "Runner local" do
|
|
36
36
|
out_err[0].should_not == ""
|
37
37
|
out_err[1].should_not == ""
|
38
38
|
end
|
39
|
+
|
40
|
+
it "can read out twice" do
|
41
|
+
runner = @command.run
|
42
|
+
runner.out.should == %x[uname]
|
43
|
+
runner.out.should == %x[uname]
|
44
|
+
end
|
39
45
|
|
46
|
+
it "can read err twice" do
|
47
|
+
@command = UnixCommander::Command.new("grep abc /etc/*")
|
48
|
+
runner = @command.run
|
49
|
+
runner.out.should_not == ""
|
50
|
+
runner.out.should_not == ""
|
51
|
+
end
|
40
52
|
end
|
41
53
|
|
42
54
|
describe "Runner ssh" do
|
@@ -70,4 +82,16 @@ describe "Runner ssh" do
|
|
70
82
|
out_err[1].should_not == ""
|
71
83
|
end
|
72
84
|
|
85
|
+
it "can read out twice" do
|
86
|
+
runner = @command.run_ssh('dev','dev')
|
87
|
+
runner.out.should == %x[uname]
|
88
|
+
runner.out.should == %x[uname]
|
89
|
+
end
|
90
|
+
|
91
|
+
it "can read err twice" do
|
92
|
+
@command = UnixCommander::Command.new("grep abc /etc/*")
|
93
|
+
runner = @command.run_ssh('dev','dev')
|
94
|
+
runner.out.should_not == ""
|
95
|
+
runner.out.should_not == ""
|
96
|
+
end
|
73
97
|
end
|