guns 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +44 -0
- data/guns.gemspec +26 -0
- data/lib/guns.rb +56 -0
- metadata +57 -0
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Guns
|
2
|
+
It has two pipes; got tickets to the show?
|
3
|
+
|
4
|
+
A low overhead sh utility to replace [session](http://www.codeforpeople.com/lib/ruby/session/)
|
5
|
+
|
6
|
+
|
7
|
+
## Install
|
8
|
+
|
9
|
+
gem install guns -s http://gemcutter.org
|
10
|
+
|
11
|
+
## Flexing
|
12
|
+
|
13
|
+
DISCLAIMER: It's not recommended to use guns in place of you shell
|
14
|
+
I'm purely using IRB for demonstration
|
15
|
+
|
16
|
+
$ irb -r rubygems -r guns
|
17
|
+
>> Guns.sh "ls not-a-dir"
|
18
|
+
=> ["", "ls: not-a-dir: No such file or directory\n", 1]
|
19
|
+
|
20
|
+
The `sh` method returns a tuple containing `stdout`, `stderr`, and `exitstatus`.
|
21
|
+
|
22
|
+
### Die hard
|
23
|
+
|
24
|
+
>> Guns.sh! "ls not-a-dir"
|
25
|
+
Guns::Failure: ls: not-a-dir: No such file or directory
|
26
|
+
|
27
|
+
from ./lib/guns.rb:50:in `sh!'
|
28
|
+
from (irb):3
|
29
|
+
`sh!` will raise an error on a non-zero exit status
|
30
|
+
|
31
|
+
### Environment variables
|
32
|
+
|
33
|
+
>> Guns.sh "echo $FOO", "FOO" => "bar"
|
34
|
+
["bar\n", "", 0]
|
35
|
+
|
36
|
+
## Further Reading
|
37
|
+
|
38
|
+
If you need more flexability and features, see [Rush](http://github.com/adamwiggins/rush)
|
39
|
+
|
40
|
+
TODO: Replace popen3
|
41
|
+
|
42
|
+
## Thanks
|
43
|
+
[Joe Ruscio](http://github.com/josephruscio) for helping me debug that nasty deadlock
|
44
|
+
[Ryan Tomayko](http://www.tomayko.com) for Shotgun; where I got started with this
|
data/guns.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
3
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
4
|
+
|
5
|
+
s.name = 'guns'
|
6
|
+
s.version = '0.1.0'
|
7
|
+
s.date = '2009-10-30'
|
8
|
+
|
9
|
+
s.description = "Guns - It's got two pipes; got tickets to the show?"
|
10
|
+
s.summary = s.description
|
11
|
+
|
12
|
+
s.authors = ["Blake Mizerany"]
|
13
|
+
s.email = "blake.mizerany@gmail.com"
|
14
|
+
|
15
|
+
s.files = %w[
|
16
|
+
README.md
|
17
|
+
guns.gemspec
|
18
|
+
lib/guns.rb
|
19
|
+
]
|
20
|
+
|
21
|
+
s.extra_rdoc_files = %w[README.md]
|
22
|
+
|
23
|
+
s.homepage = "http://github.com/bmizerany/guns/"
|
24
|
+
s.require_paths = %w[lib]
|
25
|
+
s.rubygems_version = '1.1.1'
|
26
|
+
end
|
data/lib/guns.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module Guns
|
2
|
+
|
3
|
+
class Failure < Exception ; end
|
4
|
+
|
5
|
+
def sh(cmd, *args)
|
6
|
+
env = args.pop if args.last.kind_of?(Hash)
|
7
|
+
|
8
|
+
rout, wout = IO.pipe
|
9
|
+
rerr, werr = IO.pipe
|
10
|
+
|
11
|
+
# Disable GC before forking in an attempt to get some advantage
|
12
|
+
# out of COW.
|
13
|
+
|
14
|
+
GC.disable
|
15
|
+
|
16
|
+
if fork
|
17
|
+
# Parent
|
18
|
+
wout.close
|
19
|
+
werr.close
|
20
|
+
|
21
|
+
Process.wait
|
22
|
+
|
23
|
+
exitstatus = $?.exitstatus
|
24
|
+
out = rout.read
|
25
|
+
err = rerr.read
|
26
|
+
|
27
|
+
[out, err, exitstatus]
|
28
|
+
else
|
29
|
+
# Child
|
30
|
+
|
31
|
+
if env
|
32
|
+
env.each {|k,v| ENV[k] = v}
|
33
|
+
end
|
34
|
+
|
35
|
+
STDOUT.reopen(wout)
|
36
|
+
STDERR.reopen(werr)
|
37
|
+
|
38
|
+
$0 = "#$0 -> [guns] #{cmd}"
|
39
|
+
|
40
|
+
system(cmd, *args)
|
41
|
+
|
42
|
+
exit! $?.exitstatus
|
43
|
+
end
|
44
|
+
|
45
|
+
ensure
|
46
|
+
GC.enable
|
47
|
+
end
|
48
|
+
module_function :sh
|
49
|
+
|
50
|
+
def sh!(*args)
|
51
|
+
out, err, code = sh(*args)
|
52
|
+
raise Failure, err if code != 0
|
53
|
+
end
|
54
|
+
module_function :sh!
|
55
|
+
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: guns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Blake Mizerany
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-30 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Guns - It's got two pipes; got tickets to the show?
|
17
|
+
email: blake.mizerany@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.md
|
24
|
+
files:
|
25
|
+
- README.md
|
26
|
+
- guns.gemspec
|
27
|
+
- lib/guns.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://github.com/bmizerany/guns/
|
30
|
+
licenses: []
|
31
|
+
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.3.5
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: Guns - It's got two pipes; got tickets to the show?
|
56
|
+
test_files: []
|
57
|
+
|