cmds 0.2.0 → 0.2.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/cmds.gemspec +1 -1
- data/lib/cmds/result.rb +1 -10
- data/lib/cmds/spawn.rb +6 -5
- data/lib/cmds/stream.rb +51 -0
- data/lib/cmds/sugar.rb +2 -2
- data/lib/cmds/util/tokenize_option.rb +3 -5
- data/lib/cmds/util.rb +30 -0
- data/lib/cmds/version.rb +1 -1
- data/lib/cmds.rb +5 -24
- data/spec/cmds/stream_spec.rb +13 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec573a2d3357aa33be6e99e15cfbbf17208b5f7b
|
4
|
+
data.tar.gz: 3650ce87a15e971b126395e11e86063b7605312a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a162d0e9dd3acd278e7c73274a13f14771f7c8dbd19f3bbd2427ac1830acab26930cb7daac1adb5030cbef0083fd94974cd4efb838a638bcdc26ad64e09fd35
|
7
|
+
data.tar.gz: dd7ad5d87fb29f2f3b36d22c8f440e6521c07df69f73a29a067910fa03bb8bd4b551623376802ea33995e7c3413a46af20878b2a4c63b49fc74edeeddf607f93
|
data/cmds.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency 'nrser', '>= 0.0.
|
21
|
+
spec.add_dependency 'nrser', '>= 0.0.17'
|
22
22
|
spec.add_dependency 'erubis', '~> 2.7'
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.5"
|
data/lib/cmds/result.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
require 'nrser/refinements'
|
2
2
|
|
3
|
-
using NRSER
|
4
|
-
|
5
3
|
class Cmds
|
6
4
|
# a simple data structure returned from calling {Cmds#capture}
|
7
5
|
# on a {Cmd} instance.
|
@@ -55,14 +53,7 @@ class Cmds
|
|
55
53
|
# @raise [SystemCallError] if the command failed.
|
56
54
|
#
|
57
55
|
def assert
|
58
|
-
|
59
|
-
msg = <<-BLOCK.squish
|
60
|
-
command `#{ @cmd }` exited with status #{ @status }
|
61
|
-
and stderr #{ err.inspect }
|
62
|
-
BLOCK
|
63
|
-
|
64
|
-
raise SystemCallError.new msg, @status
|
65
|
-
end
|
56
|
+
Cmds.check_status @cmd, @status, @err
|
66
57
|
self
|
67
58
|
end # raise_error
|
68
59
|
end # Result
|
data/lib/cmds/spawn.rb
CHANGED
@@ -10,8 +10,6 @@ require 'nrser/refinements'
|
|
10
10
|
require 'cmds/pipe'
|
11
11
|
require 'cmds/io_handler'
|
12
12
|
|
13
|
-
using NRSER
|
14
|
-
|
15
13
|
class Cmds
|
16
14
|
# internal core function to spawn and stream inputs and/or outputs using
|
17
15
|
# threads.
|
@@ -42,6 +40,9 @@ class Cmds
|
|
42
40
|
# - block is called with the {Cmds::IOHandler} instance for the
|
43
41
|
# execution, which it can use to handle input and outputs.
|
44
42
|
#
|
43
|
+
# @return [Fixnum]
|
44
|
+
# command exit status.
|
45
|
+
#
|
45
46
|
# @raise [ArgumentError]
|
46
47
|
# if `&io_block` has arity greater than 1.
|
47
48
|
#
|
@@ -76,7 +77,7 @@ class Cmds
|
|
76
77
|
input = handler.in unless handler.in.nil?
|
77
78
|
else
|
78
79
|
# bad block provided
|
79
|
-
raise ArgumentError.new <<-BLOCK
|
80
|
+
raise ArgumentError.new NRSER.squish <<-BLOCK
|
80
81
|
provided input block must have arity 0 or 1
|
81
82
|
BLOCK
|
82
83
|
end # case io_block.arity
|
@@ -207,7 +208,7 @@ class Cmds
|
|
207
208
|
if line.nil?
|
208
209
|
Cmds.debug "received nil, output done."
|
209
210
|
else
|
210
|
-
Cmds.debug <<-BLOCK
|
211
|
+
Cmds.debug NRSER.squish <<-BLOCK
|
211
212
|
received #{ line.bytesize } bytes, passing to handler.
|
212
213
|
BLOCK
|
213
214
|
end
|
@@ -250,7 +251,7 @@ class Cmds
|
|
250
251
|
if @assert && status != 0
|
251
252
|
# we don't necessarily have the err output, so we can't include it
|
252
253
|
# in the error message
|
253
|
-
msg = <<-BLOCK
|
254
|
+
msg = NRSER.squish <<-BLOCK
|
254
255
|
streamed command `#{ cmd }` exited with status #{ status }
|
255
256
|
BLOCK
|
256
257
|
|
data/lib/cmds/stream.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
class Cmds
|
2
|
+
# stream a command.
|
3
|
+
#
|
4
|
+
# @param *args (see #capture)
|
5
|
+
# @param **kwds (see #capture)
|
6
|
+
#
|
7
|
+
# @param [nil | String | #read] &io_block
|
8
|
+
# string or readable IO-like object to use as input to the command.
|
9
|
+
#
|
10
|
+
# @return [Fixnum]
|
11
|
+
# command exit status.
|
12
|
+
#
|
13
|
+
def stream *args, **kwds, &io_block
|
14
|
+
Cmds.debug "entering Cmd#stream",
|
15
|
+
args: args,
|
16
|
+
kwds: kwds,
|
17
|
+
io_block: io_block
|
18
|
+
|
19
|
+
Cmds.spawn prepare(*args, **kwds),
|
20
|
+
input: @input,
|
21
|
+
# include env if mode is spawn argument
|
22
|
+
env: (@env_mode == :spawn_arg ? @env : {}),
|
23
|
+
chdir: @chdir,
|
24
|
+
&io_block
|
25
|
+
end # #stream
|
26
|
+
|
27
|
+
# stream and raise an error if exit code is not 0.
|
28
|
+
#
|
29
|
+
# @param *args (see #capture)
|
30
|
+
# @param **kwds (see #capture)
|
31
|
+
# @param &io_block (see #stream)
|
32
|
+
# @return [Fixnum] (see #stream)
|
33
|
+
#
|
34
|
+
# @raise [SystemCallError]
|
35
|
+
# if exit status is not 0.
|
36
|
+
#
|
37
|
+
def stream! *args, **kwds, &io_block
|
38
|
+
cmd = prepare(*args, **kwds)
|
39
|
+
|
40
|
+
status = Cmds.spawn cmd,
|
41
|
+
input: @input,
|
42
|
+
# include env if mode is spawn argument
|
43
|
+
env: (@env_mode == :spawn_arg ? @env : {}),
|
44
|
+
chdir: @chdir,
|
45
|
+
&io_block
|
46
|
+
|
47
|
+
Cmds.check_status cmd, status
|
48
|
+
|
49
|
+
status
|
50
|
+
end # #stream!
|
51
|
+
end # Cmds
|
data/lib/cmds/sugar.rb
CHANGED
@@ -93,8 +93,8 @@ class Cmds
|
|
93
93
|
end
|
94
94
|
|
95
95
|
|
96
|
-
def self.stream! template, *
|
97
|
-
Cmds.new(template
|
96
|
+
def self.stream! template, *args, **kwds, &io_block
|
97
|
+
Cmds.new(template).stream! *args, **kwds, &io_block
|
98
98
|
end # ::stream!
|
99
99
|
|
100
100
|
|
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'nrser/refinements'
|
3
3
|
|
4
|
-
using NRSER
|
5
|
-
|
6
4
|
require_relative "defaults"
|
7
5
|
|
8
6
|
class Cmds
|
@@ -39,7 +37,7 @@ class Cmds
|
|
39
37
|
opts = defaults opts, [:array_mode, :array_join_string, :false_mode]
|
40
38
|
|
41
39
|
unless name.is_a?(String) && name.length > 0
|
42
|
-
raise ArgumentError.new <<-END
|
40
|
+
raise ArgumentError.new NRSER.squish <<-END
|
43
41
|
`name` must be a String of length greater than zero,
|
44
42
|
found #{ name.inspect }
|
45
43
|
END
|
@@ -78,7 +76,7 @@ class Cmds
|
|
78
76
|
|
79
77
|
else
|
80
78
|
# SOL
|
81
|
-
raise ArgumentError.new <<-END
|
79
|
+
raise ArgumentError.new NRSER.squish <<-END
|
82
80
|
bad array_mode option: #{ opts[:array_mode] },
|
83
81
|
should be :repeat, :join or :json
|
84
82
|
END
|
@@ -103,7 +101,7 @@ class Cmds
|
|
103
101
|
["--no-#{ esc(name) }"]
|
104
102
|
|
105
103
|
else
|
106
|
-
raise ArgumentError.new <<-END
|
104
|
+
raise ArgumentError.new NRSER.squish <<-END
|
107
105
|
bad :false_mode option: #{ opts[:false_mode] },
|
108
106
|
should be :omit or :no
|
109
107
|
END
|
data/lib/cmds/util.rb
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
# stdlib
|
4
4
|
require 'shellwords'
|
5
5
|
|
6
|
+
# deps
|
7
|
+
require 'nrser'
|
8
|
+
|
9
|
+
# package
|
6
10
|
require 'cmds/util/tokenize_options'
|
7
11
|
|
8
12
|
class Cmds
|
@@ -105,4 +109,30 @@ class Cmds
|
|
105
109
|
)
|
106
110
|
end # ::replace_shortcuts
|
107
111
|
|
112
|
+
# raise an error unless the exit status is 0.
|
113
|
+
#
|
114
|
+
# @param [String] cmd
|
115
|
+
# the command sting that was executed.
|
116
|
+
#
|
117
|
+
# @param [Fixnum] status
|
118
|
+
# the command's exit status.
|
119
|
+
#
|
120
|
+
# @return [nil]
|
121
|
+
#
|
122
|
+
# @raise [SystemCallError]
|
123
|
+
# if exit status is not 0.
|
124
|
+
#
|
125
|
+
def self.check_status cmd, status, err = nil
|
126
|
+
unless status.equal? 0
|
127
|
+
msg = NRSER.squish <<-END
|
128
|
+
command `#{ cmd }` exited with status #{ status }
|
129
|
+
END
|
130
|
+
|
131
|
+
if err
|
132
|
+
msg += " and stderr:\n\n" + err
|
133
|
+
end
|
134
|
+
|
135
|
+
raise SystemCallError.new msg, status
|
136
|
+
end
|
137
|
+
end # .assert
|
108
138
|
end # class Cmds
|
data/lib/cmds/version.rb
CHANGED
data/lib/cmds.rb
CHANGED
@@ -10,6 +10,7 @@ require 'cmds/erb_context'
|
|
10
10
|
require 'cmds/shell_eruby'
|
11
11
|
require 'cmds/result'
|
12
12
|
require 'cmds/sugar'
|
13
|
+
require 'cmds/stream'
|
13
14
|
|
14
15
|
class Cmds
|
15
16
|
# ERB stirng template (with Cmds-specific extensions) for the command.
|
@@ -180,35 +181,20 @@ class Cmds
|
|
180
181
|
def prepare *args, **kwds
|
181
182
|
Cmds.format render(*args, **kwds), @format
|
182
183
|
end # #prepare
|
183
|
-
|
184
|
-
|
185
|
-
def stream *args, **kwds, &io_block
|
186
|
-
Cmds.debug "entering Cmd#stream",
|
187
|
-
args: args,
|
188
|
-
kwds: kwds,
|
189
|
-
io_block: io_block
|
190
|
-
|
191
|
-
Cmds.spawn prepare(*args, **kwds),
|
192
|
-
input: @input,
|
193
|
-
# include env if mode is spawn argument
|
194
|
-
env: (@env_mode == :spawn_arg ? @env : {}),
|
195
|
-
chdir: @chdir,
|
196
|
-
&io_block
|
197
|
-
end # #stream
|
198
|
-
|
184
|
+
|
199
185
|
|
200
186
|
# executes the command and returns a {Cmds::Result} with the captured
|
201
187
|
# outputs.
|
202
188
|
#
|
203
|
-
# @param [Array<Object>] args
|
189
|
+
# @param [Array<Object>] *args
|
204
190
|
# positional parameters to append to those in `@args` for rendering
|
205
191
|
# into the command string.
|
206
192
|
#
|
207
|
-
# @param [Hash{Symbol => Object}] kwds
|
193
|
+
# @param [Hash{Symbol => Object}] **kwds
|
208
194
|
# keyword parameters that override those in `@kwds` for rendering
|
209
195
|
# into the command string.
|
210
196
|
#
|
211
|
-
# @param [#call] input_block
|
197
|
+
# @param [#call] &input_block
|
212
198
|
# optional block that returns a string or readable object to override
|
213
199
|
# `@input`.
|
214
200
|
#
|
@@ -303,11 +289,6 @@ class Cmds
|
|
303
289
|
end
|
304
290
|
|
305
291
|
|
306
|
-
# def assert
|
307
|
-
# capture.raise_error
|
308
|
-
# end
|
309
|
-
|
310
|
-
|
311
292
|
def proxy
|
312
293
|
stream do |io|
|
313
294
|
io.in = $stdin
|
data/spec/cmds/stream_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Cmds#stream' do
|
4
4
|
let(:times) { 5 }
|
5
5
|
|
6
6
|
it "writes to $stdout and $stderr by default" do
|
@@ -54,5 +54,16 @@ describe "Cmds::stream" do
|
|
54
54
|
|
55
55
|
expect(out).to match /^\s+3\n$/
|
56
56
|
end
|
57
|
+
end # input
|
58
|
+
|
59
|
+
it "returns the exit status" do
|
60
|
+
expect(Cmds.new('true').stream).to be 0
|
61
|
+
expect(Cmds.new('false').stream).to be 1
|
57
62
|
end
|
58
|
-
end # Cmds
|
63
|
+
end # Cmds#stream
|
64
|
+
|
65
|
+
describe 'Cmds#stream!' do
|
66
|
+
it "raises when exit code is not 0" do
|
67
|
+
expect { Cmds.new('false').stream! }.to raise_error SystemCallError
|
68
|
+
end
|
69
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nrser
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.0.
|
19
|
+
version: 0.0.17
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.0.
|
26
|
+
version: 0.0.17
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: erubis
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -149,6 +149,7 @@ files:
|
|
149
149
|
- lib/cmds/result.rb
|
150
150
|
- lib/cmds/shell_eruby.rb
|
151
151
|
- lib/cmds/spawn.rb
|
152
|
+
- lib/cmds/stream.rb
|
152
153
|
- lib/cmds/sugar.rb
|
153
154
|
- lib/cmds/util.rb
|
154
155
|
- lib/cmds/util/defaults.rb
|