tty-command 0.8.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +41 -1
- data/LICENSE.txt +1 -1
- data/README.md +83 -60
- data/lib/tty-command.rb +1 -3
- data/lib/tty/command.rb +17 -17
- data/lib/tty/command/child_process.rb +9 -10
- data/lib/tty/command/cmd.rb +24 -15
- data/lib/tty/command/dry_runner.rb +3 -4
- data/lib/tty/command/exit_error.rb +1 -2
- data/lib/tty/command/printers/abstract.rb +8 -10
- data/lib/tty/command/printers/null.rb +1 -4
- data/lib/tty/command/printers/pretty.rb +23 -22
- data/lib/tty/command/printers/progress.rb +3 -8
- data/lib/tty/command/printers/quiet.rb +3 -7
- data/lib/tty/command/process_runner.rb +57 -50
- data/lib/tty/command/result.rb +4 -3
- data/lib/tty/command/truncator.rb +4 -5
- data/lib/tty/command/version.rb +2 -2
- metadata +24 -64
- data/.gitignore +0 -9
- data/.rspec +0 -4
- data/.travis.yml +0 -29
- data/CODE_OF_CONDUCT.md +0 -49
- data/Gemfile +0 -14
- data/Rakefile +0 -10
- data/appveyor.yml +0 -26
- data/benchmarks/memory.rb +0 -11
- data/bin/console +0 -6
- data/bin/setup +0 -6
- data/examples/bash.rb +0 -12
- data/examples/basic.rb +0 -9
- data/examples/cli +0 -4
- data/examples/env.rb +0 -9
- data/examples/logger.rb +0 -10
- data/examples/output.rb +0 -10
- data/examples/pty.rb +0 -7
- data/examples/redirect_stderr.rb +0 -10
- data/examples/redirect_stdin.rb +0 -15
- data/examples/redirect_stdout.rb +0 -10
- data/examples/stdin_input.rb +0 -10
- data/examples/threaded.rb +0 -12
- data/examples/timeout.rb +0 -7
- data/examples/wait.rb +0 -21
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-command.gemspec +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f62b4f72ffaff00cb6e0f7d7f53159e48716746a157e367807771f4fceeef249
|
4
|
+
data.tar.gz: 761e3df2ac357a5591bc4d873b87f419926f73a5e06b42553e9c0d9d1924ab16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f32eb43589d385247d787d3d4d263760e764111cc125a4651f5372b456d6a0464572d30aff6a3b3c9872dbf913c2281b7d094cab0d4c3bb7f31d03c131e1cfa
|
7
|
+
data.tar.gz: 668d73499b869f65cb8bf3e6eafcf2b8a7d130fbf1821a8681be70464399c31cc192fff20055ae928395c14e59bd21ecf459b1dc2afe647fb4112fd69855cdbb
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,40 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.10.1] - 2021-02-14
|
4
|
+
|
5
|
+
### Fixed
|
6
|
+
* Fix undesired persistence of environment variables in Ruby >= 3.0.0
|
7
|
+
|
8
|
+
## [v0.10.0] - 2020-10-22
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
* Change :chdir option to escape directory location path
|
12
|
+
* Change gemspec to add metadata and remove test artefacts
|
13
|
+
* Change to update pastel dependency and restrict version to minor only
|
14
|
+
* Remove bundler as a dev dependency and relax rspec's upper boundary
|
15
|
+
|
16
|
+
### Fixed
|
17
|
+
* Fix Ruby 2.7 keyword conversion errors
|
18
|
+
* Fix error when environment variable contains % character
|
19
|
+
|
20
|
+
## [v0.9.0] - 2019-09-28
|
21
|
+
|
22
|
+
### Changed
|
23
|
+
* Change gemspec to require Ruby >= 2.0.0
|
24
|
+
|
25
|
+
## [v0.8.2] - 2018-08-07
|
26
|
+
|
27
|
+
### Changed
|
28
|
+
* Change gemspec to load only required files
|
29
|
+
|
30
|
+
### Fixed
|
31
|
+
* Fix issue with Ruby greater than 2.5.0 displaying thread error traceback by default
|
32
|
+
|
33
|
+
## [v0.8.1] - 2018-05-20
|
34
|
+
|
35
|
+
### Changed
|
36
|
+
* Change ProcessRunner#write_stream to handle all writing logic
|
37
|
+
|
3
38
|
## [v0.8.0] - 2018-04-22
|
4
39
|
|
5
40
|
### Added
|
@@ -110,6 +145,11 @@
|
|
110
145
|
|
111
146
|
* Initial implementation and release
|
112
147
|
|
148
|
+
[v0.10.1]: https://github.com/piotrmurach/tty-command/compare/v0.10.0...v0.10.1
|
149
|
+
[v0.10.0]: https://github.com/piotrmurach/tty-command/compare/v0.9.0...v0.10.0
|
150
|
+
[v0.9.0]: https://github.com/piotrmurach/tty-command/compare/v0.8.2...v0.9.0
|
151
|
+
[v0.8.2]: https://github.com/piotrmurach/tty-command/compare/v0.8.1...v0.8.2
|
152
|
+
[v0.8.1]: https://github.com/piotrmurach/tty-command/compare/v0.8.0...v0.8.1
|
113
153
|
[v0.8.0]: https://github.com/piotrmurach/tty-command/compare/v0.7.0...v0.8.0
|
114
154
|
[v0.7.0]: https://github.com/piotrmurach/tty-command/compare/v0.6.0...v0.7.0
|
115
155
|
[v0.6.0]: https://github.com/piotrmurach/tty-command/compare/v0.5.0...v0.6.0
|
@@ -120,4 +160,4 @@
|
|
120
160
|
[v0.3.1]: https://github.com/piotrmurach/tty-command/compare/v0.3.0...v0.3.1
|
121
161
|
[v0.3.0]: https://github.com/piotrmurach/tty-command/compare/v0.2.0...v0.3.0
|
122
162
|
[v0.2.0]: https://github.com/piotrmurach/tty-command/compare/v0.1.0...v0.2.0
|
123
|
-
[v0.1.0]: https://github.com/piotrmurach/tty-command/compare/v0.1.0
|
163
|
+
[v0.1.0]: https://github.com/piotrmurach/tty-command/compare/953ccdd...v0.1.0
|
data/LICENSE.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
The MIT License (MIT)
|
2
2
|
|
3
|
-
Copyright (c) 2016 Piotr Murach
|
3
|
+
Copyright (c) 2016 Piotr Murach (https://piotrmurach.com)
|
4
4
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
<div align="center">
|
2
|
+
<a href="https://ttytoolkit.org"><img width="130" src="https://github.com/piotrmurach/tty/blob/master/images/tty.png" alt="TTY Toolkit logo"/></a>
|
3
|
+
</div>
|
4
|
+
|
1
5
|
# TTY::Command [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
2
6
|
|
3
7
|
[![Gem Version](https://badge.fury.io/rb/tty-command.svg)][gem]
|
4
|
-
[![
|
8
|
+
[![Actions CI](https://github.com/piotrmurach/tty-command/workflows/CI/badge.svg?branch=master)][gh_actions_ci]
|
5
9
|
[![Build status](https://ci.appveyor.com/api/projects/status/0150ync7bdkfhmsv?svg=true)][appveyor]
|
6
10
|
[![Code Climate](https://codeclimate.com/github/piotrmurach/tty-command/badges/gpa.svg)][codeclimate]
|
7
11
|
[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-command/badge.svg)][coverage]
|
@@ -9,7 +13,7 @@
|
|
9
13
|
|
10
14
|
[gitter]: https://gitter.im/piotrmurach/tty
|
11
15
|
[gem]: http://badge.fury.io/rb/tty-command
|
12
|
-
[
|
16
|
+
[gh_actions_ci]: https://github.com/piotrmurach/tty-command/actions?query=workflow%3ACI
|
13
17
|
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-command
|
14
18
|
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-command
|
15
19
|
[coverage]: https://coveralls.io/github/piotrmurach/tty-command
|
@@ -32,7 +36,7 @@ Why should we be handcuffed to `sh` or `bash` for these scripts when we could be
|
|
32
36
|
Add this line to your application's Gemfile:
|
33
37
|
|
34
38
|
```ruby
|
35
|
-
gem
|
39
|
+
gem "tty-command"
|
36
40
|
```
|
37
41
|
|
38
42
|
And then execute:
|
@@ -84,11 +88,11 @@ Or install it yourself as:
|
|
84
88
|
Create a command instance and then run some commands:
|
85
89
|
|
86
90
|
```ruby
|
87
|
-
require
|
91
|
+
require "tty-command"
|
88
92
|
|
89
93
|
cmd = TTY::Command.new
|
90
|
-
cmd.run(
|
91
|
-
cmd.run(
|
94
|
+
cmd.run("ls -la")
|
95
|
+
cmd.run("echo Hello!")
|
92
96
|
```
|
93
97
|
|
94
98
|
Note that `run` will throw an exception if the command fails. This is already an improvement over ordinary shell scripts, which just keep on going when things go bad. That usually makes things worse.
|
@@ -96,7 +100,7 @@ Note that `run` will throw an exception if the command fails. This is already an
|
|
96
100
|
You can use the return value to capture stdout and stderr:
|
97
101
|
|
98
102
|
```ruby
|
99
|
-
out, err = cmd.run(
|
103
|
+
out, err = cmd.run("cat ~/.bashrc | grep alias")
|
100
104
|
```
|
101
105
|
|
102
106
|
Instead of using a plain old string, you can break up the arguments and they'll get escaped if necessary:
|
@@ -123,13 +127,13 @@ The `env`, `command` and `options` arguments are described in the following sect
|
|
123
127
|
For example, to display file contents:
|
124
128
|
|
125
129
|
```ruby
|
126
|
-
cmd.run(
|
130
|
+
cmd.run("cat file.txt")
|
127
131
|
```
|
128
132
|
|
129
133
|
If the command succeeds, a `TTY::Command::Result` is returned that records stdout and stderr:
|
130
134
|
|
131
135
|
```ruby
|
132
|
-
out, err = cmd.run(
|
136
|
+
out, err = cmd.run("date")
|
133
137
|
puts "The date is #{out}"
|
134
138
|
# => "The date is Tue 10 May 2016 22:30:15 BST\n"
|
135
139
|
```
|
@@ -137,7 +141,7 @@ puts "The date is #{out}"
|
|
137
141
|
You can also pass a block that gets invoked anytime stdout and/or stderr receive output:
|
138
142
|
|
139
143
|
```ruby
|
140
|
-
cmd.run(
|
144
|
+
cmd.run("long running script") do |out, err|
|
141
145
|
output << out if out
|
142
146
|
errors << err if err
|
143
147
|
end
|
@@ -157,8 +161,8 @@ If the error output is very long, the stderr may contain only a prefix, number o
|
|
157
161
|
If you expect a command to fail occasionally, use `run!` instead. Then you can detect failures and respond appropriately. For example:
|
158
162
|
|
159
163
|
```ruby
|
160
|
-
if cmd.run!(
|
161
|
-
cmd.run(
|
164
|
+
if cmd.run!("which xyzzy").failure?
|
165
|
+
cmd.run("brew install xyzzy")
|
162
166
|
end
|
163
167
|
```
|
164
168
|
|
@@ -180,7 +184,7 @@ cmd = TTY::Command.new(printer: :progress)
|
|
180
184
|
By default the printers log to `stdout` but this can be changed by passing an object that responds to `<<` message:
|
181
185
|
|
182
186
|
```ruby
|
183
|
-
logger = Logger.new(
|
187
|
+
logger = Logger.new("dev.log")
|
184
188
|
cmd = TTY::Command.new(output: logger)
|
185
189
|
```
|
186
190
|
|
@@ -190,18 +194,34 @@ You can force the printer to always in print in color by passing the `:color` op
|
|
190
194
|
cmd = TTY::Command.new(color: true)
|
191
195
|
```
|
192
196
|
|
197
|
+
If the default printers don't meet your needs you can always create [a custom printer](#34-custom-printer)
|
198
|
+
|
193
199
|
#### 2.3.1 Color
|
194
200
|
|
195
201
|
When using printers you can switch off coloring by using `:color` option set to `false`.
|
196
202
|
|
197
|
-
#### 2.3.2
|
203
|
+
#### 2.3.2 UUID
|
198
204
|
|
199
|
-
By default, when logging is enabled, each log entry is prefixed by specific command run uuid number. This number can be switched off using the `:uuid` option:
|
205
|
+
By default, when logging is enabled and `pretty` printer is used, each log entry is prefixed by specific command run uuid number. This number can be switched off using the `:uuid` option at initialization:
|
200
206
|
|
201
207
|
```ruby
|
202
208
|
cmd = TTY::Command.new(uuid: false)
|
203
|
-
cmd.run(
|
204
|
-
# =>
|
209
|
+
cmd.run("rm -R all_my_files")
|
210
|
+
# =>
|
211
|
+
# Running rm -r all_my_files
|
212
|
+
# ...
|
213
|
+
# Finished in 6 seconds with exit status 0 (successful)
|
214
|
+
```
|
215
|
+
|
216
|
+
or individually per command run:
|
217
|
+
|
218
|
+
```rub
|
219
|
+
cmd = TTY::Command.new
|
220
|
+
cmd.run("echo hello", uuid: false)
|
221
|
+
# =>
|
222
|
+
# Running echo hello
|
223
|
+
# hello
|
224
|
+
# Finished in 0.003 seconds with exit status 0 (successful)
|
205
225
|
```
|
206
226
|
|
207
227
|
#### 2.3.3 Only output on error
|
@@ -210,13 +230,13 @@ When using a command that can fail, setting `:only_output_on_error` option to `t
|
|
210
230
|
|
211
231
|
```ruby
|
212
232
|
cmd = TTY::Command.new
|
213
|
-
cmd.run(
|
233
|
+
cmd.run("non_failing_command", only_output_on_error: true)
|
214
234
|
```
|
215
235
|
|
216
236
|
This will only print the `Running` and `Finished` lines, while:
|
217
237
|
|
218
238
|
```ruby
|
219
|
-
cmd.run(
|
239
|
+
cmd.run("non_failing_command")
|
220
240
|
```
|
221
241
|
|
222
242
|
will also print any output that the `non_failing_command` might generate.
|
@@ -224,13 +244,13 @@ will also print any output that the `non_failing_command` might generate.
|
|
224
244
|
Running either:
|
225
245
|
|
226
246
|
```ruby
|
227
|
-
cmd.run(
|
247
|
+
cmd.run("failing_command", only_output_on_error: true)
|
228
248
|
```
|
229
249
|
|
230
250
|
either:
|
231
251
|
|
232
252
|
```ruby
|
233
|
-
cmd.run(
|
253
|
+
cmd.run("failing_command")
|
234
254
|
```
|
235
255
|
|
236
256
|
will also print the output.
|
@@ -251,7 +271,7 @@ Sometimes it can be useful to put your script into a "dry run" mode that prints
|
|
251
271
|
|
252
272
|
```ruby
|
253
273
|
cmd = TTY::Command.new(dry_run: true)
|
254
|
-
cmd.run(:rm,
|
274
|
+
cmd.run(:rm, "all_my_files")
|
255
275
|
# => [123abc] (dry run) rm all_my_files
|
256
276
|
```
|
257
277
|
|
@@ -266,7 +286,7 @@ cmd.dry_run? # => true
|
|
266
286
|
If you need to wait for a long running script and stop it when a given pattern has been matched use `wait` like so:
|
267
287
|
|
268
288
|
```ruby
|
269
|
-
cmd.wait
|
289
|
+
cmd.wait "tail -f /var/log/production.log", /something happened/
|
270
290
|
```
|
271
291
|
|
272
292
|
### 2.6 Test
|
@@ -274,7 +294,7 @@ cmd.wait 'tail -f /var/log/production.log', /something happened/
|
|
274
294
|
To simulate classic bash test command you case use `test` method with expression to check as a first argument:
|
275
295
|
|
276
296
|
```ruby
|
277
|
-
if cmd.test
|
297
|
+
if cmd.test "-e /etc/passwd"
|
278
298
|
puts "Sweet..."
|
279
299
|
else
|
280
300
|
puts "Ohh no! Where is it?"
|
@@ -297,24 +317,24 @@ cmd.ruby %q{-e "puts 'Hello world'"}
|
|
297
317
|
The environment variables need to be provided as hash entries, that can be set directly as a first argument:
|
298
318
|
|
299
319
|
```ruby
|
300
|
-
cmd.run({
|
320
|
+
cmd.run({"RAILS_ENV" => "PRODUCTION"}, :rails, "server")
|
301
321
|
```
|
302
322
|
|
303
323
|
or as an option with `:env` key:
|
304
324
|
|
305
325
|
```ruby
|
306
|
-
cmd.run(:rails,
|
326
|
+
cmd.run(:rails, "server", env: {rails_env: :production})
|
307
327
|
```
|
308
328
|
|
309
329
|
When a value in env is nil, the variable is unset in the child process:
|
310
330
|
|
311
331
|
```ruby
|
312
|
-
cmd.run(:echo,
|
332
|
+
cmd.run(:echo, "hello", env: {foo: "bar", baz: nil})
|
313
333
|
```
|
314
334
|
|
315
335
|
### 3.2 Options
|
316
336
|
|
317
|
-
When a hash is given in the last argument (options), it allows to specify a current directory, umask, user, group and
|
337
|
+
When a hash is given in the last argument (options), it allows to specify a current directory, umask, user, group and zero or more fd redirects for the child process.
|
318
338
|
|
319
339
|
#### 3.2.1 Redirection
|
320
340
|
|
@@ -347,27 +367,27 @@ The hash key and value specify a file descriptor in the child process (stderr &
|
|
347
367
|
You can also redirect to a file:
|
348
368
|
|
349
369
|
```ruby
|
350
|
-
cmd.run(:cat, :in =>
|
351
|
-
cmd.run(:cat, :in => open(
|
352
|
-
cmd.run(:ls, :out =>
|
370
|
+
cmd.run(:cat, :in => "file")
|
371
|
+
cmd.run(:cat, :in => open("/etc/passwd"))
|
372
|
+
cmd.run(:ls, :out => "log")
|
353
373
|
cmd.run(:ls, :out => "/dev/null")
|
354
|
-
cmd.run(:ls, :out =>
|
374
|
+
cmd.run(:ls, :out => "out.log", :err => "err.log")
|
355
375
|
cmd.run(:ls, [:out, :err] => "log")
|
356
|
-
cmd.run("ls 1>&2", :err =>
|
376
|
+
cmd.run("ls 1>&2", :err => "log")
|
357
377
|
```
|
358
378
|
|
359
379
|
It is possible to specify flags and permissions of file creation explicitly by passing an array value:
|
360
380
|
|
361
381
|
```ruby
|
362
|
-
cmd.run(:ls, :out => [
|
363
|
-
cmd.run(:ls, :out => [
|
364
|
-
cmd.run(:ls, :out => [
|
382
|
+
cmd.run(:ls, :out => ["log", "w"]) # 0664 assumed
|
383
|
+
cmd.run(:ls, :out => ["log", "w", 0600])
|
384
|
+
cmd.run(:ls, :out => ["log", File::WRONLY|File::EXCL|File::CREAT, 0600])
|
365
385
|
```
|
366
386
|
|
367
387
|
You can, for example, read data from one source and output to another:
|
368
388
|
|
369
389
|
```ruby
|
370
|
-
cmd.run("cat", :in => "Gemfile", :out =>
|
390
|
+
cmd.run("cat", :in => "Gemfile", :out => "gemfile.log")
|
371
391
|
```
|
372
392
|
|
373
393
|
#### 3.2.2 Handling Input
|
@@ -382,7 +402,7 @@ puts "Your name: #{name}"
|
|
382
402
|
In order to execute `cli` with name input do:
|
383
403
|
|
384
404
|
```ruby
|
385
|
-
cmd.run(
|
405
|
+
cmd.run("cli", input: "Piotr\n")
|
386
406
|
# => Your name: Piotr
|
387
407
|
```
|
388
408
|
|
@@ -401,7 +421,7 @@ cmd.run("my_cli_program", "login", in: in_stream).out
|
|
401
421
|
|
402
422
|
#### 3.2.3 Timeout
|
403
423
|
|
404
|
-
You can timeout command
|
424
|
+
You can timeout command execution by providing the `:timeout` option in seconds:
|
405
425
|
|
406
426
|
```ruby
|
407
427
|
cmd.run("while test 1; sleep 1; done", timeout: 5)
|
@@ -431,7 +451,7 @@ cmd = TTY::Command.new(binmode: true)
|
|
431
451
|
|
432
452
|
#### 3.2.5 Signal
|
433
453
|
|
434
|
-
You can specify process termination signal other than the
|
454
|
+
You can specify process termination signal other than the default `SIGTERM`:
|
435
455
|
|
436
456
|
```ruby
|
437
457
|
cmd.run("whilte test1; sleep1; done", timeout: 5, signal: :KILL)
|
@@ -469,14 +489,14 @@ However, in `interactive` mode with `pty` flag on:
|
|
469
489
|
cmd.run("git log", pty: true) # => uses pager and waits for user input (never returns)
|
470
490
|
```
|
471
491
|
|
472
|
-
In addition, when pty device is used, any input to command may be echoed to the standard output, as well as some
|
492
|
+
In addition, when pty device is used, any input to command may be echoed to the standard output, as well as some redirects may not work.
|
473
493
|
|
474
494
|
#### 3.2.7 Current directory
|
475
495
|
|
476
496
|
To change directory in which the command is run pass the `:chdir` option:
|
477
497
|
|
478
498
|
```ruby
|
479
|
-
cmd.run(:echo,
|
499
|
+
cmd.run(:echo, "hello", chdir: "/var/tmp")
|
480
500
|
```
|
481
501
|
|
482
502
|
#### 3.2.8 User
|
@@ -484,7 +504,7 @@ cmd.run(:echo, 'hello', chdir: '/var/tmp')
|
|
484
504
|
To run command as a given user do:
|
485
505
|
|
486
506
|
```ruby
|
487
|
-
cmd.run(:echo,
|
507
|
+
cmd.run(:echo, "hello", user: "piotr")
|
488
508
|
```
|
489
509
|
|
490
510
|
#### 3.2.9 Group
|
@@ -492,7 +512,7 @@ cmd.run(:echo, 'hello', user: 'piotr')
|
|
492
512
|
To run command as part of group do:
|
493
513
|
|
494
514
|
```ruby
|
495
|
-
cmd.run(:echo,
|
515
|
+
cmd.run(:echo, "hello", group: "devs")
|
496
516
|
```
|
497
517
|
|
498
518
|
#### 3.2.10 Umask
|
@@ -500,7 +520,7 @@ cmd.run(:echo, 'hello', group: 'devs')
|
|
500
520
|
To run command with umask do:
|
501
521
|
|
502
522
|
```ruby
|
503
|
-
cmd.run(:echo,
|
523
|
+
cmd.run(:echo, "hello", umask: "007")
|
504
524
|
```
|
505
525
|
|
506
526
|
### 3.3 Result
|
@@ -508,13 +528,13 @@ cmd.run(:echo, 'hello', umask: '007')
|
|
508
528
|
Each time you run command the stdout and stderr are captured and return as result. The result can be examined directly by casting it to tuple:
|
509
529
|
|
510
530
|
```ruby
|
511
|
-
out, err = cmd.run(:echo,
|
531
|
+
out, err = cmd.run(:echo, "Hello")
|
512
532
|
```
|
513
533
|
|
514
534
|
However, if you want to you can defer reading:
|
515
535
|
|
516
536
|
```ruby
|
517
|
-
result = cmd.run(:echo,
|
537
|
+
result = cmd.run(:echo, "Hello")
|
518
538
|
result.out
|
519
539
|
result.err
|
520
540
|
```
|
@@ -524,7 +544,7 @@ result.err
|
|
524
544
|
To check if command exited successfully use `success?`:
|
525
545
|
|
526
546
|
```ruby
|
527
|
-
result = cmd.run(:echo,
|
547
|
+
result = cmd.run(:echo, "Hello")
|
528
548
|
result.success? # => true
|
529
549
|
```
|
530
550
|
|
@@ -533,7 +553,7 @@ result.success? # => true
|
|
533
553
|
To check if command exited unsuccessfully use `failure?` or `failed?`:
|
534
554
|
|
535
555
|
```ruby
|
536
|
-
result = cmd.run(:echo,
|
556
|
+
result = cmd.run(:echo, "Hello")
|
537
557
|
result.failure? # => false
|
538
558
|
result.failed? # => false
|
539
559
|
```
|
@@ -543,7 +563,7 @@ result.failed? # => false
|
|
543
563
|
To check if command ran to completion use `exited?` or `complete?`:
|
544
564
|
|
545
565
|
```ruby
|
546
|
-
result = cmd.run(:echo,
|
566
|
+
result = cmd.run(:echo, "Hello")
|
547
567
|
result.exited? # => true
|
548
568
|
result.complete? # => true
|
549
569
|
```
|
@@ -553,7 +573,7 @@ result.complete? # => true
|
|
553
573
|
The result itself is an enumerable and allows you to iterate over the stdout output:
|
554
574
|
|
555
575
|
```ruby
|
556
|
-
result = cmd.run(:ls,
|
576
|
+
result = cmd.run(:ls, "-1")
|
557
577
|
result.each { |line| puts line }
|
558
578
|
# =>
|
559
579
|
# CHANGELOG.md
|
@@ -576,26 +596,29 @@ TTY::Command.record_separator = "\n\r"
|
|
576
596
|
or configured per `each` call by passing delimiter as an argument:
|
577
597
|
|
578
598
|
```ruby
|
579
|
-
cmd.run(:ls,
|
599
|
+
cmd.run(:ls, "-1").each("\t") { ... }
|
580
600
|
```
|
581
601
|
|
582
602
|
### 3.4 Custom printer
|
583
603
|
|
584
|
-
If the built-in printers do not meet your requirements you can create your own.
|
604
|
+
If the built-in printers do not meet your requirements you can create your own. A printer is a regular Ruby class that can be registered through `:printer` option to receive notifications about received command data.
|
605
|
+
|
606
|
+
As the command runs the custom printer will be notified when the command starts, when data is printed to stdout, when data is printed to stderr and when the command exits.
|
607
|
+
|
608
|
+
Please see [lib/tty/command/printers/abstract.rb](https://github.com/piotrmurach/tty-command/blob/master/lib/tty/command/printers/abstract.rb) for a full set of methods that you can override.
|
609
|
+
|
610
|
+
At the very minimum you need to specify the `write` method that will be called during the lifecycle of command execution. The `write` accepts two arguments, first the currently run command instance and second the message to be printed:
|
585
611
|
|
586
612
|
```ruby
|
587
613
|
CustomPrinter < TTY::Command::Printers::Abstract
|
588
|
-
def write(message)
|
589
|
-
puts message
|
614
|
+
def write(cmd, message)
|
615
|
+
puts cmd.to_command + message
|
590
616
|
end
|
591
617
|
end
|
592
618
|
|
593
|
-
|
594
|
-
|
595
|
-
cmd = TTY::Command.new(printer: printer)
|
619
|
+
cmd = TTY::Command.new(printer: CustomPrinter)
|
596
620
|
```
|
597
621
|
|
598
|
-
|
599
622
|
## 4. Example
|
600
623
|
|
601
624
|
Here's a slightly more elaborate example to illustrate how tty-command can improve on plain old shell scripts. This example installs a new version of Ruby on an Ubuntu machine.
|
@@ -635,4 +658,4 @@ The gem is available as open source under the terms of the [MIT License](http://
|
|
635
658
|
|
636
659
|
## Copyright
|
637
660
|
|
638
|
-
Copyright (c) 2016
|
661
|
+
Copyright (c) 2016 Piotr Murach. See LICENSE for further details.
|