cmds 0.0.2 → 0.0.3
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/Gemfile +1 -1
- data/lib/cmds.rb +87 -16
- data/lib/cmds/version.rb +1 -1
- data/spec/cmds/call_spec.rb +12 -1
- data/spec/cmds/error_spec.rb +8 -0
- data/spec/cmds/ok_spec.rb +8 -0
- data/spec/cmds/raise_on_error_spec.rb +11 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 746aa0ec8697ab254e151c8000620c7a3a6d8c52
|
4
|
+
data.tar.gz: d15e1589321da1a22ad00d98cb12875d71a0ea7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 186e86b834c8d38aa58b624c394acc850848fff6faac92ff10ecd3336992beff7a6b9d3a139baaec9148b12e1fe036c04a29bf954dde6c27a14309cf69304f98
|
7
|
+
data.tar.gz: de1c715943e12a45f003e5e9a4b0c9b63f8443c8c42a64a35d983f6d742404d82b28eddc06ad238d1777e3aaf20bb1b95ccc4871abfce1090d3b52c822e7846b
|
data/Gemfile
CHANGED
data/lib/cmds.rb
CHANGED
@@ -27,6 +27,18 @@ class Cmds
|
|
27
27
|
def error?
|
28
28
|
! ok?
|
29
29
|
end
|
30
|
+
|
31
|
+
# raises an error if there was one
|
32
|
+
def raise_error
|
33
|
+
if error?
|
34
|
+
msg = NRSER.squish <<-BLOCK
|
35
|
+
command `#{ @cmd }` exited with status #{ @status }
|
36
|
+
and stderr output #{ err.inspect }
|
37
|
+
BLOCK
|
38
|
+
|
39
|
+
raise SystemCallError.new msg, @status
|
40
|
+
end
|
41
|
+
end
|
30
42
|
end
|
31
43
|
|
32
44
|
# extension of Erubis' EscapedEruby (which auto-escapes `<%= %>` and
|
@@ -199,14 +211,17 @@ class Cmds
|
|
199
211
|
NRSER.squish erb.result(context.get_binding)
|
200
212
|
end # ::sub
|
201
213
|
|
202
|
-
def self.
|
214
|
+
def self.subs_to_args_kwds_input subs
|
203
215
|
args = []
|
204
216
|
kwds = {}
|
217
|
+
input = nil
|
205
218
|
|
206
219
|
case subs.length
|
207
220
|
when 0
|
208
|
-
#
|
221
|
+
# nothing to do
|
209
222
|
when 1
|
223
|
+
# can either be a hash, which is interpreted as a keywords,
|
224
|
+
# or an array, which is interpreted as positional arguments
|
210
225
|
case subs[0]
|
211
226
|
when Hash
|
212
227
|
kwds = subs[0]
|
@@ -220,7 +235,9 @@ class Cmds
|
|
220
235
|
BLOCK
|
221
236
|
end
|
222
237
|
|
223
|
-
when 2
|
238
|
+
when 2, 3
|
239
|
+
# first arg needs to be an array, second a hash, and optional third
|
240
|
+
# can be input
|
224
241
|
unless subs[0].is_a? Array
|
225
242
|
raise TypeError.new NRSER.squish <<-BLOCK
|
226
243
|
first *subs arg needs to be an array, not #{ subs[0].inspect }
|
@@ -229,23 +246,45 @@ class Cmds
|
|
229
246
|
|
230
247
|
unless subs[1].is_a? Hash
|
231
248
|
raise TypeError.new NRSER.squish <<-BLOCK
|
232
|
-
|
249
|
+
second *subs arg needs to be a Hash, not #{ subs[1].inspect }
|
233
250
|
BLOCK
|
234
251
|
end
|
235
252
|
|
236
|
-
args, kwds = subs
|
253
|
+
args, kwds, input = subs
|
237
254
|
else
|
238
255
|
raise ArgumentError.new NRSER.squish <<-BLOCK
|
239
256
|
must provide one or two *subs arguments, received #{ 1 + subs.length }
|
240
257
|
BLOCK
|
241
258
|
end
|
242
259
|
|
243
|
-
[args, kwds]
|
260
|
+
[args, kwds, input]
|
244
261
|
end
|
245
262
|
|
246
263
|
# create a new Cmd from template and subs and call it
|
247
264
|
def self.run template, *subs
|
248
|
-
|
265
|
+
args, kwds, input = subs_to_args_kwds_input subs
|
266
|
+
self.new(template, args: args, kwds: kwds, input: input).call
|
267
|
+
end
|
268
|
+
|
269
|
+
def self.ok? template, *subs
|
270
|
+
args, kwds, input = subs_to_args_kwds_input subs
|
271
|
+
self.new(template, args: args, kwds: kwds, input: input).ok?
|
272
|
+
end
|
273
|
+
|
274
|
+
def self.error? template, *subs
|
275
|
+
args, kwds, input = subs_to_args_kwds_input subs
|
276
|
+
self.new(template, args: args, kwds: kwds, input: input).error?
|
277
|
+
end
|
278
|
+
|
279
|
+
def self.raise_on_error template, *subs
|
280
|
+
args, kwds, input = subs_to_args_kwds_input subs
|
281
|
+
self.new(
|
282
|
+
template,
|
283
|
+
args: args,
|
284
|
+
kwds: kwds,
|
285
|
+
input: input,
|
286
|
+
raise_on_error: true
|
287
|
+
).call
|
249
288
|
end
|
250
289
|
|
251
290
|
def self.replace_shortcuts template
|
@@ -282,35 +321,59 @@ class Cmds
|
|
282
321
|
)
|
283
322
|
end
|
284
323
|
|
285
|
-
attr_reader :tempalte, :args, :kwds
|
324
|
+
attr_reader :tempalte, :args, :kwds, :input, :raise_on_error
|
286
325
|
|
287
|
-
def initialize template,
|
326
|
+
def initialize template, opts = {}
|
288
327
|
@template = template
|
289
|
-
@args
|
328
|
+
@args = opts[:args] || []
|
329
|
+
@kwds = opts[:kwds] || {}
|
330
|
+
@input = opts[:input] || nil
|
331
|
+
@raise_on_error = opts[:raise_on_error] || false
|
290
332
|
end #initialize
|
291
333
|
|
292
334
|
def call *subs
|
293
|
-
args
|
335
|
+
# merge any stored args and kwds and get any overriding input
|
336
|
+
args, kwds, input = merge_subs subs
|
294
337
|
|
295
338
|
cmd = Cmds.sub @template, args, kwds
|
296
339
|
|
297
|
-
out, err, status =
|
340
|
+
out, err, status = if input.nil?
|
341
|
+
Open3.capture3 cmd
|
342
|
+
else
|
343
|
+
Open3.capture3 cmd, stdin_data: input
|
344
|
+
end
|
345
|
+
|
346
|
+
result = Cmds::Result.new cmd, status.exitstatus, out, err
|
347
|
+
|
348
|
+
result.raise_error if @raise_on_error
|
298
349
|
|
299
|
-
|
350
|
+
return result
|
300
351
|
end #call
|
301
352
|
|
302
353
|
# returns a new `Cmds` with the subs merged in
|
303
354
|
def curry *subs
|
304
|
-
|
355
|
+
args, kwds, input = merge_subs(subs)
|
356
|
+
self.class.new @template, args: args, kwds: kwds, input: input
|
357
|
+
end
|
358
|
+
|
359
|
+
def ok?
|
360
|
+
call.ok?
|
361
|
+
end
|
362
|
+
|
363
|
+
def error?
|
364
|
+
call.error?
|
305
365
|
end
|
306
366
|
|
307
367
|
private
|
308
368
|
|
309
369
|
def merge_subs subs
|
310
370
|
# break `subs` into `args` and `kwds`
|
311
|
-
args, kwds = Cmds.
|
371
|
+
args, kwds, input = Cmds.subs_to_args_kwds_input subs
|
372
|
+
|
373
|
+
# use any default input if we didn't get a new one
|
374
|
+
input = @input if input.nil?
|
312
375
|
|
313
|
-
[@args + args, @kwds.merge(kwds)]
|
376
|
+
[@args + args, @kwds.merge(kwds), input]
|
314
377
|
end #merge_subs
|
315
378
|
|
316
379
|
# end private
|
@@ -320,3 +383,11 @@ end # Cmds
|
|
320
383
|
def Cmds *args
|
321
384
|
Cmds.run *args
|
322
385
|
end
|
386
|
+
|
387
|
+
def Cmds? *args
|
388
|
+
Cmds.ok? *args
|
389
|
+
end
|
390
|
+
|
391
|
+
def Cmds! *args
|
392
|
+
Cmds.raise_on_error *args
|
393
|
+
end
|
data/lib/cmds/version.rb
CHANGED
data/spec/cmds/call_spec.rb
CHANGED
@@ -13,4 +13,15 @@ describe "Cmds::call" do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end # is reusable
|
16
|
-
|
16
|
+
|
17
|
+
it "accepts input" do
|
18
|
+
input = <<-BLOCK
|
19
|
+
one
|
20
|
+
two
|
21
|
+
three
|
22
|
+
four!
|
23
|
+
BLOCK
|
24
|
+
|
25
|
+
expect( Cmds.new("wc -l", input: input).call.out ).to match /^\s+4$/
|
26
|
+
end
|
27
|
+
end # Cmds::call
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Cmds::raise_on_error" do
|
4
|
+
it "should raise an error when the command fails" do
|
5
|
+
expect{ Cmds.raise_on_error "exit 1" }.to raise_error Errno::EPERM
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should do the same for Cmds!" do
|
9
|
+
expect{ Cmds! "exit 1" }.to raise_error Errno::EPERM
|
10
|
+
end
|
11
|
+
end # Cmds::run
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nrser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nrser
|
@@ -103,7 +103,10 @@ files:
|
|
103
103
|
- spec/cmds/call_spec.rb
|
104
104
|
- spec/cmds/curry_spec.rb
|
105
105
|
- spec/cmds/erb_context_spec.rb
|
106
|
+
- spec/cmds/error_spec.rb
|
106
107
|
- spec/cmds/expand_option_hash_spec.rb
|
108
|
+
- spec/cmds/ok_spec.rb
|
109
|
+
- spec/cmds/raise_on_error_spec.rb
|
107
110
|
- spec/cmds/replace_shortcuts_spec.rb
|
108
111
|
- spec/cmds/run_spec.rb
|
109
112
|
- spec/cmds/sub_spec.rb
|
@@ -138,7 +141,10 @@ test_files:
|
|
138
141
|
- spec/cmds/call_spec.rb
|
139
142
|
- spec/cmds/curry_spec.rb
|
140
143
|
- spec/cmds/erb_context_spec.rb
|
144
|
+
- spec/cmds/error_spec.rb
|
141
145
|
- spec/cmds/expand_option_hash_spec.rb
|
146
|
+
- spec/cmds/ok_spec.rb
|
147
|
+
- spec/cmds/raise_on_error_spec.rb
|
142
148
|
- spec/cmds/replace_shortcuts_spec.rb
|
143
149
|
- spec/cmds/run_spec.rb
|
144
150
|
- spec/cmds/sub_spec.rb
|