qbash 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +44 -13
- data/lib/qbash.rb +18 -10
- data/qbash.gemspec +1 -1
- data/test/test_qbash.rb +4 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 513c4fae2df78a6b4ae254b80dcd5e306c34a68cb8c1e869c3073ee612504d41
|
4
|
+
data.tar.gz: 424695c22db403cfa8ad40def087742cb804a08e4d5c858f378c02ebd3a12303
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fd4f01dd7ba2cbc6da044367d45d14a07e699c632e9ce421674b151095972465e7e3cd86e13bc3efcc80b29a33c9d62cf976c94b56db82578026a0734ba64e3
|
7
|
+
data.tar.gz: e71a4444310641326ce9eb07d312eeaa5157f1c78faf8dcc0c1effc068df8ce50a3bcc3b184849ade9079bfb936a076bf774834ba1590191fe81ea62c835f8b9
|
data/README.md
CHANGED
@@ -1,15 +1,21 @@
|
|
1
1
|
# Quick and Simple Executor of Bash Commands
|
2
2
|
|
3
|
-
[![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/
|
3
|
+
[![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/qbash)](http://www.rultor.com/p/yegor256/qbash)
|
4
4
|
[![We recommend RubyMine](https://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
|
5
5
|
|
6
|
-
[![rake](https://github.com/yegor256/
|
7
|
-
[![PDD status](http://www.0pdd.com/svg?name=yegor256/
|
8
|
-
[![Gem Version](https://badge.fury.io/rb/
|
9
|
-
[![Test Coverage](https://img.shields.io/codecov/c/github/yegor256/
|
10
|
-
[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/github/yegor256/
|
11
|
-
[![Hits-of-Code](https://hitsofcode.com/github/yegor256/
|
12
|
-
[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/
|
6
|
+
[![rake](https://github.com/yegor256/qbash/actions/workflows/rake.yml/badge.svg)](https://github.com/yegor256/qbash/actions/workflows/rake.yml)
|
7
|
+
[![PDD status](http://www.0pdd.com/svg?name=yegor256/qbash)](http://www.0pdd.com/p?name=yegor256/qbash)
|
8
|
+
[![Gem Version](https://badge.fury.io/rb/qbash.svg)](http://badge.fury.io/rb/qbash)
|
9
|
+
[![Test Coverage](https://img.shields.io/codecov/c/github/yegor256/qbash.svg)](https://codecov.io/github/yegor256/qbash?branch=master)
|
10
|
+
[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/github/yegor256/qbash/master/frames)
|
11
|
+
[![Hits-of-Code](https://hitsofcode.com/github/yegor256/qbash)](https://hitsofcode.com/view/github/yegor256/qbash)
|
12
|
+
[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/qbash/blob/master/LICENSE.txt)
|
13
|
+
|
14
|
+
How do you execute a new shell command from Ruby?
|
15
|
+
There are [many ways](https://stackoverflow.com/questions/2232).
|
16
|
+
None of them offers a one-liner that would execute a command, print
|
17
|
+
its output to the console or a logger, and then raise an exception if
|
18
|
+
the exit code is not zero. This small gem offers exactly this one-liner.
|
13
19
|
|
14
20
|
First, install it:
|
15
21
|
|
@@ -17,11 +23,11 @@ First, install it:
|
|
17
23
|
gem install qbash
|
18
24
|
```
|
19
25
|
|
20
|
-
|
26
|
+
Then, you can use [qbash][qbash] global function:
|
21
27
|
|
22
28
|
```ruby
|
23
29
|
require 'qbash'
|
24
|
-
stdout = qbash('echo "Hello, world!"')
|
30
|
+
stdout = qbash('echo "Hello, world!"', log: $stdout)
|
25
31
|
```
|
26
32
|
|
27
33
|
If the command fails, an exception will be raised.
|
@@ -32,8 +38,14 @@ It's possible to provide the standard input and environment variables:
|
|
32
38
|
stdout = qbash('cat > $FILE', env: { 'FILE' => 'a.txt' }, stdin: 'Hello!')
|
33
39
|
```
|
34
40
|
|
35
|
-
It's possible to configure the logging facility too, with the help
|
36
|
-
of the [loog](https://github.com/yegor256/loog) gem
|
41
|
+
It's possible to configure the logging facility too, for example, with the help
|
42
|
+
of the [loog](https://github.com/yegor256/loog) gem (the output
|
43
|
+
will be returned _and_ printed to the logger):
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'loog'
|
47
|
+
qbash('echo "Hello, world!"', log: Loog::VERBOSE)
|
48
|
+
```
|
37
49
|
|
38
50
|
You can also make it return both stdout and exit code, with the help
|
39
51
|
of the `both` option set to `true`:
|
@@ -49,8 +61,26 @@ acceptable (there will be no failures ever).
|
|
49
61
|
The command may be provided as an array, which automatically will be
|
50
62
|
converted to a string by joining all items with spaces between them.
|
51
63
|
|
64
|
+
```ruby
|
65
|
+
qbash(
|
66
|
+
[
|
67
|
+
'echo "Hello, world!"'
|
68
|
+
'&& echo "How are you?"',
|
69
|
+
'&& cat /etc/passwd'
|
70
|
+
]
|
71
|
+
)
|
72
|
+
```
|
73
|
+
|
52
74
|
It is very much recommended to escape all command-line values with the help
|
53
|
-
of [Shellwords.escape][shellwords]
|
75
|
+
of the [Shellwords.escape()][shellwords] utility method, for example:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
file = '/tmp/test.txt'
|
79
|
+
qbash("cat #{Shellwords.escape(file)}")
|
80
|
+
```
|
81
|
+
|
82
|
+
Without such an escaping, in this example, a space inside the `file`
|
83
|
+
will lead to an unpredicatable result of the execution.
|
54
84
|
|
55
85
|
## How to contribute
|
56
86
|
|
@@ -69,3 +99,4 @@ bundle exec rake
|
|
69
99
|
If it's clean and you don't see any error messages, submit your pull request.
|
70
100
|
|
71
101
|
[shellwords]: https://ruby-doc.org/stdlib-3.0.1/libdoc/shellwords/rdoc/Shellwords.html
|
102
|
+
[qbash]: https://rubydoc.info/github/yegor256/qbash/master/Kernel#qbash-instance_method
|
data/lib/qbash.rb
CHANGED
@@ -37,18 +37,22 @@ module Kernel
|
|
37
37
|
#
|
38
38
|
# To escape arguments, use +Shellwords.escape()+ method.
|
39
39
|
#
|
40
|
-
# @param [String] cmd The command to run
|
41
|
-
# @param [String] stdin
|
42
|
-
# @param [Hash] env
|
43
|
-
# @param [Loog]
|
44
|
-
# @param [Array] accept List of accepted exit codes (
|
40
|
+
# @param [String] cmd The command to run, for example +echo "Hello, world!"+
|
41
|
+
# @param [String] stdin The +stdin+ to provide to the command
|
42
|
+
# @param [Hash] env Hash of environment variables
|
43
|
+
# @param [Loog|IO] log Logging facility with +.debug()+ method (or +$stdout+)
|
44
|
+
# @param [Array] accept List of accepted exit codes (accepts all if the list is empty)
|
45
45
|
# @param [Boolean] both If set to TRUE, the function returns an array +(stdout, code)+
|
46
|
-
# @return [String]
|
47
|
-
def qbash(cmd, stdin: '', env: {},
|
48
|
-
|
46
|
+
# @return [String] Everything that was printed to the +stdout+ by the command
|
47
|
+
def qbash(cmd, stdin: '', env: {}, log: Loog::NULL, accept: [0], both: false)
|
48
|
+
cmd = cmd.join(' ') if cmd.is_a?(Array)
|
49
|
+
if log.respond_to?(:debug)
|
50
|
+
log.debug("+ #{cmd}")
|
51
|
+
else
|
52
|
+
log.print("+ #{cmd}\n")
|
53
|
+
end
|
49
54
|
buf = ''
|
50
55
|
e = 1
|
51
|
-
cmd = cmd.join(' ') if cmd.is_a?(Array)
|
52
56
|
Open3.popen2e(env, "/bin/bash -c #{Shellwords.escape(cmd)}") do |sin, sout, thr|
|
53
57
|
sin.write(stdin)
|
54
58
|
sin.close
|
@@ -58,7 +62,11 @@ module Kernel
|
|
58
62
|
rescue IOError => e
|
59
63
|
ln = Backtrace.new(e).to_s
|
60
64
|
end
|
61
|
-
|
65
|
+
if log.respond_to?(:debug)
|
66
|
+
log.debug(ln)
|
67
|
+
else
|
68
|
+
log.print("#{ln}\n")
|
69
|
+
end
|
62
70
|
buf += ln
|
63
71
|
end
|
64
72
|
e = thr.value.to_i
|
data/qbash.gemspec
CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
|
27
27
|
s.required_ruby_version = '>=3.2'
|
28
28
|
s.name = 'qbash'
|
29
|
-
s.version = '0.0.
|
29
|
+
s.version = '0.0.4'
|
30
30
|
s.license = 'MIT'
|
31
31
|
s.summary = 'Quick Executor of a BASH Command'
|
32
32
|
s.description =
|
data/test/test_qbash.rb
CHANGED
@@ -44,6 +44,10 @@ class TestQbash < Minitest::Test
|
|
44
44
|
assert_equal('123', qbash(['printf 1;', 'printf 2;', 'printf 3']))
|
45
45
|
end
|
46
46
|
|
47
|
+
def test_log_to_console
|
48
|
+
qbash('echo Hello world!', log: $stdout)
|
49
|
+
end
|
50
|
+
|
47
51
|
def test_with_stdin
|
48
52
|
Dir.mktmpdir do |home|
|
49
53
|
f = File.join(home, 'a b c.txt')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qbash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
11
|
+
date: 2024-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: backtrace
|