shell_tools 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +15 -0
- data/Rakefile +8 -0
- data/lib/shell_tools.rb +58 -0
- data/lib/shell_tools/version.rb +3 -0
- data/shell_tools.gemspec +25 -0
- data/test/shell_utils_test.rb +25 -0
- data/test/test_helper.rb +4 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# ShellTools
|
2
|
+
|
3
|
+
Some common shell utilities.
|
4
|
+
|
5
|
+
## sh
|
6
|
+
|
7
|
+
Execute the command and return the output. The stderr and stdout are merged together into the output. If it exits with a non-zero status, this method will raise an error.
|
8
|
+
|
9
|
+
## sh_with_code
|
10
|
+
|
11
|
+
Execute the command and return the output and status code. The stderr and stdout are merged together into the output
|
12
|
+
|
13
|
+
## escape
|
14
|
+
|
15
|
+
Escapes any number of words and joins them into an escaped string.
|
data/Rakefile
ADDED
data/lib/shell_tools.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require "shell_tools/version"
|
2
|
+
|
3
|
+
module ShellTools
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def sh(cmd, base = nil)
|
7
|
+
out, code = sh_with_code(cmd)
|
8
|
+
code == 0 ? out : raise(out.empty? ? "Running `#{cmd}' failed. Run this command directly for more detailed output." : out)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Run in shell, return both status and output
|
12
|
+
# @see #sh
|
13
|
+
def sh_with_code(cmd, base = nil)
|
14
|
+
cmd << " 2>&1"
|
15
|
+
outbuf = ''
|
16
|
+
outbuf = `#{base && "cd '#{base}' && "}#{cmd}`
|
17
|
+
[outbuf, $?]
|
18
|
+
end
|
19
|
+
|
20
|
+
def escape(*command)
|
21
|
+
command.flatten.map {|word| escape_word(word) }.join(' ')
|
22
|
+
end
|
23
|
+
|
24
|
+
def escape_word(str)
|
25
|
+
if str.empty?
|
26
|
+
"''"
|
27
|
+
elsif %r{\A[0-9A-Za-z+,./:=@_-]+\z} =~ str
|
28
|
+
str
|
29
|
+
else
|
30
|
+
result = ''
|
31
|
+
str.scan(/('+)|[^']+/) {
|
32
|
+
if $1
|
33
|
+
result << %q{\'} * $1.length
|
34
|
+
else
|
35
|
+
result << "'#{$&}'"
|
36
|
+
end
|
37
|
+
}
|
38
|
+
result
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def capture
|
43
|
+
old_out, old_err = STDOUT.dup, STDERR.dup
|
44
|
+
stdout_read, stdout_write = IO.pipe
|
45
|
+
stderr_read, stderr_write = IO.pipe
|
46
|
+
$stdout.reopen(stdout_write)
|
47
|
+
$stderr.reopen(stderr_write)
|
48
|
+
yield
|
49
|
+
stdout_write.close
|
50
|
+
stderr_write.close
|
51
|
+
out = stdout_read.rewind && stdout_read.read rescue nil
|
52
|
+
err = stderr_read.rewind && stderr_read.read rescue nil
|
53
|
+
[out, err]
|
54
|
+
ensure
|
55
|
+
$stdout.reopen(old_out)
|
56
|
+
$stderr.reopen(old_err)
|
57
|
+
end
|
58
|
+
end
|
data/shell_tools.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "shell_tools/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "shell_tools"
|
7
|
+
s.version = ShellTools::VERSION
|
8
|
+
s.authors = ["Josh Hull"]
|
9
|
+
s.email = ["joshbuddy@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Some common shell utils}
|
12
|
+
s.description = %q{Some common shell utils.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "shell_tools"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
s.add_development_dependency "rake"
|
24
|
+
s.add_development_dependency "minitest"
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "ShellTools" do
|
4
|
+
it "should sh" do
|
5
|
+
assert_equal Time.new.to_i.to_s, ShellTools.sh("date +%s").strip
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should sh and raise" do
|
9
|
+
assert_raises RuntimeError do
|
10
|
+
ShellTools.sh("mymilkshakebringsvariousboystotheYARD")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should sh_with_code" do
|
15
|
+
out, status = ShellTools.sh_with_code("date +%s")
|
16
|
+
assert_equal Time.new.to_i.to_s, out.strip
|
17
|
+
assert_equal 0, status.to_i
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should sh_with_code and fail" do
|
21
|
+
out, status = ShellTools.sh_with_code("mymilkshakebringsvariousboystotheYARD")
|
22
|
+
assert_equal "sh: mymilkshakebringsvariousboystotheYARD: command not found", out.strip
|
23
|
+
refute_equal 0, status.to_i
|
24
|
+
end
|
25
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shell_tools
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Josh Hull
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-06 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70259743014820 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70259743014820
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: minitest
|
27
|
+
requirement: &70259743013800 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70259743013800
|
36
|
+
description: Some common shell utils.
|
37
|
+
email:
|
38
|
+
- joshbuddy@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- lib/shell_tools.rb
|
48
|
+
- lib/shell_tools/version.rb
|
49
|
+
- shell_tools.gemspec
|
50
|
+
- test/shell_utils_test.rb
|
51
|
+
- test/test_helper.rb
|
52
|
+
homepage: ''
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
hash: -759511355527622154
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
hash: -759511355527622154
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project: shell_tools
|
78
|
+
rubygems_version: 1.8.10
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Some common shell utils
|
82
|
+
test_files:
|
83
|
+
- test/shell_utils_test.rb
|
84
|
+
- test/test_helper.rb
|