qbash 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/qbash.rb +9 -3
- data/qbash.gemspec +1 -1
- data/test/test_qbash.rb +6 -2
- 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: 413276b3af88e8343fc3a872a6c41141525cf5a9977e4a78bf4f51954f949ef9
|
4
|
+
data.tar.gz: 2dccf907789c415829801f0fc3c6caec76a19e98fb2f86009c56e2097778d7c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43bd520c7e65fe1a9124beea490c45fb416927b79399892de8c58ff7ddde1ea65ef9dcf1a5c1e759cec0b8b8037c7378a37559e211d36d6d4f632bb67db43e4d
|
7
|
+
data.tar.gz: bf9eea333fa3f58a275b23e5ee8d8ae899bc4ce28c686ab32e158265b49be6a4b945a81115286b1f0bd7801bb77d900d8e6c88efcde8f64db36dbdb4a9f58101
|
data/README.md
CHANGED
@@ -32,6 +32,9 @@ stdout = qbash('echo "Hello, world!"', log: $stdout)
|
|
32
32
|
|
33
33
|
If the command fails, an exception will be raised.
|
34
34
|
|
35
|
+
The function automatically merges `stderr` with `stdout`
|
36
|
+
(you can't change this).
|
37
|
+
|
35
38
|
It's possible to provide the standard input and environment variables:
|
36
39
|
|
37
40
|
```ruby
|
data/lib/qbash.rb
CHANGED
@@ -34,22 +34,28 @@ require 'tago'
|
|
34
34
|
module Kernel
|
35
35
|
# Execute a single bash command.
|
36
36
|
#
|
37
|
+
# For example:
|
38
|
+
#
|
39
|
+
# year = qbash('date +%Y')
|
40
|
+
#
|
37
41
|
# If exit code is not zero, an exception will be raised.
|
38
42
|
#
|
39
43
|
# To escape arguments, use +Shellwords.escape()+ method.
|
40
44
|
#
|
45
|
+
# Stderr automatically merges with stdout.
|
46
|
+
#
|
41
47
|
# Read this <a href="https://github.com/yegor256/qbash">README</a> file for more details.
|
42
48
|
#
|
43
49
|
# @param [String] cmd The command to run, for example +echo "Hello, world!"+
|
44
50
|
# @param [String] stdin The +stdin+ to provide to the command
|
45
51
|
# @param [Hash] env Hash of environment variables
|
46
52
|
# @param [Loog|IO] log Logging facility with +.debug()+ method (or +$stdout+)
|
47
|
-
# @param [Array] accept List of accepted exit codes (accepts all if the list is
|
53
|
+
# @param [Array] accept List of accepted exit codes (accepts all if the list is +nil+)
|
48
54
|
# @param [Boolean] both If set to TRUE, the function returns an array +(stdout, code)+
|
49
55
|
# @param [Integer] timeout If it's set to non-NIL, the execution will fail after this number of seconds
|
50
56
|
# @return [String] Everything that was printed to the +stdout+ by the command
|
51
57
|
def qbash(cmd, stdin: '', env: {}, log: Loog::NULL, accept: [0], both: false, timeout: nil)
|
52
|
-
cmd = cmd.join(' ') if cmd.is_a?(Array)
|
58
|
+
cmd = cmd.reject { |a| a.nil? || a.empty? }.join(' ') if cmd.is_a?(Array)
|
53
59
|
if log.respond_to?(:debug)
|
54
60
|
log.debug("+ #{cmd}")
|
55
61
|
else
|
@@ -77,7 +83,7 @@ module Kernel
|
|
77
83
|
buf += ln
|
78
84
|
end
|
79
85
|
e = thr.value.to_i
|
80
|
-
if !accept.
|
86
|
+
if !accept.nil? && !accept.include?(e)
|
81
87
|
raise "The command '#{cmd}' failed with exit code ##{e} in #{start.ago}\n#{buf}"
|
82
88
|
end
|
83
89
|
end
|
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.7'
|
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
@@ -48,6 +48,10 @@ class TestQbash < Minitest::Test
|
|
48
48
|
qbash('echo Hello world!', log: $stdout)
|
49
49
|
end
|
50
50
|
|
51
|
+
def test_skip_nil
|
52
|
+
assert_equal('Hi!', qbash(['printf', nil, 'Hi!', '']))
|
53
|
+
end
|
54
|
+
|
51
55
|
def test_with_stdin
|
52
56
|
Dir.mktmpdir do |home|
|
53
57
|
f = File.join(home, 'a b c.txt')
|
@@ -75,13 +79,13 @@ class TestQbash < Minitest::Test
|
|
75
79
|
|
76
80
|
def test_ignore_errors
|
77
81
|
Dir.mktmpdir do |home|
|
78
|
-
qbash("cat #{Shellwords.escape(File.join(home, 'b.txt'))}", accept:
|
82
|
+
qbash("cat #{Shellwords.escape(File.join(home, 'b.txt'))}", accept: nil)
|
79
83
|
end
|
80
84
|
end
|
81
85
|
|
82
86
|
def test_with_both
|
83
87
|
Dir.mktmpdir do |home|
|
84
|
-
stdout, code = qbash("cat #{Shellwords.escape(File.join(home, 'foo.txt'))}", accept:
|
88
|
+
stdout, code = qbash("cat #{Shellwords.escape(File.join(home, 'foo.txt'))}", accept: nil, both: true)
|
85
89
|
assert(code.positive?)
|
86
90
|
assert(!stdout.empty?)
|
87
91
|
end
|
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.7
|
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-10-
|
11
|
+
date: 2024-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: backtrace
|