command_utils 0.0.0
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 +7 -0
- data/lib/command_utils/non_zero_status.rb +18 -0
- data/lib/command_utils.rb +86 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 55fdf974b8fe587df928b134b85230d1e2444658
|
4
|
+
data.tar.gz: 0972d50cb9c28dddfbf24e1fe2af7fd91efe716e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1cf38a61eea8bc39a2a4a229f90e74642e3d6022514215ec7d0093f70cb1761299a5ca901b4ae0af46954afa05bdda0c940003d561c5c2c611123df535050095
|
7
|
+
data.tar.gz: 74ed7398ded2e1cc63041a832ccdb9d7ecefd2a230a0a04921095ec38028c518b50181339744a72a253a816ae79a69bbbb8acdb989f21d99514ce26211976fe7
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CommandUtils
|
2
|
+
|
3
|
+
# Raised when executed command does not return 0
|
4
|
+
class NonZeroStatus < RuntimeError
|
5
|
+
|
6
|
+
# Command exit status
|
7
|
+
attr_accessor :status
|
8
|
+
# Command as passed to Process#spawn
|
9
|
+
attr_accessor :command
|
10
|
+
|
11
|
+
def initialize message, status, command
|
12
|
+
super message
|
13
|
+
@status, @command = status, command
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require_relative 'command_utils/non_zero_status'
|
2
|
+
|
3
|
+
# Class to assist calling external commands, while processing its output and return code.
|
4
|
+
# All methods which execute given command, raise NonZeroStatus if its return is not 0.
|
5
|
+
class CommandUtils
|
6
|
+
|
7
|
+
# Takes command in same format supported by Process#spawn
|
8
|
+
def initialize *command
|
9
|
+
@command = command
|
10
|
+
end
|
11
|
+
|
12
|
+
# Execute command, yielding to given block, each time there is output.
|
13
|
+
# stream:: either +:stdout+ or +:stderr+.
|
14
|
+
# data:: data read from respective stream.
|
15
|
+
def each_output # :yields: stream, data
|
16
|
+
run do
|
17
|
+
loop do
|
18
|
+
io_list = [@stdout_read, @stderr_read].keep_if{|io| not io.closed?}
|
19
|
+
break if io_list.empty?
|
20
|
+
IO.select(io_list).first.each do |io|
|
21
|
+
if io.eof?
|
22
|
+
io.close
|
23
|
+
next
|
24
|
+
end
|
25
|
+
label = case io
|
26
|
+
when @stdout_read
|
27
|
+
:stdout
|
28
|
+
when @stderr_read
|
29
|
+
:stderr
|
30
|
+
end
|
31
|
+
yield label, io.read
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Execute command, logging its output to given Logger object.
|
38
|
+
# Must receive a hash, containing at least:
|
39
|
+
# +:logger+:: Logger instance.
|
40
|
+
# +:stdout_level+:: Logger level to log stdout.
|
41
|
+
# +:stderr_level+:: Logger level to log stderr.
|
42
|
+
# and optionally:
|
43
|
+
# +:stdout_prefix+:: Prefix to use for all stdout messages.
|
44
|
+
# +:stderr_prefix+:: Prefix to use for all stderr messages.
|
45
|
+
def logger_exec options
|
46
|
+
logger = options[:logger]
|
47
|
+
each_output do |stream, data|
|
48
|
+
level = options["#{stream}_level".to_sym]
|
49
|
+
prefix = options["#{stream}_prefix".to_sym]
|
50
|
+
logger.send(level, "#{prefix}#{data}")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def run
|
57
|
+
spawn
|
58
|
+
yield
|
59
|
+
process_status
|
60
|
+
end
|
61
|
+
|
62
|
+
def spawn
|
63
|
+
@stdout_read, @stdout_write = IO.pipe
|
64
|
+
@stderr_read, @stderr_write = IO.pipe
|
65
|
+
@pid = Process.spawn(
|
66
|
+
*@command,
|
67
|
+
in: :close,
|
68
|
+
out: @stdout_write.fileno,
|
69
|
+
err: @stderr_write.fileno,
|
70
|
+
close_others: true,
|
71
|
+
)
|
72
|
+
@stdout_write.close
|
73
|
+
@stderr_write.close
|
74
|
+
end
|
75
|
+
|
76
|
+
def process_status
|
77
|
+
Process.wait @pid
|
78
|
+
unless (status = $?.exitstatus) == 0
|
79
|
+
raise NonZeroStatus.new(
|
80
|
+
"Command exited with #{status}.",
|
81
|
+
status,
|
82
|
+
@command
|
83
|
+
)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: command_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fabio Pugliese Ornellas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard-rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-rdoc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 1.0.3
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '1.0'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 1.0.3
|
61
|
+
description: This Gem will help you call external commands, process its stdout and
|
62
|
+
stderr, to your own fit, and at the end, validate its return code.
|
63
|
+
email: fabio.ornellas@gmail.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- lib/command_utils.rb
|
69
|
+
- lib/command_utils/non_zero_status.rb
|
70
|
+
homepage: https://github.com/fornellas/command_utils
|
71
|
+
licenses:
|
72
|
+
- GPL
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.4.5
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Simple Gem to assist running external commands an processing its outputs.
|
94
|
+
test_files: []
|