cmds 0.2.10 → 0.2.11
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/bin/console +4 -2
- data/cmds.gemspec +1 -1
- data/lib/cmds.rb +17 -5
- data/lib/cmds/capture.rb +6 -6
- data/lib/cmds/result.rb +0 -1
- data/lib/cmds/spawn.rb +30 -30
- data/lib/cmds/stream.rb +1 -1
- data/lib/cmds/util/defaults.rb +9 -1
- data/lib/cmds/util/tokenize_option.rb +2 -0
- data/lib/cmds/util/tokenize_value.rb +1 -0
- data/lib/cmds/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 49607e4b3abe1cdace16b1315240d076c4140f99
|
4
|
+
data.tar.gz: e1d27dafc9a3a02f3c058f0a331b482097955a28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6eef1e155c1dd4d3ae3d5670b15ec24d330f8137d7272a822a80653490d2ba2350e2d4442c45e718a2fab282c210fabd7547479ba45f2ec7cd11d9f065cbf9d
|
7
|
+
data.tar.gz: 38ac00432f28b3851cd53ba9ad00beb2037ec71a572a16df9ab1644b37e8de8181baece24d9e860993233dd480211d04abc0a15c02a1b65a53fa3f7e3d7a38e5
|
data/Gemfile
CHANGED
data/bin/console
CHANGED
@@ -5,8 +5,10 @@ require 'nrser'
|
|
5
5
|
require 'cmds'
|
6
6
|
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
NRSER::Log.setup_for_console! application: 'cmds'
|
9
|
+
|
10
|
+
require 'nrser/refinements/types'
|
11
|
+
using NRSER::Types
|
10
12
|
|
11
13
|
|
12
14
|
# You can add fixtures and/or initialization code here to make experimenting
|
data/cmds.gemspec
CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
# ----------------------------------------------------------------------------
|
29
29
|
|
30
30
|
# Mu guns
|
31
|
-
spec.add_dependency "nrser",
|
31
|
+
spec.add_dependency "nrser", '~> 0.3.0'
|
32
32
|
|
33
33
|
# ERB replacement with more features
|
34
34
|
#
|
data/lib/cmds.rb
CHANGED
@@ -23,11 +23,19 @@ require 'cmds/stream'
|
|
23
23
|
require 'cmds/capture'
|
24
24
|
|
25
25
|
|
26
|
+
|
27
|
+
|
26
28
|
# Definitions
|
27
29
|
# =======================================================================
|
28
30
|
|
29
31
|
class Cmds
|
30
32
|
|
33
|
+
# Mixins
|
34
|
+
# ========================================================================
|
35
|
+
|
36
|
+
include NRSER::Log::Mixin
|
37
|
+
|
38
|
+
|
31
39
|
# Attributes
|
32
40
|
# ============================================================================
|
33
41
|
|
@@ -226,17 +234,21 @@ class Cmds
|
|
226
234
|
#
|
227
235
|
# Available as the {#kwds} attribute.
|
228
236
|
#
|
229
|
-
def initialize template, **
|
230
|
-
|
237
|
+
def initialize template, **options
|
238
|
+
options = defaults options
|
239
|
+
|
240
|
+
if options.key? :opts
|
241
|
+
options[:kwds][:opts] = options.delete :opts
|
242
|
+
end
|
231
243
|
|
232
|
-
|
244
|
+
logger.trace "Cmd constructing...",
|
233
245
|
template: template,
|
234
|
-
|
246
|
+
options: options
|
235
247
|
|
236
248
|
@template = template
|
237
249
|
|
238
250
|
# Assign options to instance variables
|
239
|
-
|
251
|
+
options.each { |key, value|
|
240
252
|
instance_variable_set "@#{ key }", value
|
241
253
|
}
|
242
254
|
|
data/lib/cmds/capture.rb
CHANGED
@@ -4,7 +4,7 @@ class Cmds
|
|
4
4
|
# outputs.
|
5
5
|
#
|
6
6
|
# @param [Array<Object>] *args
|
7
|
-
# positional parameters to append to those in `@args` for rendering
|
7
|
+
# positional parameters to append to those in `@args` for rendering
|
8
8
|
# into the command string.
|
9
9
|
#
|
10
10
|
# @param [Hash{Symbol => Object}] **kwds
|
@@ -19,7 +19,7 @@ class Cmds
|
|
19
19
|
# result of execution with command string, status, stdout and stderr.
|
20
20
|
#
|
21
21
|
def capture *args, **kwds, &input_block
|
22
|
-
|
22
|
+
logger.trace "entering Cmds#capture",
|
23
23
|
args: args,
|
24
24
|
kwds: kwds,
|
25
25
|
input: input
|
@@ -28,14 +28,14 @@ class Cmds
|
|
28
28
|
# otherwise default to instance variable (which may be `nil`)
|
29
29
|
input = input_block.nil? ? input : input_block.call
|
30
30
|
|
31
|
-
|
31
|
+
logger.trace "configured input",
|
32
32
|
input: input
|
33
33
|
|
34
34
|
# strings output will be concatenated onto
|
35
35
|
out = ''
|
36
36
|
err = ''
|
37
37
|
|
38
|
-
|
38
|
+
logger.trace "calling Cmds.spawn..."
|
39
39
|
|
40
40
|
status = spawn(*args, **kwds) do |io|
|
41
41
|
# send the input to stream, which sends it to spawn
|
@@ -51,7 +51,7 @@ class Cmds
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
|
54
|
+
logger.trace "Cmds.spawn completed",
|
55
55
|
status: status
|
56
56
|
|
57
57
|
# build a Result
|
@@ -68,4 +68,4 @@ class Cmds
|
|
68
68
|
|
69
69
|
alias_method :call, :capture
|
70
70
|
|
71
|
-
end # class Cmds
|
71
|
+
end # class Cmds
|
data/lib/cmds/result.rb
CHANGED
data/lib/cmds/spawn.rb
CHANGED
@@ -118,7 +118,7 @@ class Cmds
|
|
118
118
|
input: nil,
|
119
119
|
**spawn_opts,
|
120
120
|
&io_block
|
121
|
-
|
121
|
+
logger.trace "entering Cmds#spawn",
|
122
122
|
cmd: cmd,
|
123
123
|
env: env,
|
124
124
|
input: input,
|
@@ -175,7 +175,7 @@ class Cmds
|
|
175
175
|
end # case io_block.arity
|
176
176
|
end # if io_block
|
177
177
|
|
178
|
-
|
178
|
+
logger.trace "looking at input...",
|
179
179
|
input: input
|
180
180
|
|
181
181
|
# (possibly) create the input pipe... this will be nil if the provided
|
@@ -183,7 +183,7 @@ class Cmds
|
|
183
183
|
# `spawn` options.
|
184
184
|
in_pipe = case input
|
185
185
|
when nil, String
|
186
|
-
|
186
|
+
logger.trace "input is a String or nil, creating pipe..."
|
187
187
|
|
188
188
|
in_pipe = Cmds::Pipe.new "INPUT", :in
|
189
189
|
spawn_opts[:in] = in_pipe.r
|
@@ -193,10 +193,10 @@ class Cmds
|
|
193
193
|
in_pipe
|
194
194
|
|
195
195
|
else
|
196
|
-
|
196
|
+
logger.trace "input should be io-like, setting spawn opt.",
|
197
197
|
input: input
|
198
198
|
if input == $stdin
|
199
|
-
|
199
|
+
logger.trace "input is $stdin."
|
200
200
|
end
|
201
201
|
spawn_opts[:in] = input
|
202
202
|
nil
|
@@ -221,13 +221,13 @@ class Cmds
|
|
221
221
|
["ERROR", :err],
|
222
222
|
["OUTPUT", :out],
|
223
223
|
].map do |name, sym|
|
224
|
-
|
224
|
+
logger.trace "looking at #{ name }..."
|
225
225
|
|
226
226
|
dest = handler.public_send sym
|
227
227
|
|
228
228
|
# see if hanlder.out or hanlder.err is a Proc
|
229
229
|
if dest.is_a? Proc
|
230
|
-
|
230
|
+
logger.trace "#{ name } is a Proc, creating pipe..."
|
231
231
|
pipe = Cmds::Pipe.new name, sym
|
232
232
|
# the corresponding :out or :err option for spawn needs to be
|
233
233
|
# the pipe's write handle
|
@@ -236,7 +236,7 @@ class Cmds
|
|
236
236
|
pipe
|
237
237
|
|
238
238
|
else
|
239
|
-
|
239
|
+
logger.trace "#{ name } should be io-like, setting spawn opt.",
|
240
240
|
output: dest
|
241
241
|
spawn_opts[sym] = dest
|
242
242
|
# the pipe is nil!
|
@@ -244,7 +244,7 @@ class Cmds
|
|
244
244
|
end
|
245
245
|
end # map outputs
|
246
246
|
|
247
|
-
|
247
|
+
logger.trace "spawning...",
|
248
248
|
env: env,
|
249
249
|
cmd: cmd,
|
250
250
|
opts: spawn_opts
|
@@ -253,13 +253,13 @@ class Cmds
|
|
253
253
|
cmd,
|
254
254
|
spawn_opts
|
255
255
|
|
256
|
-
|
256
|
+
logger.trace "spawned.",
|
257
257
|
pid: pid
|
258
258
|
|
259
259
|
wait_thread = Process.detach pid
|
260
260
|
wait_thread[:name] = "WAIT"
|
261
261
|
|
262
|
-
|
262
|
+
logger.trace "wait thread created.",
|
263
263
|
thread: wait_thread
|
264
264
|
|
265
265
|
# close child ios if created
|
@@ -274,14 +274,14 @@ class Cmds
|
|
274
274
|
in_thread = if in_pipe
|
275
275
|
Thread.new do
|
276
276
|
Thread.current[:name] = in_pipe.name
|
277
|
-
|
277
|
+
logger.trace "thread started, writing input..."
|
278
278
|
|
279
279
|
in_pipe.w.write input unless input.nil?
|
280
280
|
|
281
|
-
|
281
|
+
logger.trace "write done, closing in_pipe.w..."
|
282
282
|
in_pipe.w.close
|
283
283
|
|
284
|
-
|
284
|
+
logger.trace "thread done."
|
285
285
|
end # Thread
|
286
286
|
end
|
287
287
|
|
@@ -289,54 +289,54 @@ class Cmds
|
|
289
289
|
if pipe
|
290
290
|
Thread.new do
|
291
291
|
Thread.current[:name] = pipe.name
|
292
|
-
|
292
|
+
logger.trace "thread started"
|
293
293
|
|
294
294
|
loop do
|
295
|
-
|
295
|
+
logger.trace "blocking on gets..."
|
296
296
|
line = pipe.r.gets
|
297
297
|
if line.nil?
|
298
|
-
|
298
|
+
logger.trace "received nil, output done."
|
299
299
|
else
|
300
|
-
|
301
|
-
received #{ line.bytesize } bytes, passing to handler.
|
302
|
-
BLOCK
|
300
|
+
logger.trace \
|
301
|
+
"received #{ line.bytesize } bytes, passing to handler."
|
303
302
|
end
|
304
303
|
handler.thread_send_line pipe.sym, line
|
305
304
|
break if line.nil?
|
306
305
|
end
|
307
306
|
|
308
|
-
|
307
|
+
logger.trace \
|
308
|
+
"reading done, closing pipe.r (unless already closed)..."
|
309
309
|
pipe.r.close unless pipe.r.closed?
|
310
310
|
|
311
|
-
|
311
|
+
logger.trace "thread done."
|
312
312
|
end # thread
|
313
313
|
end # if pipe
|
314
314
|
end # map threads
|
315
315
|
|
316
|
-
|
316
|
+
logger.trace "handing off main thread control to the handler..."
|
317
317
|
begin
|
318
318
|
handler.start
|
319
319
|
|
320
|
-
|
320
|
+
logger.trace "handler done."
|
321
321
|
|
322
322
|
ensure
|
323
323
|
# wait for the threads to complete
|
324
|
-
|
324
|
+
logger.trace "joining threads..."
|
325
325
|
|
326
326
|
[in_thread, out_thread, err_thread, wait_thread].each do |thread|
|
327
327
|
if thread
|
328
|
-
|
328
|
+
logger.trace "joining #{ thread[:name] } thread..."
|
329
329
|
thread.join
|
330
330
|
end
|
331
331
|
end
|
332
332
|
|
333
|
-
|
333
|
+
logger.trace "all threads done."
|
334
334
|
end
|
335
335
|
|
336
336
|
status = wait_thread.value.exitstatus
|
337
|
-
|
337
|
+
logger.trace "exit status: #{ status.inspect }"
|
338
338
|
|
339
|
-
|
339
|
+
logger.trace "checking @assert and exit status..."
|
340
340
|
if @assert && status != 0
|
341
341
|
# we don't necessarily have the err output, so we can't include it
|
342
342
|
# in the error message
|
@@ -347,7 +347,7 @@ class Cmds
|
|
347
347
|
raise SystemCallError.new msg, status
|
348
348
|
end
|
349
349
|
|
350
|
-
|
350
|
+
logger.trace "streaming completed."
|
351
351
|
|
352
352
|
return status
|
353
353
|
end # .spawn
|
data/lib/cmds/stream.rb
CHANGED
data/lib/cmds/util/defaults.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
4
|
+
require 'active_support/core_ext/object/deep_dup'
|
5
|
+
|
3
6
|
class Cmds
|
4
7
|
# hash of common default values used in method options.
|
5
8
|
#
|
@@ -32,6 +35,11 @@ class Cmds
|
|
32
35
|
# Don't change directories
|
33
36
|
chdir: nil,
|
34
37
|
|
38
|
+
# Commands often use dash-separated option names, but it's a lot more
|
39
|
+
# convenient in Ruby to use underscored when using {Symbol}. This option
|
40
|
+
# will convert the underscores to dashes.
|
41
|
+
dash_opt_names: false,
|
42
|
+
|
35
43
|
# No additional environment
|
36
44
|
env: {},
|
37
45
|
|
@@ -97,7 +105,7 @@ class Cmds
|
|
97
105
|
#
|
98
106
|
def self.defaults opts, keys = '*', extras = {}
|
99
107
|
if keys == '*'
|
100
|
-
DEFAULTS.
|
108
|
+
DEFAULTS.deep_dup
|
101
109
|
else
|
102
110
|
keys.
|
103
111
|
map {|key|
|
data/lib/cmds/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
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.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nrser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nrser
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.3.0
|
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.
|
26
|
+
version: 0.3.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: erubis
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|