cocaine 0.0.2 → 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/README.md +63 -39
- data/lib/cocaine/command_line.rb +15 -5
- data/lib/cocaine/version.rb +1 -1
- data/spec/cocaine/command_line_spec.rb +14 -4
- data/spec/spec_helper.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -2,75 +2,99 @@
|
|
2
2
|
|
3
3
|
A small library for doing (command) lines.
|
4
4
|
|
5
|
+
## Feedback
|
6
|
+
|
7
|
+
Question? Idea? Problem? Bug? Something else? Comment? Concern? Like use question marks?
|
8
|
+
|
9
|
+
[GitHub Issues For All!](https://github.com/thoughtbot/cocaine/issues)
|
10
|
+
|
5
11
|
## Usage
|
6
12
|
|
7
13
|
The basic, normal stuff.
|
8
14
|
|
9
|
-
|
10
|
-
|
11
|
-
|
15
|
+
```ruby
|
16
|
+
line = Cocaine::CommandLine.run("command", "some 'crazy' options")
|
17
|
+
line.command # => "command some 'crazy' options"
|
18
|
+
output = line.run # => Get you some output!
|
19
|
+
```
|
12
20
|
|
13
21
|
Allowing arguments to be dynamic.
|
14
22
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
```ruby
|
24
|
+
line = Cocaine::CommandLine.new("convert", ":in -scale :resolution :out",
|
25
|
+
:in => "omg.jpg",
|
26
|
+
:resolution => "32x32",
|
27
|
+
:out => omg_thumb.jpg")
|
28
|
+
line.command # => "convert 'omg.jpg' -scale '32x32' 'omg_thumb.jpg'"
|
29
|
+
```
|
20
30
|
|
21
31
|
It prevents attempts at being bad.
|
22
32
|
|
23
|
-
|
24
|
-
|
33
|
+
```ruby
|
34
|
+
line = Cocaine::CommandLine.new("cat", ":file", :file => "haha`rm -rf /`.txt")
|
35
|
+
line.command # => "cat 'haha`rm -rf /`.txt'"
|
25
36
|
|
26
|
-
|
27
|
-
|
37
|
+
line = Cocaine::CommandLine.new("cat", ":file", :file => "ohyeah?'`rm -rf /`.ha!")
|
38
|
+
line.command # => "cat 'ohyeah?'\\''`rm -rf /`.ha!'"
|
39
|
+
```
|
28
40
|
|
29
41
|
You can ignore the result.
|
30
42
|
|
31
|
-
|
32
|
-
|
43
|
+
```ruby
|
44
|
+
line = Cocaine::CommandLine.new("noisy", "--extra-verbose", :swallow_stderr => true)
|
45
|
+
line.command # => "noisy --extra-verbose 2>/dev/null"
|
33
46
|
|
34
|
-
|
35
|
-
|
47
|
+
# ... and on Windows...
|
48
|
+
line.command # => "noisy --extra-verbose 2>NUL"
|
49
|
+
```
|
36
50
|
|
37
51
|
If your command errors, you get an exception.
|
38
52
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
53
|
+
```ruby
|
54
|
+
line = Cocaine::CommandLine.new("git", "commit")
|
55
|
+
begin
|
56
|
+
line.run
|
57
|
+
rescue Cocaine::ExitStatusError => e
|
58
|
+
e.message # => "Command 'git commit' returned 1. Expected 0"
|
59
|
+
end
|
60
|
+
```
|
45
61
|
|
46
62
|
You don't have the command? You get an exception.
|
47
63
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
64
|
+
```ruby
|
65
|
+
line = Cocaine::CommandLine.new("lolwut")
|
66
|
+
begin
|
67
|
+
line.run
|
68
|
+
rescue Cocaine::CommandNotFoundError => e
|
69
|
+
e # => the command isn't in the $PATH for this process.
|
70
|
+
end
|
71
|
+
```
|
54
72
|
|
55
73
|
But don't fear, you can specify where to look for the command.
|
56
74
|
|
57
|
-
|
58
|
-
|
59
|
-
|
75
|
+
```ruby
|
76
|
+
Cocaine::CommandLine.path = "/opt/bin"
|
77
|
+
line = Cocaine::CommandLine.new("lolwut")
|
78
|
+
line.command # => "/opt/bin/lolwut"
|
79
|
+
```
|
60
80
|
|
61
81
|
Or, just, you know, put it in the command.
|
62
82
|
|
63
|
-
|
64
|
-
|
83
|
+
```ruby
|
84
|
+
line = Cocaine::CommandLine.new("/opt/bin/lolwut")
|
85
|
+
line.command # => "/opt/bin/lolwut"
|
86
|
+
```
|
65
87
|
|
66
88
|
If your command might return something non-zero, and you expect that, it's cool.
|
67
89
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
90
|
+
```ruby
|
91
|
+
line = Cocaine::CommandLine.new("/usr/bin/false", "", :expected_outcodes => [0, 1])
|
92
|
+
begin
|
93
|
+
line.run
|
94
|
+
rescue Cocaine::ExitStatusError => e
|
95
|
+
# => You never get here!
|
96
|
+
end
|
97
|
+
```
|
74
98
|
|
75
99
|
## License
|
76
100
|
|
data/lib/cocaine/command_line.rb
CHANGED
@@ -15,15 +15,18 @@ module Cocaine
|
|
15
15
|
|
16
16
|
def command
|
17
17
|
cmd = []
|
18
|
-
cmd <<
|
18
|
+
cmd << @binary
|
19
19
|
cmd << interpolate(@params, @options)
|
20
20
|
cmd << bit_bucket if @swallow_stderr
|
21
|
-
cmd.join(" ")
|
21
|
+
cmd.join(" ").strip
|
22
22
|
end
|
23
23
|
|
24
24
|
def run
|
25
|
+
output = ''
|
25
26
|
begin
|
26
|
-
|
27
|
+
with_modified_path do
|
28
|
+
output = self.class.send(:'`', command)
|
29
|
+
end
|
27
30
|
rescue Errno::ENOENT
|
28
31
|
raise Cocaine::CommandNotFoundError
|
29
32
|
end
|
@@ -38,8 +41,15 @@ module Cocaine
|
|
38
41
|
|
39
42
|
private
|
40
43
|
|
41
|
-
def
|
42
|
-
|
44
|
+
def with_modified_path
|
45
|
+
begin
|
46
|
+
saved_path = ENV['PATH']
|
47
|
+
extra_path = [self.class.path].flatten
|
48
|
+
ENV['PATH'] = [ENV['PATH'], *extra_path].join(File::PATH_SEPARATOR)
|
49
|
+
yield
|
50
|
+
ensure
|
51
|
+
ENV['PATH'] = saved_path
|
52
|
+
end
|
43
53
|
end
|
44
54
|
|
45
55
|
def interpolate(pattern, vars)
|
data/lib/cocaine/version.rb
CHANGED
@@ -11,10 +11,20 @@ describe Cocaine::CommandLine do
|
|
11
11
|
cmd.command.should == "convert a.jpg b.png"
|
12
12
|
end
|
13
13
|
|
14
|
-
it "
|
15
|
-
Cocaine::CommandLine.path = "/
|
16
|
-
cmd = Cocaine::CommandLine.new("
|
17
|
-
cmd.command.should == "
|
14
|
+
it "specifies the path where the command should be run" do
|
15
|
+
Cocaine::CommandLine.path = "/path/to/command/dir"
|
16
|
+
cmd = Cocaine::CommandLine.new("ruby", "-e 'puts ENV[%{PATH}]'")
|
17
|
+
cmd.command.should == "ruby -e 'puts ENV[%{PATH}]'"
|
18
|
+
output = cmd.run
|
19
|
+
output.should match(%r{/path/to/command/dir})
|
20
|
+
end
|
21
|
+
|
22
|
+
it "specifies more than one path where the command should be run" do
|
23
|
+
Cocaine::CommandLine.path = ["/path/to/command/dir", "/some/other/path"]
|
24
|
+
cmd = Cocaine::CommandLine.new("ruby", "-e 'puts ENV[%{PATH}]'")
|
25
|
+
output = cmd.run
|
26
|
+
output.should match(%r{/path/to/command/dir})
|
27
|
+
output.should match(%r{/some/other/path})
|
18
28
|
end
|
19
29
|
|
20
30
|
it "can interpolate quoted variables into the parameters" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.2
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jon Yurek
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05
|
18
|
+
date: 2011-06-05 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|