qbash 0.0.1 → 0.0.3

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
  SHA256:
3
- metadata.gz: fccb41b161a6c90a23d521fa77e25afa0524e90fe208e3438c9b4d5af83d6c78
4
- data.tar.gz: 85260c4600d72f50d1c3fe38030ea9a939c62e335c5ca2851b25d9d5a2c68696
3
+ metadata.gz: d3ac44ac6da719be360ee7785811de7c2b465efbe274c8ae9f7b0992647c05c0
4
+ data.tar.gz: cf4e840be85b2b9fc19df98fc2e7f45f45b6c93a525b209a4f715be104870ef5
5
5
  SHA512:
6
- metadata.gz: c709f5c9ca3b1683d11e9bbc019e84fbf4a9c782e043fe36f88b0467f3a63123713eea48a0d8bd2f24c3ebb9fbbb2edaf4c796b0dc73615ccea8e4f5bb248953
7
- data.tar.gz: 2343e378d673146501843e35998b7e3efbd0ca8f32e3ee1e67b46dc96319d0f1e0fa23caf118da2e883135360b00a168ce7b4e5809bcd8e6c02726273ee87585
6
+ metadata.gz: 5eaf974307cfb04ead1c2a890c6c34cb68c1ba9a1f4a3e1f2b858f5eda0579361296fb5e95f8fd3349455f07b5acf4ea8a9fac400f6cde5ace0d1f7381b64cb9
7
+ data.tar.gz: 554613f18dd98fcac12d7e0d4ce56f1cc8d0aaeaea5866fad42ef41f5bb96dfaddb37d00b75fdefde6665eb0b59f1f1add15668d618379e1e14829457b87844f
data/Gemfile CHANGED
@@ -31,7 +31,7 @@ gem 'rake', '13.2.1', require: false
31
31
  gem 'random-port', '~>0.0', require: false
32
32
  gem 'rspec-rails', '7.0.1', require: false
33
33
  gem 'rubocop', '1.66.1', require: false
34
- gem 'rubocop-performance', '1.21.1', require: false
34
+ gem 'rubocop-performance', '1.22.1', require: false
35
35
  gem 'rubocop-rspec', '3.0.5', require: false
36
36
  gem 'simplecov', '0.22.0', require: false
37
37
  gem 'simplecov-cobertura', '2.1.0', require: false
data/Gemfile.lock CHANGED
@@ -196,7 +196,7 @@ GEM
196
196
  unicode-display_width (>= 2.4.0, < 3.0)
197
197
  rubocop-ast (1.32.3)
198
198
  parser (>= 3.3.1.0)
199
- rubocop-performance (1.21.1)
199
+ rubocop-performance (1.22.1)
200
200
  rubocop (>= 1.48.1, < 2.0)
201
201
  rubocop-ast (>= 1.31.1, < 2.0)
202
202
  rubocop-rspec (3.0.5)
@@ -252,7 +252,7 @@ DEPENDENCIES
252
252
  random-port (~> 0.0)
253
253
  rspec-rails (= 7.0.1)
254
254
  rubocop (= 1.66.1)
255
- rubocop-performance (= 1.21.1)
255
+ rubocop-performance (= 1.22.1)
256
256
  rubocop-rspec (= 3.0.5)
257
257
  simplecov (= 0.22.0)
