cmds 0.2.10 → 0.2.11

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: fb76b191ec12117d2d68bef750fe3f613d770cc5
4
- data.tar.gz: 5afb4768e3fada0c9d088b2552ead175c46d7a94
3
+ metadata.gz: 49607e4b3abe1cdace16b1315240d076c4140f99
4
+ data.tar.gz: e1d27dafc9a3a02f3c058f0a331b482097955a28
5
5
  SHA512:
6
- metadata.gz: ce525833832fd9d2086296e587233e91c4fa86f76bd4ff2ba2913b49d8816a3dd6261f4b768316782598d5e215ab76dbf794591701f927318dbddcf2b2f96b1f
7
- data.tar.gz: 9c03d0736dd8c17559c86211bcbf4a4d384703362cd3b954370417d51da6e0c9a27b3ae3600fda6a07fae1814af24fd27d181b5e60fd78f2914847eb73eb844b
6
+ metadata.gz: a6eef1e155c1dd4d3ae3d5670b15ec24d330f8137d7272a822a80653490d2ba2350e2d4442c45e718a2fab282c210fabd7547479ba45f2ec7cd11d9f065cbf9d
7
+ data.tar.gz: 38ac00432f28b3851cd53ba9ad00beb2037ec71a572a16df9ab1644b37e8de8181baece24d9e860993233dd480211d04abc0a15c02a1b65a53fa3f7e3d7a38e5
data/Gemfile CHANGED
@@ -1,7 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # use local
4
- # gem 'nrser', '~> 0.0', :path => '../nrser'
4
+ # gem 'nrser', '~> 0.3.0.dev', :path => '../nrser'
5
5
 
6
6
  # Specify your gem's dependencies in cmds.gemspec
7
7
  gemspec
@@ -5,8 +5,10 @@ require 'nrser'
5
5
  require 'cmds'
6
6
 
7
7
 
8
- require 'nrser/refinements'
9
- using NRSER
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
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
28
28
  # ----------------------------------------------------------------------------
29
29
 
30
30
  # Mu guns
31
- spec.add_dependency "nrser", ">= 0.2.0.pre.3"
31
+ spec.add_dependency "nrser", '~> 0.3.0'
32
32
 
33
33
  # ERB replacement with more features
34
34
  #
@@ -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, **opts
230
- opts = defaults opts
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
- Cmds.debug "Cmd constructing...",
244
+ logger.trace "Cmd constructing...",
233
245
  template: template,
234
- opts: opts
246
+ options: options
235
247
 
236
248
  @template = template
237
249
 
238
250
  # Assign options to instance variables
