cmds 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/cmds.gemspec +2 -1
- data/lib/cmds/capture.rb +71 -0
- data/lib/cmds/result.rb +1 -1
- data/lib/cmds/spawn.rb +22 -0
- data/lib/cmds/stream.rb +4 -16
- data/lib/cmds/sugar.rb +8 -8
- data/lib/cmds/util.rb +15 -3
- data/lib/cmds/version.rb +1 -1
- data/lib/cmds.rb +172 -130
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82780e4858c704d9607db8032e99818163e80220
|
4
|
+
data.tar.gz: e0ba46030000a9c252940a1a80beb1f528ea3a6c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c3bd920b4afeaaabc83d967aaaffbe7c24a084d2164532b741dbb96c0e03385452136ecfe3712069a716326d3725aab9a38ea5c387f9a043118235f0c16ab47
|
7
|
+
data.tar.gz: 4597c1e73ead17c16c850cd0d6043b0c3092c25499f858026f2ff3324975dafb0ce496f48e1eb2febd78a79308436cd5c0eaf8bedfd9e4e63d55634c497bb18f
|
data/cmds.gemspec
CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency "rake"
|
26
26
|
spec.add_development_dependency "rspec"
|
27
27
|
spec.add_development_dependency "pastel"
|
28
|
-
spec.add_development_dependency "yard"
|
28
|
+
spec.add_development_dependency "yard", "~> 0.9.9"
|
29
29
|
spec.add_development_dependency "redcarpet"
|
30
|
+
spec.add_development_dependency 'github-markup'
|
30
31
|
end
|
data/lib/cmds/capture.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
class Cmds
|
2
|
+
|
3
|
+
# executes the command and returns a {Cmds::Result} with the captured
|
4
|
+
# outputs.
|
5
|
+
#
|
6
|
+
# @param [Array<Object>] *args
|
7
|
+
# positional parameters to append to those in `@args` for rendering
|
8
|
+
# into the command string.
|
9
|
+
#
|
10
|
+
# @param [Hash{Symbol => Object}] **kwds
|
11
|
+
# keyword parameters that override those in `@kwds` for rendering
|
12
|
+
# into the command string.
|
13
|
+
#
|
14
|
+
# @param [#call] &input_block
|
15
|
+
# optional block that returns a string or readable object to override
|
16
|
+
# `@input`.
|
17
|
+
#
|
18
|
+
# @return [Cmds::Result]
|
19
|
+
# result of execution with command string, status, stdout and stderr.
|
20
|
+
#
|
21
|
+
def capture *args, **kwds, &input_block
|
22
|
+
Cmds.debug "entering Cmds#capture",
|
23
|
+
args: args,
|
24
|
+
kwds: kwds,
|
25
|
+
input: input
|
26
|
+
|
27
|
+
# extract input from block via `call` if one is provided,
|
28
|
+
# otherwise default to instance variable (which may be `nil`)
|
29
|
+
input = input_block.nil? ? input : input_block.call
|
30
|
+
|
31
|
+
Cmds.debug "configured input",
|
32
|
+
input: input
|
33
|
+
|
34
|
+
# strings output will be concatenated onto
|
35
|
+
out = ''
|
36
|
+
err = ''
|
37
|
+
|
38
|
+
Cmds.debug "calling Cmds.spawn..."
|
39
|
+
|
40
|
+
status = spawn(*args, **kwds) do |io|
|
41
|
+
# send the input to stream, which sends it to spawn
|
42
|
+
io.in = input
|
43
|
+
|
44
|
+
# and concat the output lines as they come in
|
45
|
+
io.on_out do |line|
|
46
|
+
out += line
|
47
|
+
end
|
48
|
+
|
49
|
+
io.on_err do |line|
|
50
|
+
err += line
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Cmds.debug "Cmds.spawn completed",
|
55
|
+
status: status
|
56
|
+
|
57
|
+
# build a Result
|
58
|
+
# result = Cmds::Result.new cmd, status, out_reader.value, err_reader.value
|
59
|
+
result = Cmds::Result.new last_prepared_cmd, status, out, err
|
60
|
+
|
61
|
+
# tell the Result to assert if the Cmds has been told to, which will
|
62
|
+
# raise a SystemCallError with the exit status if it was non-zero
|
63
|
+
result.assert if assert
|
64
|
+
|
65
|
+
return result
|
66
|
+
end # #capture
|
67
|
+
|
68
|
+
|
69
|
+
alias_method :call, :capture
|
70
|
+
|
71
|
+
end # class Cmds
|
data/lib/cmds/result.rb
CHANGED
@@ -2,7 +2,7 @@ require 'nrser/refinements'
|
|
2
2
|
|
3
3
|
class Cmds
|
4
4
|
# a simple data structure returned from calling {Cmds#capture}
|
5
|
-
# on a {
|
5
|
+
# on a {Cmds} instance.
|
6
6
|
#
|
7
7
|
# it contains the exit status code, standard output and standard error,
|
8
8
|
# as well as the actual string command issued (after all substitutions).
|
data/lib/cmds/spawn.rb
CHANGED
@@ -262,4 +262,26 @@ class Cmds
|
|
262
262
|
|
263
263
|
return status
|
264
264
|
end # .spawn
|
265
|
+
|
266
|
+
|
267
|
+
protected
|
268
|
+
# ========================================================================
|
269
|
+
|
270
|
+
# Internal method that simply passes through to {Cmds.spawn}, serving as
|
271
|
+
# a hook point for subclasses.
|
272
|
+
#
|
273
|
+
# Accepts and returns the same things as
|
274
|
+
#
|
275
|
+
def spawn *args, **kwds, &io_block
|
276
|
+
Cmds.spawn prepare(*args, **kwds),
|
277
|
+
input: input,
|
278
|
+
# include env if mode is spawn argument
|
279
|
+
env: (env_mode == :spawn_arg ? env : {}),
|
280
|
+
chdir: chdir,
|
281
|
+
&io_block
|
282
|
+
end # #spawn
|
283
|
+
|
284
|
+
# end protected
|
285
|
+
|
286
|
+
|
265
287
|
end # Cmds
|
data/lib/cmds/stream.rb
CHANGED
@@ -11,17 +11,12 @@ class Cmds
|
|
11
11
|
# command exit status.
|
12
12
|
#
|
13
13
|
def stream *args, **kwds, &io_block
|
14
|
-
Cmds.debug "entering
|
14
|
+
Cmds.debug "entering Cmds#stream",
|
15
15
|
args: args,
|
16
16
|
kwds: kwds,
|
17
17
|
io_block: io_block
|
18
18
|
|
19
|
-
|
20
|
-
input: @input,
|
21
|
-
# include env if mode is spawn argument
|
22
|
-
env: (@env_mode == :spawn_arg ? @env : {}),
|
23
|
-
chdir: @chdir,
|
24
|
-
&io_block
|
19
|
+
spawn *args, **kwds, &io_block
|
25
20
|
end # #stream
|
26
21
|
|
27
22
|
# stream and raise an error if exit code is not 0.
|
@@ -35,16 +30,9 @@ class Cmds
|
|
35
30
|
# if exit status is not 0.
|
36
31
|
#
|
37
32
|
def stream! *args, **kwds, &io_block
|
38
|
-
|
33
|
+
status = stream *args, **kwds, &io_block
|
39
34
|
|
40
|
-
|
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
|
35
|
+
Cmds.check_status last_prepared_cmd, status
|
48
36
|
|
49
37
|
status
|
50
38
|
end # #stream!
|
data/lib/cmds/sugar.rb
CHANGED
@@ -22,8 +22,8 @@ end
|
|
22
22
|
|
23
23
|
|
24
24
|
class Cmds
|
25
|
-
# create a new {
|
26
|
-
# calls {
|
25
|
+
# create a new {Cmds} instance with the template and parameters and
|
26
|
+
# calls {Cmds#prepare}.
|
27
27
|
#
|
28
28
|
# @param [String] template
|
29
29
|
# ERB template parameters are rendered into to create the command string.
|
@@ -42,7 +42,7 @@ class Cmds
|
|
42
42
|
end
|
43
43
|
|
44
44
|
|
45
|
-
# create a new {
|
45
|
+
# create a new {Cmds} from template with parameters and call {Cmds#capture}
|
46
46
|
# on it.
|
47
47
|
#
|
48
48
|
# @param template (see .prepare)
|
@@ -61,7 +61,7 @@ class Cmds
|
|
61
61
|
end
|
62
62
|
|
63
63
|
|
64
|
-
# create a new {
|
64
|
+
# create a new {Cmds} from template with parameters and call {Cmd#ok?}
|
65
65
|
# on it.
|
66
66
|
#
|
67
67
|
# @param template (see .prepare)
|
@@ -82,7 +82,7 @@ class Cmds
|
|
82
82
|
end
|
83
83
|
|
84
84
|
|
85
|
-
# create a new {
|
85
|
+
# create a new {Cmds} and
|
86
86
|
def self.assert template, *args, **kwds, &io_block
|
87
87
|
Cmds.new(template).capture(*args, **kwds, &io_block).assert
|
88
88
|
end
|
@@ -98,7 +98,7 @@ class Cmds
|
|
98
98
|
end # ::stream!
|
99
99
|
|
100
100
|
|
101
|
-
# creates a new {
|
101
|
+
# creates a new {Cmds}, captures and returns stdout
|
102
102
|
# (sugar for `Cmds.capture(template, *args, **kwds, &input_block).out`).
|
103
103
|
#
|
104
104
|
# @see Cmd.out
|
@@ -116,7 +116,7 @@ class Cmds
|
|
116
116
|
end
|
117
117
|
|
118
118
|
|
119
|
-
# creates a new {
|
119
|
+
# creates a new {Cmds}, captures and returns stdout. raises an error if the
|
120
120
|
# command fails.
|
121
121
|
#
|
122
122
|
# @see Cmd.out!
|
@@ -137,7 +137,7 @@ class Cmds
|
|
137
137
|
end
|
138
138
|
|
139
139
|
|
140
|
-
# captures a new {
|
140
|
+
# captures a new {Cmds}, captures and chomps stdout
|
141
141
|
# (sugar for `Cmds.out(template, *args, **kwds, &input_block).chomp`).
|
142
142
|
#
|
143
143
|
# @see .out
|
data/lib/cmds/util.rb
CHANGED
@@ -41,11 +41,22 @@ class Cmds
|
|
41
41
|
esc value.to_s
|
42
42
|
end
|
43
43
|
}.join ' '
|
44
|
-
end #
|
44
|
+
end # .tokenize
|
45
45
|
|
46
|
-
|
46
|
+
|
47
|
+
# Formats a command string.
|
48
|
+
#
|
49
|
+
# @param [String] string
|
50
|
+
# Command string to format.
|
51
|
+
#
|
52
|
+
# @param [nil, :squish, :pretty, #call] with
|
53
|
+
# How to format the command string.
|
54
|
+
#
|
47
55
|
def self.format string, with = :squish
|
48
56
|
case with
|
57
|
+
when nil
|
58
|
+
string
|
59
|
+
|
49
60
|
when :squish
|
50
61
|
NRSER.squish string
|
51
62
|
|
@@ -55,7 +66,8 @@ class Cmds
|
|
55
66
|
else
|
56
67
|
with.call string
|
57
68
|
end
|
58
|
-
end
|
69
|
+
end # .format
|
70
|
+
|
59
71
|
|
60
72
|
def self.pretty_format string
|
61
73
|
string = string.gsub(/\n(\s*\n)+\n/, "\n\n")
|
data/lib/cmds/version.rb
CHANGED
data/lib/cmds.rb
CHANGED
@@ -11,13 +11,16 @@ require 'cmds/shell_eruby'
|
|
11
11
|
require 'cmds/result'
|
12
12
|
require 'cmds/sugar'
|
13
13
|
require 'cmds/stream'
|
14
|
+
require 'cmds/capture'
|
14
15
|
|
15
16
|
class Cmds
|
16
17
|
# ERB stirng template (with Cmds-specific extensions) for the command.
|
17
18
|
#
|
18
19
|
# @return [String]
|
20
|
+
#
|
19
21
|
attr_reader :template
|
20
|
-
|
22
|
+
|
23
|
+
|
21
24
|
# base/common positional parameters to render into the command
|
22
25
|
# template.
|
23
26
|
#
|
@@ -27,9 +30,11 @@ class Cmds
|
|
27
30
|
# {#stream}, etc.) accept `*args`, which will be appended to
|
28
31
|
# these values to create the final array for rendering.
|
29
32
|
#
|
30
|
-
# @return [Array<Object>]
|
33
|
+
# @return [Array<Object>]
|
34
|
+
#
|
31
35
|
attr_reader :args
|
32
|
-
|
36
|
+
|
37
|
+
|
33
38
|
# base/common keyword parameters to render into the command template.
|
34
39
|
#
|
35
40
|
# defaults to `{}`.
|
@@ -39,7 +44,9 @@ class Cmds
|
|
39
44
|
# these values to create the final hash for rendering.
|
40
45
|
#
|
41
46
|
# @return [Hash{Symbol => Object}]
|
47
|
+
#
|
42
48
|
attr_reader :kwds
|
49
|
+
|
43
50
|
|
44
51
|
# string or readable IO-like object to use as default input to the
|
45
52
|
# command.
|
@@ -49,21 +56,37 @@ class Cmds
|
|
49
56
|
# value if present.
|
50
57
|
#
|
51
58
|
# @return [String | #read]
|
59
|
+
#
|
52
60
|
attr_reader :input
|
53
61
|
|
62
|
+
|
54
63
|
# if `true`, will execution will raise an error on non-zero exit code.
|
55
64
|
#
|
56
65
|
# defaults to `false`.
|
57
66
|
#
|
58
67
|
# @return [Boolean]
|
68
|
+
#
|
59
69
|
attr_reader :assert
|
60
70
|
|
61
|
-
|
71
|
+
|
72
|
+
# Environment variables to set for command execution.
|
62
73
|
#
|
63
74
|
# defaults to `{}`.
|
64
75
|
#
|
65
76
|
# @return [Hash{String | Symbol => String}]
|
77
|
+
#
|
66
78
|
attr_reader :env
|
79
|
+
|
80
|
+
|
81
|
+
# How environment variables will be set for command execution - inline at
|
82
|
+
# the top of the command, or passed to `Process.spawn` as an argument.
|
83
|
+
#
|
84
|
+
# See the `inline`
|
85
|
+
#
|
86
|
+
# @return [:inline, :spawn_arg]
|
87
|
+
#
|
88
|
+
attr_reader :env_mode
|
89
|
+
|
67
90
|
|
68
91
|
# format specifier symbol:
|
69
92
|
#
|
@@ -78,64 +101,161 @@ class Cmds
|
|
78
101
|
attr_reader :format
|
79
102
|
|
80
103
|
|
81
|
-
# directory to run the command in
|
82
|
-
#
|
83
|
-
# @return [nil | String]
|
84
|
-
attr_reader :chdir
|
85
|
-
|
86
|
-
|
87
|
-
# construct a Cmd.
|
104
|
+
# Optional directory to run the command in, set by the `:chdir` option
|
105
|
+
# in {Cmds#initialize}.
|
88
106
|
#
|
89
|
-
# @
|
90
|
-
#
|
107
|
+
# @return [nil]
|
108
|
+
# If the command will not change directory to run (default behavior).
|
91
109
|
#
|
92
|
-
# @
|
93
|
-
#
|
94
|
-
#
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
110
|
+
# @return [String | Pathname]
|
111
|
+
# If the command will change directory to run.
|
112
|
+
#
|
113
|
+
attr_reader :chdir
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
# The results of the last time {Cmds#prepare} was called on the instance.
|
99
118
|
#
|
100
|
-
#
|
101
|
-
# sets the {#input} attribute.
|
119
|
+
# A little bit funky, I know, but it turns out to be quite useful.
|
102
120
|
#
|
103
|
-
# @
|
104
|
-
#
|
121
|
+
# @return [nil]
|
122
|
+
# If {Cmds#prepare} has never been called.
|
105
123
|
#
|
106
|
-
# @
|
107
|
-
#
|
124
|
+
# @return [String]
|
125
|
+
# If {Cmds#prepare} has been called.
|
108
126
|
#
|
109
|
-
|
110
|
-
|
127
|
+
attr_reader :last_prepared_cmd
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
# Construct a `Cmds` instance.
|
111
132
|
#
|
112
|
-
|
133
|
+
# @param [String] template
|
134
|
+
# String template to use when creating the command string to send to the
|
135
|
+
# shell via {#prepare}.
|
136
|
+
#
|
137
|
+
# Allows ERB (positional and keyword), `%s` (positional) and `%{name}`
|
138
|
+
# (keyword) placeholders.
|
139
|
+
#
|
140
|
+
# Available as the {#template} attribute.
|
141
|
+
#
|
142
|
+
# @param [Array<Object>] args:
|
143
|
+
# Positional arguments to interpolate into the template on {#prepare}.
|
144
|
+
#
|
145
|
+
# Available as the {#args} attribute.
|
146
|
+
#
|
147
|
+
# @param [Boolean] assert:
|
148
|
+
# When `true`, execution will raise an error if the command doesn't exit
|
149
|
+
# successfully (if the command exits with any status other than `0`).
|
150
|
+
#
|
151
|
+
# Available as the {#assert} attribute.
|
152
|
+
#
|
153
|
+
# @param [nil | String | Pathname] chdir:
|
154
|
+
# Optional directory to change into when executing.
|
155
|
+
#
|
156
|
+
# Available as the {#chdir} attribute.
|
157
|
+
#
|
158
|
+
# @param [Hash{(String | Symbol) => String}] env:
|
159
|
+
# Hash of environment variables to set when executing the command.
|
160
|
+
#
|
161
|
+
# Available as the {#env} attribute.
|
162
|
+
#
|
163
|
+
# @param [:inline, :spawn_arg] env_mode:
|
164
|
+
# Controls how the env vars are added to the command.
|
165
|
+
#
|
166
|
+
# - `:inline` adds them to the top of the prepared string. This is nice
|
167
|
+
# if you want do print the command out and paste it into a terminal.
|
168
|
+
# This is the default.
|
169
|
+
#
|
170
|
+
# - `:spawn_arg` passes them as an argument to `Process.spawn`. In this
|
171
|
+
# case they will not be included in the output of {#prepare}
|
172
|
+
# (or {#render}).
|
173
|
+
#
|
174
|
+
# Available as the {#env_mode} attribute.
|
175
|
+
#
|
176
|
+
# @param [nil, :squish, :pretty, #call] format:
|
177
|
+
# Dictates how to format the rendered template string before passing
|
178
|
+
# off to the shell.
|
179
|
+
#
|
180
|
+
# This feature lets you write templates in a more relaxed
|
181
|
+
# manner without `\` line-endings all over the place.
|
182
|
+
#
|
183
|
+
# - `nil` performs **no formatting at all*.
|
184
|
+
#
|
185
|
+
# - `:squish` reduces any consecutive whitespace (including newlines) to
|
186
|
+
# a single space. This is the default.
|
187
|
+
#
|
188
|
+
# - `:pretty` tries to keep the general formatting but make it acceptable
|
189
|
+
# to the shell by adding `\` at the end of lines. See
|
190
|
+
# {Cmds.pretty_format}.
|
191
|
+
#
|
192
|
+
# - An object that responds to `#call` will be called with the command
|
193
|
+
# string as it's only argument for custom formatting.
|
194
|
+
#
|
195
|
+
# See {Cmds.format} for more details.
|
196
|
+
#
|
197
|
+
# Available as the {#format} attribute.
|
198
|
+
#
|
199
|
+
# @param [nil | String | #read] input:
|
200
|
+
# Input to send to the command on execution. Can be a string or an
|
201
|
+
# `IO`-like object that responds to `#read`.
|
202
|
+
#
|
203
|
+
# Available as the {#input} attribute.
|
204
|
+
#
|
205
|
+
# @param [Hash{Symbol => Object}] kwds:
|
206
|
+
# Keyword arguments to shell escape and interpolate into the template on
|
207
|
+
# {#prepare}.
|
208
|
+
#
|
209
|
+
# Available as the {#kwds} attribute.
|
210
|
+
#
|
211
|
+
def initialize template,
|
212
|
+
args: [],
|
213
|
+
assert: false,
|
214
|
+
chdir: nil,
|
215
|
+
env: {},
|
216
|
+
env_mode: :inline,
|
217
|
+
format: :squish,
|
218
|
+
input: nil,
|
219
|
+
kwds: {}
|
113
220
|
Cmds.debug "Cmd constructing...",
|
114
221
|
template: template,
|
115
|
-
opts:
|
222
|
+
opts: {
|
223
|
+
args: args,
|
224
|
+
kwds: kwds,
|
225
|
+
input: input,
|
226
|
+
assert: assert,
|
227
|
+
env: env,
|
228
|
+
format: format,
|
229
|
+
env_mode: env_mode,
|
230
|
+
chdir: chdir,
|
231
|
+
}
|
116
232
|
|
117
233
|
@template = template
|
118
|
-
@args =
|
119
|
-
@kwds =
|
120
|
-
@input =
|
121
|
-
@assert =
|
122
|
-
@env =
|
123
|
-
@format =
|
124
|
-
@env_mode =
|
125
|
-
@chdir =
|
234
|
+
@args = args.freeze
|
235
|
+
@kwds = kwds.freeze
|
236
|
+
@input = input
|
237
|
+
@assert = assert
|
238
|
+
@env = env.freeze
|
239
|
+
@format = format
|
240
|
+
@env_mode = env_mode
|
241
|
+
@chdir = chdir
|
242
|
+
|
243
|
+
# An internal cache of the last result of calling {#prepare}, or `nil` if
|
244
|
+
# {#prepare} has never been called. Kinda funky but ends up being useful.
|
245
|
+
@last_prepared_cmd = nil
|
126
246
|
end # #initialize
|
127
247
|
|
128
248
|
|
129
|
-
# returns a new {
|
249
|
+
# returns a new {Cmds} with the parameters and input merged in
|
130
250
|
def curry *args, **kwds, &input_block
|
131
|
-
self.class.new
|
132
|
-
args: (
|
133
|
-
kwds: (
|
134
|
-
input: (
|
135
|
-
assert:
|
136
|
-
env:
|
137
|
-
format:
|
138
|
-
chdir:
|
251
|
+
self.class.new template, {
|
252
|
+
args: (self.args + args),
|
253
|
+
kwds: (self.kwds.merge kwds),
|
254
|
+
input: (input_block ? input_block.call : self.input),
|
255
|
+
assert: self.assert,
|
256
|
+
env: self.env,
|
257
|
+
format: self.format,
|
258
|
+
chdir: self.chdir,
|
139
259
|
}
|
140
260
|
end
|
141
261
|
|
@@ -154,12 +274,12 @@ class Cmds
|
|
154
274
|
# the rendered command string.
|
155
275
|
#
|
156
276
|
def render *args, **kwds
|
157
|
-
context = Cmds::ERBContext.new((
|
158
|
-
erb = Cmds::ShellEruby.new Cmds.replace_shortcuts(
|
277
|
+
context = Cmds::ERBContext.new((self.args + args), self.kwds.merge(kwds))
|
278
|
+
erb = Cmds::ShellEruby.new Cmds.replace_shortcuts(self.template)
|
159
279
|
rendered = NRSER.dedent erb.result(context.get_binding)
|
160
280
|
|
161
|
-
if
|
162
|
-
rendered =
|
281
|
+
if self.env_mode == :inline && !self.env.empty?
|
282
|
+
rendered = self.env.sort_by {|name, value|
|
163
283
|
name
|
164
284
|
}.map {|name, value|
|
165
285
|
"#{ name }=#{ Cmds.esc value }"
|
@@ -179,86 +299,8 @@ class Cmds
|
|
179
299
|
# the prepared command string.
|
180
300
|
#
|
181
301
|
def prepare *args, **kwds
|
182
|
-
Cmds.format render(*args, **kwds),
|
302
|
+
@last_prepared_cmd = Cmds.format render(*args, **kwds), self.format
|
183
303
|
end # #prepare
|
184
|
-
|
185
|
-
|
186
|
-
# executes the command and returns a {Cmds::Result} with the captured
|
187
|
-
# outputs.
|
188
|
-
#
|
189
|
-
# @param [Array<Object>] *args
|
190
|
-
# positional parameters to append to those in `@args` for rendering
|
191
|
-
# into the command string.
|
192
|
-
#
|
193
|
-
# @param [Hash{Symbol => Object}] **kwds
|
194
|
-
# keyword parameters that override those in `@kwds` for rendering
|
195
|
-
# into the command string.
|
196
|
-
#
|
197
|
-
# @param [#call] &input_block
|
198
|
-
# optional block that returns a string or readable object to override
|
199
|
-
# `@input`.
|
200
|
-
#
|
201
|
-
# @return [Cmds::Result]
|
202
|
-
# result of execution with command string, status, stdout and stderr.
|
203
|
-
#
|
204
|
-
def capture *args, **kwds, &input_block
|
205
|
-
Cmds.debug "entering Cmds#capture",
|
206
|
-
args: args,
|
207
|
-
kwds: kwds,
|
208
|
-
input: input
|
209
|
-
|
210
|
-
# prepare the command string
|
211
|
-
cmd = prepare *args, **kwds
|
212
|
-
|
213
|
-
# extract input from block via `call` if one is provided,
|
214
|
-
# otherwise default to instance variable (which may be `nil`)
|
215
|
-
input = input_block.nil? ? @input : input_block.call
|
216
|
-
|
217
|
-
Cmds.debug "prepared",
|
218
|
-
cmd: cmd,
|
219
|
-
input: input
|
220
|
-
|
221
|
-
# strings output will be concatenated onto
|
222
|
-
out = ''
|
223
|
-
err = ''
|
224
|
-
|
225
|
-
Cmds.debug "calling Cmds.spawn..."
|
226
|
-
|
227
|
-
status = Cmds.spawn(
|
228
|
-
cmd,
|
229
|
-
# include env if mode is spawn argument
|
230
|
-
env: (@env_mode == :spawn_arg ? @env : {}),
|
231
|
-
chdir: @chdir
|
232
|
-
) do |io|
|
233
|
-
# send the input to stream, which sends it to spawn
|
234
|
-
io.in = input
|
235
|
-
|
236
|
-
# and concat the output lines as they come in
|
237
|
-
io.on_out do |line|
|
238
|
-
out += line
|
239
|
-
end
|
240
|
-
|
241
|
-
io.on_err do |line|
|
242
|
-
err += line
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
Cmds.debug "Cmds.spawn completed",
|
247
|
-
status: status
|
248
|
-
|
249
|
-
# build a Result
|
250
|
-
# result = Cmds::Result.new cmd, status, out_reader.value, err_reader.value
|
251
|
-
result = Cmds::Result.new cmd, status, out, err
|
252
|
-
|
253
|
-
# tell the Result to assert if the Cmds has been told to, which will
|
254
|
-
# raise a SystemCallError with the exit status if it was non-zero
|
255
|
-
result.assert if @assert
|
256
|
-
|
257
|
-
return result
|
258
|
-
end # #capture
|
259
|
-
|
260
|
-
|
261
|
-
alias_method :call, :capture
|
262
304
|
|
263
305
|
|
264
306
|
# execute command and return `true` if it exited successfully.
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nrser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nrser
|
@@ -96,6 +96,20 @@ dependencies:
|
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: yard
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.9.9
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.9.9
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: redcarpet
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
115
|
- - ">="
|
@@ -109,7 +123,7 @@ dependencies:
|
|
109
123
|
- !ruby/object:Gem::Version
|
110
124
|
version: '0'
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
126
|
+
name: github-markup
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
114
128
|
requirements:
|
115
129
|
- - ">="
|
@@ -142,6 +156,7 @@ files:
|
|
142
156
|
- bin/rspec
|
143
157
|
- cmds.gemspec
|
144
158
|
- lib/cmds.rb
|
159
|
+
- lib/cmds/capture.rb
|
145
160
|
- lib/cmds/debug.rb
|
146
161
|
- lib/cmds/erb_context.rb
|
147
162
|
- lib/cmds/io_handler.rb
|