proclib 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/examples/main.rb +8 -2
- data/lib/proclib.rb +13 -1
- data/lib/proclib/executor.rb +7 -0
- data/lib/proclib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f14da2636c7920354164801ec23b79a257e0bf1
|
4
|
+
data.tar.gz: 804b5af1d78d5f3295688362451ca40ca719213e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7df0c94025f7aac0fc1cd90631799b49978c6aad6ab0e03be49c944e2a3eeeac28ece0c7ded5ed95218da6922fc37ce711102fa511bfab4cfe33676c0adadaf7
|
7
|
+
data.tar.gz: 8f8b399172a311691292f17dd0e9a3f9bf2ffc74bdf162bbd54bbe46b5cb9b37703c0d49b0844bf1e42637eea3a865b087b5c36767665bd581a34d8d3944f666
|
data/examples/main.rb
CHANGED
@@ -16,6 +16,12 @@ _, stdout, _ = Proclib.run("ls /tmp/", capture_output: true)
|
|
16
16
|
puts "Files in /tmp"
|
17
17
|
puts stdout.join
|
18
18
|
|
19
|
-
cmd = "seq 1
|
19
|
+
cmd = "seq 1 5 | while read n; do echo $n; sleep 0.5; done"
|
20
20
|
|
21
|
-
Proclib.run({
|
21
|
+
Proclib.run({one: cmd, two: cmd}, log_to_console: true)
|
22
|
+
|
23
|
+
output_callback = -> (line, tag, pipe_name) {
|
24
|
+
STDOUT.printf("%s:%s:%s", tag, pipe_name, line)
|
25
|
+
}
|
26
|
+
|
27
|
+
Proclib.run(cmd, tag: :count_things, on_output: output_callback)
|
data/lib/proclib.rb
CHANGED
@@ -10,7 +10,12 @@ require 'proclib/executor'
|
|
10
10
|
|
11
11
|
module Proclib
|
12
12
|
module Methods
|
13
|
-
def run(cmd,
|
13
|
+
def run(cmd,
|
14
|
+
tag: nil,
|
15
|
+
log_to_console: false,
|
16
|
+
capture_output: true,
|
17
|
+
on_output: nil)
|
18
|
+
|
14
19
|
runnable = if cmd.kind_of? String
|
15
20
|
Process.new(cmd, tag: tag || cmd[0..20])
|
16
21
|
elsif cmd.kind_of?(Hash)
|
@@ -21,9 +26,16 @@ module Proclib
|
|
21
26
|
"Expected String or Hash"
|
22
27
|
end
|
23
28
|
|
29
|
+
unless on_output.nil? || on_output.kind_of?(Proc) || on_ouptut.kind_of?(Lambda)
|
30
|
+
raise ArgumentError, "Expected :on_output to be a proc or lambda if given"
|
31
|
+
end
|
32
|
+
|
24
33
|
executor = Executor.new(runnable,
|
25
34
|
log_to_console: log_to_console,
|
35
|
+
on_output: on_output,
|
26
36
|
cache_output: capture_output)
|
37
|
+
|
38
|
+
|
27
39
|
executor.run_sync
|
28
40
|
end
|
29
41
|
end
|
data/lib/proclib/executor.rb
CHANGED
@@ -40,6 +40,13 @@ module Proclib
|
|
40
40
|
def configure_output
|
41
41
|
channel.on(:output) {|e| console_logger << e.data } if opts[:log_to_console]
|
42
42
|
channel.on(:output) {|e| output_cache << e.data} if opts[:cache_output]
|
43
|
+
|
44
|
+
if opts[:on_output]
|
45
|
+
channel.on(:output) do |e|
|
46
|
+
msg = e.data
|
47
|
+
opts[:on_output].call(msg.line, msg.process_tag, msg.pipe_name)
|
48
|
+
end
|
49
|
+
end
|
43
50
|
end
|
44
51
|
|
45
52
|
def output_cache
|
data/lib/proclib/version.rb
CHANGED