239
- opts.each { |key, value|
251
+ options.each { |key, value|
240
252
  instance_variable_set "@#{ key }", value
241
253
  }
242
254
 
@@ -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
- Cmds.debug "entering Cmds#capture",
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
- Cmds.debug "configured input",
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
- Cmds.debug "calling Cmds.spawn..."
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
- Cmds.debug "Cmds.spawn completed",
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
@@ -1,4 +1,3 @@
1
- require 'nrser/refinements'
2
1
 
3
2
  class Cmds
4
3
  # A simple data structure returned from calling {Cmds#capture}
@@ -118,7 +118,7 @@ class Cmds
118
118
  input: nil,
119
119
  **spawn_opts,
120
120
  &io_block
121
- Cmds.debug "entering Cmds#spawn",
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
- Cmds.debug "looking at input...",
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
- Cmds.debug "input is a String or nil, creating pipe..."
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
- Cmds.debug "input should be io-like, setting spawn opt.",
196
+ logger.trace "input should be io-like, setting spawn opt.",
197
197
  input: input
198
198
  if input == $stdin
199
- Cmds.debug "input is $stdin."
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
- Cmds.debug "looking at #{ name }..."
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
- Cmds.debug "#{ name } is a Proc, creating pipe..."
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
- Cmds.debug "#{ name } should be io-like, setting spawn opt.",
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
- Cmds.debug "spawning...",
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
- Cmds.debug "spawned.",
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
- Cmds.debug "wait thread created.",
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
- Cmds.debug "thread started, writing input..."
277
+ logger.trace "thread started, writing input..."
278
278
 
279
279
  in_pipe.w.write input unless input.nil?
280
280
 
281
- Cmds.debug "write done, closing in_pipe.w..."
281
+ logger.trace "write done, closing in_pipe.w..."
282
282
  in_pipe.w.close
283
283
 
284
- Cmds.debug "thread done."
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
- Cmds.debug "thread started"
292
+ logger.trace "thread started"
293
293
 
294
294
  loop do
295
- Cmds.debug "blocking on gets..."
295
+ logger.trace "blocking on gets..."
296
296
  line = pipe.r.gets
297
297
  if line.nil?
298
- Cmds.debug "received nil, output done."
298
+ logger.trace "received nil, output done."
299
299
  else
300
- Cmds.debug NRSER.squish <<-BLOCK
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
- Cmds.debug "reading done, closing pipe.r (unless already closed)..."
307
+ logger.trace \
308
+ "reading done, closing pipe.r (unless already closed)..."
309
309
  pipe.r.close unless pipe.r.closed?
310
310
 
311
- Cmds.debug "thread done."
311
+ logger.trace "thread done."
312
312
  end # thread
313
313
  end # if pipe
314
314
  end # map threads
315
315
 
316
- Cmds.debug "handing off main thread control to the handler..."
316
+ logger.trace "handing off main thread control to the handler..."
317
317
  begin
318
318
  handler.start
319
319
 
320
- Cmds.debug "handler done."
320
+ logger.trace "handler done."
321
321
 
322
322
  ensure
323
323
  # wait for the threads to complete
324
- Cmds.debug "joining threads..."
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
- Cmds.debug "joining #{ thread[:name] } thread..."
328
+ logger.trace "joining #{ thread[:name] } thread..."
329
329
  thread.join
330
330
  end
331
331
  end
332
332
 
333
- Cmds.debug "all threads done."
333
+ logger.trace "all threads done."
334
334
  end
335
335
 
336
336
  status = wait_thread.value.exitstatus
337
- Cmds.debug "exit status: #{ status.inspect }"
337
+ logger.trace "exit status: #{ status.inspect }"
338
338
 
339
- Cmds.debug "checking @assert and exit status..."
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
- Cmds.debug "streaming completed."
350
+ logger.trace "streaming completed."
351
351
 
352
352
  return status
353
353
  end # .spawn
@@ -11,7 +11,7 @@ class Cmds
11
11
  # command exit status.
12
12
  #
13
13
  def stream *args, **kwds, &io_block
14
- Cmds.debug "entering Cmds#stream",
14
+ logger.trace "entering Cmds#stream",
15
15
  args: args,
16
16
  kwds: kwds,
17
17
  io_block: io_block
@@ -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.dup
108
+ DEFAULTS.deep_dup
101
109
  else
102
110
  keys.
103
111
  map {|key|
@@ -52,6 +52,8 @@ class Cmds
52
52
  END
53
53
  end
54
54
 
55
+ name = name.gsub( '_', '-' ) if opts[:dash_opt_names]
56
+
55
57
  # Set type (`:short` or `:long`) prefix and name/value separator depending
56
58
  # on if name is "short" (single character) or "long" (anything else)
57
59
  #
@@ -16,6 +16,7 @@ class Cmds
16
16
  TOKENIZE_OPT_KEYS = [
17
17
  :array_mode,
18
18
  :array_join_string,
19
+ :dash_opt_names,
19
20
  :false_mode,
20
21
  :flatten_array_values,
21
22
  :hash_mode,
@@ -16,6 +16,6 @@ class Cmds
16
16
  #
17
17
  # @return [String]
18
18
  #
19
- VERSION = '0.2.10'
19
+ VERSION = '0.2.11'
20
20
 
21
21
  end
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.10
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-03-03 00:00:00.000000000 Z
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.2.0.pre.3
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.2.0.pre.3
26
+ version: 0.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: erubis
29
29
  requirement: !ruby/object:Gem::Requirement