cmds 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cmds/spawn.rb +80 -23
- data/lib/cmds/util/tokenize_option.rb +0 -2
- data/lib/cmds/version.rb +1 -1
- data/lib/cmds.rb +10 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 844e8c090bc1bd1fdd4c2196bb879c329497138d
|
4
|
+
data.tar.gz: e2c403e462e1f37fe2a8114101942c6ed5352bae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb6158b6334ac66d4497619010d8d9557abf4a5ebea68d5325c100f83b49e732289ed39105133166426867cc4a3da6e6284cadba4e82f6715ccb89f1a1065057
|
7
|
+
data.tar.gz: b24a5986c2825e9ff8c5e7f518cf713c49b3ffa93dac464f2de1ce02625a8e02c8d635fe7c321e270b46c714beb78045a7e9612c5d8f9697324c33361d47d9c2
|
data/lib/cmds/spawn.rb
CHANGED
@@ -11,51 +11,87 @@ require 'cmds/pipe'
|
|
11
11
|
require 'cmds/io_handler'
|
12
12
|
|
13
13
|
class Cmds
|
14
|
-
#
|
14
|
+
# Low-level static method to spawn and stream inputs and/or outputs using
|
15
15
|
# threads.
|
16
16
|
#
|
17
|
-
#
|
17
|
+
# This is the core execution functionality of the whole library - everything
|
18
|
+
# end up here.
|
19
|
+
#
|
20
|
+
# **WARNING** - This method runs the `cmd` string **AS IS** - no escaping,
|
21
|
+
# formatting, interpolation, etc. are done at this point.
|
22
|
+
#
|
23
|
+
# The whole rest of the library is built on top of this method to provide
|
24
|
+
# that stuff, and if you're using this library, you probably want to use that
|
25
|
+
# stuff.
|
26
|
+
#
|
27
|
+
# You should not need to use this method directly unless you are extending
|
28
|
+
# the library's functionality.
|
29
|
+
#
|
30
|
+
# Originally inspired by
|
18
31
|
#
|
19
32
|
# https://nickcharlton.net/posts/ruby-subprocesses-with-stdout-stderr-streams.html
|
20
33
|
#
|
21
34
|
# with major modifications from looking at Ruby's open3 module.
|
22
35
|
#
|
36
|
+
# At the end of the day ends up calling `Process.spawn`.
|
37
|
+
#
|
23
38
|
# @param [String] cmd
|
24
|
-
#
|
39
|
+
# **SHELL-READY** command string. This is important - whatever you feed in
|
40
|
+
# here will be run **AS IS** - no escaping, formatting, etc.
|
25
41
|
#
|
26
42
|
# @param [nil | String | #read] input
|
27
|
-
#
|
28
|
-
#
|
43
|
+
# String or readable input, or `nil` (meaning no input).
|
44
|
+
#
|
45
|
+
# Allows {Cmds} instances can pass their `@input` instance variable.
|
46
|
+
#
|
47
|
+
# Don't provide input here and via `io_block`.
|
29
48
|
#
|
30
|
-
# @param [Hash{Symbol | String => Object}] env
|
31
|
-
#
|
49
|
+
# @param [Hash{(Symbol | String) => Object}] env
|
50
|
+
# Hash of `ENV` vars to provide for the command.
|
51
|
+
#
|
52
|
+
# We convert symbol keys to strings, but other than that just pass it
|
53
|
+
# through to `Process.spawn`, which I think will `#to_s` everything.
|
54
|
+
#
|
55
|
+
# Pretty much you want to have everything be strings or symbols for this
|
56
|
+
# to make any sense but we're not checking shit at the moment.
|
57
|
+
#
|
58
|
+
# If the {Cmds#env_mode} is `:inline` it should have already prefixed
|
59
|
+
# `cmd` with the definitions and not provide this keyword (or provide
|
60
|
+
# `{}`).
|
32
61
|
#
|
33
62
|
# @param [#call & (#arity ∈ {0, 1})] &io_block
|
34
|
-
#
|
63
|
+
# Optional block to handle io. Behavior depends on arity:
|
35
64
|
#
|
36
|
-
# -
|
37
|
-
# -
|
65
|
+
# - Arity `0`
|
66
|
+
# - Block is called and expected to return an object
|
38
67
|
# suitable for input (`nil`, `String` or `IO`-like).
|
39
|
-
# -
|
40
|
-
# -
|
68
|
+
# - Arity `1`
|
69
|
+
# - Block is called with the {Cmds::IOHandler} instance for the
|
41
70
|
# execution, which it can use to handle input and outputs.
|
71
|
+
#
|
72
|
+
# Don't provide input here and via `input` keyword arg.
|
42
73
|
#
|
43
74
|
# @return [Fixnum]
|
44
|
-
#
|
75
|
+
# Command exit status.
|
76
|
+
#
|
77
|
+
# @raise [ArgumentError]
|
78
|
+
# If `&io_block` has arity greater than 1.
|
45
79
|
#
|
46
80
|
# @raise [ArgumentError]
|
47
|
-
#
|
81
|
+
# If input is provided via the `input` keyword arg and the `io_block`.
|
48
82
|
#
|
49
|
-
def self.spawn
|
83
|
+
def self.spawn cmd,
|
84
|
+
env: {},
|
85
|
+
input: nil,
|
86
|
+
chdir: nil,
|
87
|
+
&io_block
|
50
88
|
Cmds.debug "entering Cmds#spawn",
|
51
89
|
cmd: cmd,
|
52
|
-
|
90
|
+
env: env,
|
91
|
+
input: input,
|
92
|
+
chdir: chdir,
|
53
93
|
io_block: io_block
|
54
94
|
|
55
|
-
env = opts[:env] || {}
|
56
|
-
input = opts[:input]
|
57
|
-
chdir = opts[:chdir]
|
58
|
-
|
59
95
|
# create the handler that will be yielded to the input block
|
60
96
|
handler = Cmds::IOHandler.new
|
61
97
|
|
@@ -63,18 +99,36 @@ class Cmds
|
|
63
99
|
#
|
64
100
|
# if a block was provided it overrides the `input` argument.
|
65
101
|
#
|
66
|
-
if io_block
|
102
|
+
if io_block
|
67
103
|
case io_block.arity
|
68
104
|
when 0
|
69
105
|
# when the input block takes no arguments it returns the input
|
106
|
+
|
107
|
+
# Check that `:input` kwd wasn't provided.
|
108
|
+
unless input.nil?
|
109
|
+
raise ArgumentError,
|
110
|
+
"Don't call Cmds.spawn with `:input` keyword arg and a block"
|
111
|
+
end
|
112
|
+
|
70
113
|
input = io_block.call
|
114
|
+
|
71
115
|
when 1
|
72
116
|
# when the input block takes one argument, give it the handler and
|
73
117
|
# ignore the return value
|
74
118
|
io_block.call handler
|
75
119
|
|
76
120
|
# if input was assigned to the handler in the block, use it as input
|
77
|
-
|
121
|
+
unless handler.in.nil?
|
122
|
+
|
123
|
+
# Check that `:input` kwd wasn't provided.
|
124
|
+
unless input.nil?
|
125
|
+
raise ArgumentError,
|
126
|
+
"Don't call Cmds.spawn with `:input` keyword arg and a block"
|
127
|
+
end
|
128
|
+
|
129
|
+
input = handler.in
|
130
|
+
end
|
131
|
+
|
78
132
|
else
|
79
133
|
# bad block provided
|
80
134
|
raise ArgumentError.new NRSER.squish <<-BLOCK
|
@@ -270,7 +324,10 @@ class Cmds
|
|
270
324
|
# Internal method that simply passes through to {Cmds.spawn}, serving as
|
271
325
|
# a hook point for subclasses.
|
272
326
|
#
|
273
|
-
# Accepts and returns the same things as
|
327
|
+
# Accepts and returns the same things as {Cmds#stream}.
|
328
|
+
#
|
329
|
+
# @param (see Cmds#stream)
|
330
|
+
# @return (see Cmds#stream)
|
274
331
|
#
|
275
332
|
def spawn *args, **kwds, &io_block
|
276
333
|
Cmds.spawn prepare(*args, **kwds),
|
data/lib/cmds/version.rb
CHANGED
data/lib/cmds.rb
CHANGED
@@ -296,7 +296,13 @@ class Cmds
|
|
296
296
|
def prepare *args, **kwds
|
297
297
|
@last_prepared_cmd = Cmds.format render(*args, **kwds), self.format
|
298
298
|
end # #prepare
|
299
|
-
|
299
|
+
|
300
|
+
|
301
|
+
# @!group Execution Instance Methods
|
302
|
+
# ----------------------------------------------------------------------------
|
303
|
+
#
|
304
|
+
# Methods that run the command.
|
305
|
+
#
|
300
306
|
|
301
307
|
# execute command and return `true` if it exited successfully.
|
302
308
|
#
|
@@ -423,4 +429,7 @@ class Cmds
|
|
423
429
|
def err *args, **kwds, &input_block
|
424
430
|
capture(*args, **kwds, &input_block).err
|
425
431
|
end
|
432
|
+
|
433
|
+
# @!endgroup Execution Instance Methods
|
434
|
+
|
426
435
|
end # Cmds
|
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.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nrser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nrser
|
@@ -232,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
232
232
|
version: '0'
|
233
233
|
requirements: []
|
234
234
|
rubyforge_project:
|
235
|
-
rubygems_version: 2.
|
235
|
+
rubygems_version: 2.5.2
|
236
236
|
signing_key:
|
237
237
|
specification_version: 4
|
238
238
|
summary: helps read, write and remember commands.
|