qbash 0.0.2 → 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: 74362bdd955fbbdf87abf2855c79d2aeecab2578f8bbc5f5b35003b4935c4e17
4
- data.tar.gz: 4153458c48d71d04d6d94fda9e589149e97519a9cded80ca1f1ad059ab06c75e
3
+ metadata.gz: d3ac44ac6da719be360ee7785811de7c2b465efbe274c8ae9f7b0992647c05c0
4
+ data.tar.gz: cf4e840be85b2b9fc19df98fc2e7f45f45b6c93a525b209a4f715be104870ef5
5
5
  SHA512:
6
- metadata.gz: 4c728817527bd343c532664a39f3ee8eb8be3ca172cbf974e9a7d410c0eb9fa34a11c264d53fe0d6daddf33d09aca96b9355d3985121a85bb18ca69cd1be5fae
7
- data.tar.gz: f5a5a9edb0de50f1c598e626173e6d4252e8db06cfe256632eb5002ef0393c92c046080d64892a8758eb40e32972753b2c58a2af6dbbaf34623284057c9ed65a
6
+ metadata.gz: 5eaf974307cfb04ead1c2a890c6c34cb68c1ba9a1f4a3e1f2b858f5eda0579361296fb5e95f8fd3349455f07b5acf4ea8a9fac400f6cde5ace0d1f7381b64cb9
7
+ data.tar.gz: 554613f18dd98fcac12d7e0d4ce56f1cc8d0aaeaea5866fad42ef41f5bb96dfaddb37d00b75fdefde6665eb0b59f1f1add15668d618379e1e14829457b87844f
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,13 @@ 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
+ ```
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,15 +37,19 @@ 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 Input string
42
- # @param [Hash] env Environment variables
43
- # @param [Loog] loog Logging facility with +.debug()+ method
44
- # @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)
45
45
  # @param [Boolean] both If set to TRUE, the function returns an array +(stdout, code)+
46
- # @return [String] Stdout
47
- def qbash(cmd, stdin: '', env: {}, loog: Loog::NULL, accept: [0], both: false)
48
- 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
49
53
  buf = ''
50
54
  e = 1
51
55
  cmd = cmd.join(' ') if cmd.is_a?(Array)
@@ -58,7 +62,11 @@ module Kernel
58
62
  rescue IOError => e
59
63
  ln = Backtrace.new(e).to_s
60
64
  end
61
- loog.debug(ln)
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.2'
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.2
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-17 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