258
258
  simplecov-cobertura (= 2.1.0)
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/bash)](http://www.rultor.com/p/yegor256/bash)
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/bash/actions/workflows/rake.yml/badge.svg)](https://github.com/yegor256/bash/actions/workflows/rake.yml)
7
- [![PDD status](http://www.0pdd.com/svg?name=yegor256/bash)](http://www.0pdd.com/p?name=yegor256/bash)
8
- [![Gem Version](https://badge.fury.io/rb/bash.svg)](http://badge.fury.io/rb/bash)
9
- [![Test Coverage](https://img.shields.io/codecov/c/github/yegor256/bash.svg)](https://codecov.io/github/yegor256/bash?branch=master)
10
- [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/github/yegor256/bash/master/frames)
11
- [![Hits-of-Code](https://hitsofcode.com/github/yegor256/bash)](https://hitsofcode.com/view/github/yegor256/bash)
12
- [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/bash/blob/master/LICENSE.txt)
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,7 +23,7 @@ First, install it:
17
23
  gem install qbash
18
24
  ```
19
25
 
20
- Simply execute a bash command from Ruby:
26
+ Then, you can use [qbash][qbash] global function:
21
27
 
22
28
  ```ruby
23
29
  require 'qbash'
@@ -33,7 +39,48 @@ stdout = qbash('cat > $FILE', env: { 'FILE' => 'a.txt' }, stdin: 'Hello!')
33
39
  ```
34
40
 
35
41
  It's possible to configure the logging facility too, with the help
36
- of the [loog](https://github.com/yegor256/loog) gem.
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!"', loog: Loog::VERBOSE)
48
+ ```
49
+
50
+ You can also make it return both stdout and exit code, with the help
51
+ of the `both` option set to `true`:
52
+
53
+ ```ruby
54
+ stdout, code = qbash('cat a.txt', both: true, accept: [])
55
+ ```
56
+
57
+ Here, the `accept` param contains the list of exit codes that are "acceptable"
58
+ and won't lead to runtime failures. When the list is empty, all exists are
59
+ acceptable (there will be no failures ever).
60
+
61
+ The command may be provided as an array, which automatically will be
62
+ converted to a string by joining all items with spaces between them.
63
+
64
+ ```ruby
65
+ qbash(
66
+ [
67
+ 'echo "Hello, world!"'
68
+ '&& echo "How are you?"',
69
+ '&& cat /etc/passwd'
70
+ ]
71
+ )
72
+ ```
73
+
74
+ It is very much recommended to escape all command-line values with the help
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.
37
84
 
38
85
  ## How to contribute
39
86
 
@@ -50,3 +97,6 @@ bundle exec rake
50
97
  ```
51
98
 
52
99
  If it's clean and you don't see any error messages, submit your pull request.
100
+
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
@@ -20,9 +20,10 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
- require 'open3'
24
- require 'loog'
25
23
  require 'backtrace'
24
+ require 'loog'
25
+ require 'open3'
26
+ require 'shellwords'
26
27
 
27
28
  # Execute one bash command.
28
29
  #
@@ -36,15 +37,19 @@ module Kernel
36
37
  #
37
38
  # To escape arguments, use +Shellwords.escape()+ method.
38
39
  #
39
- # @param [String] cmd The command to run
40
- # @param [String] stdin Input string
41
- # @param [Hash] env Environment variables
42
- # @param [Loog] loog Logging facility with +.debug()+ method
43
- # @param [Array] accept List of accepted exit codes (accept all if empty)
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)
44
45
  # @param [Boolean] both If set to TRUE, the function returns an array +(stdout, code)+
45
- # @return [String] Stdout
46
- def qbash(cmd, stdin: '', env: {}, loog: Loog::NULL, accept: [0], both: false)
47
- loog.debug("+ #{cmd}")
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
+ if log.respond_to?(:debug)
49
+ log.debug("+ #{cmd}")
50
+ else
51
+ log.print("+ #{cmd}\n")
52
+ end
48
53
  buf = ''
49
54
  e = 1
50
55
  cmd = cmd.join(' ') if cmd.is_a?(Array)
@@ -57,7 +62,11 @@ module Kernel
57
62
  rescue IOError => e
58
63
  ln = Backtrace.new(e).to_s
59
64
  end
60
- loog.debug(ln)
65
+ if log.respond_to?(:debug)
66
+ log.debug(ln)
67
+ else
68
+ log.print("#{ln}\n")
69
+ end
61
70
  buf += ln
62
71
  end
63
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.1'
29
+ s.version = '0.0.3'
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.1
4
+ version: 0.0.3
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-16 00:00:00.000000000 Z
11
+ date: 2024-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace