cocaine 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
- line = Cocaine::CommandLine.run("command", "some 'crazy' options")
10
- line.command # => "command some 'crazy' options"
11
- output = line.run # => Get you some output!
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
- line = Cocaine::CommandLine.new("convert", ":in -scale :resolution :out",
16
- :in => "omg.jpg",
17
- :resolution => "32x32",
18
- :out => omg_thumb.jpg")
19
- line.command # => "convert 'omg.jpg' -scale '32x32' 'omg_thumb.jpg'"
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
- line = Cocaine::CommandLine.new("cat", ":file", :file => "haha`rm -rf /`.txt")
24
- line.command # => "cat 'haha`rm -rf /`.txt'"
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
- line = Cocaine::CommandLine.new("cat", ":file", :file => "ohyeah?'`rm -rf /`.ha!")
27
- line.command # => "cat 'ohyeah?'\\''`rm -rf /`.ha!'"
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
- line = Cocaine::CommandLine.new("noisy", "--extra-verbose", :swallow_stderr => true)
32
- line.command # => "noisy --extra-verbose 2>/dev/null"
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
- # ... and on Windows...
35
- line.command # => "noisy --extra-verbose 2>NUL"
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
- line = Cocaine::CommandLine.new("git", "commit")
40
- begin
41
- line.run
42
- rescue Cocaine::ExitStatusError => e
43
- e.message # => "Command 'git commit' returned 1. Expected 0"
44
- end
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
- line = Cocaine::CommandLine.new("lolwut")
49
- begin
50
- line.run
51
- rescue Cocaine::CommandNotFoundError => e
52
- e # => the command isn't in the $PATH for this process.
53
- end
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
- Cocaine::CommandLine.path = "/opt/bin"
58
- line = Cocaine::CommandLine.new("lolwut")
59
- line.command # => "/opt/bin/lolwut"
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
- line = Cocaine::CommandLine.new("/opt/bin/lolwut")
64
- line.command # => "/opt/bin/lolwut"
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
- line = Cocaine::CommandLine.new("/usr/bin/false", "", :expected_outcodes => [0, 1])
69
- begin
70
- line.run
71
- rescue Cocaine::ExitStatusError => e
72
- # => You never get here!
73
- end
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
 
@@ -15,15 +15,18 @@ module Cocaine
15
15
 
16
16
  def command
17
17
  cmd = []
18
- cmd << full_path(@binary)
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
- output = self.class.send(:'`', command)
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 full_path(binary)
42
- [self.class.path, binary].compact.join("/")
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)
@@ -1,3 +1,3 @@
1
1
  module Cocaine
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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 "can set a default path and produce commands with that path" do
15
- Cocaine::CommandLine.path = "/opt/bin"
16
- cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
17
- cmd.command.should == "/opt/bin/convert a.jpg b.png"
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
@@ -1,5 +1,5 @@
1
1
  require 'rspec'
2
- require 'spec/support/with_exitstatus'
2
+ require './spec/support/with_exitstatus'
3
3
  require 'mocha'
4
4
  require 'bourne'
5
5
  require 'cocaine'
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
- - 2
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 00:00:00 -04:00
18
+ date: 2011-06-05 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency