command_utils 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0751ad0ace2f2c6163ce2f9063983bec3a144235
4
- data.tar.gz: 4b4d127135dedce36e4797d2ed6afb8520ee8fc9
3
+ metadata.gz: 4991e3855f226e37e49caf964e057ba3344dba28
4
+ data.tar.gz: df365e0070852ec25222cc916631bbd8a3f16b0b
5
5
  SHA512:
6
- metadata.gz: 8c8b90bacde267555dd47922c3de5e5ec4ccd18076d73e759aacffde9dc50e82b884812c40846dbd24282ff841a1ace9dcabc14d34e5456eb16f5d638d0e8425
7
- data.tar.gz: 4fae3852a2c9f830f18cc662100410b4427ea5589e06ff2b4d73fe092dd51463616781b74f0888b93773511ab506e8f0be9dc986cb99f728b3dd3bf67c3a602e
6
+ metadata.gz: e0709414a5649afbd1999eeb46daf7e940c246fdc111aa7df37bd5c83a907fce11f162aee29faeb57c6f7ec5dcdade4e68cd29194b1f94f58dd931aa9d54c830
7
+ data.tar.gz: 43aee5e4542f6c67df965151797cda5513ec140512ec03a7e16e157140be82182e2197f7e3d26775fd942130e0a2a0298fec0455c9438ca7344e696766f02a75
@@ -0,0 +1,37 @@
1
+ class CommandUtils
2
+
3
+ # Line buffers writes, and calls method at each line
4
+ class LineBuffer
5
+ # Receive a method to call passing each line received. Optionally, specify a prefix.
6
+ def initialize method, prefix=''
7
+ @method = method
8
+ @prefix = prefix
9
+ @buffer = nil
10
+ end
11
+
12
+ # Receive a new chunk, and if a line is formed, call method.
13
+ def write str
14
+ @buffer ||= ''
15
+ @buffer += str
16
+ return unless @buffer.include? "\n"
17
+ lines = @buffer.split("\n")
18
+ @buffer = if @buffer.match("\n$")
19
+ nil
20
+ else
21
+ lines.pop
22
+ end
23
+ lines.each do |line|
24
+ @method.call(@prefix + line)
25
+ end
26
+ end
27
+
28
+ # Send all buffer to method
29
+ def flush
30
+ return unless @buffer
31
+ @method.call(@prefix + @buffer)
32
+ @buffer = nil
33
+ end
34
+
35
+ end
36
+
37
+ end
data/lib/command_utils.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative 'command_utils/non_zero_status'
2
+ require_relative 'command_utils/line_buffer'
2
3
 
3
4
  # Class to assist calling external commands, while processing its output and return code.
4
5
  # All methods which execute given command, raise NonZeroStatus if its return is not 0.
@@ -10,7 +11,7 @@ class CommandUtils
10
11
  yield self if block_given?
11
12
  end
12
13
 
13
- # Execute command, yielding to given block, each time there is output.
14
+ # Execute command, yielding to given block, each time there is output available.
14
15
  # stream:: either +:stdout+ or +:stderr+.
15
16
  # data:: data read from respective stream.
16
17
  def each_output # :yields: stream, data
@@ -35,15 +36,43 @@ class CommandUtils
35
36
  end
36
37
  end
37
38
 
38
- # Takes command in same format supported by Process#spawn.
39
- # Execute command, yielding to given block, each time there is output.
40
- # stream:: either +:stdout+ or +:stderr+.
41
- # data:: data read from respective stream.
39
+ # Wrapper for CommandUtils#each_output
42
40
  def self.each_output *command, &block # :yields: stream, data
43
41
  self.new(*command).each_output(&block)
44
42
  end
45
43
 
46
- # Execute command, logging its output to given Logger object.
44
+ # Execute command, yielding to given block, each time there is a new line available.
45
+ # stream:: either +:stdout+ or +:stderr+.
46
+ # data:: data read from respective stream.
47
+ def each_line &block # :yields: stream, data
48
+ stdout_lb = LineBuffer.new(
49
+ proc do |data|
50
+ block.call :stdout, data
51
+ end
52
+ )
53
+ stderr_lb = LineBuffer.new(
54
+ proc do |data|
55
+ block.call :stderr, data
56
+ end
57
+ )
58
+ each_output do |stream, data|
59
+ case stream
60
+ when :stdout
61
+ stdout_lb.write data
62
+ when :stderr
63
+ stderr_lb.write data
64
+ end
65
+ end
66
+ stdout_lb.flush
67
+ stderr_lb.flush
68
+ end
69
+
70
+ # Wrapper for CommandUtils#each_line
71
+ def self.each_line *command, &block # :yields: stream, data
72
+ self.new(*command).each_line(&block)
73
+ end
74
+
75
+ # Execute command, logging its output, line buffered, to given Logger object.
47
76
  # Must receive a hash, containing at least:
48
77
  # +:logger+:: Logger instance.
49
78
  # +:stdout_level+:: Logger level to log stdout.
@@ -52,23 +81,14 @@ class CommandUtils
52
81
  # +:stdout_prefix+:: Prefix to use for all stdout messages.
53
82
  # +:stderr_prefix+:: Prefix to use for all stderr messages.
54
83
  def logger_exec options
55
- logger = options[:logger]
56
- each_output do |stream, data|
84
+ each_line do |stream, data|
57
85
  level = options["#{stream}_level".to_sym]
58
86
  prefix = options["#{stream}_prefix".to_sym]
59
- logger.send(level, "#{prefix}#{data}")
87
+ options[:logger].send(level, "#{prefix}#{data}")
60
88
  end
61
89
  end
62
90
 
63
- # Takes command in same format supported by Process#spawn.
64
- # Execute command, logging its output to given Logger object.
65
- # Must receive a hash, containing at least:
66
- # +:logger+:: Logger instance.
67
- # +:stdout_level+:: Logger level to log stdout.
68
- # +:stderr_level+:: Logger level to log stderr.
69
- # and optionally:
70
- # +:stdout_prefix+:: Prefix to use for all stdout messages.
71
- # +:stderr_prefix+:: Prefix to use for all stderr messages.
91
+ # Wrapper for CommandUtils@logger_exec
72
92
  def self.logger_exec command, options
73
93
  self.new(command).logger_exec(options)
74
94
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Pugliese Ornellas
@@ -66,6 +66,7 @@ extensions: []
66
66
  extra_rdoc_files: []
67
67
  files:
68
68
  - lib/command_utils.rb
69
+ - lib/command_utils/line_buffer.rb
69
70
  - lib/command_utils/non_zero_status.rb
70
71
  homepage: https://github.com/fornellas/command_utils
71
72
  licenses: