Permalink
Browse files

Pass interpolations to run

Instead of declaring all interpolation values upfront, pass them to the
run method instead. This way the CommandLine object can be re-used for
different values, and it reduces confusion about whether something is an
option or interpolation value.
  • Loading branch information...
1 parent 910fcce commit ec9efa2249d958f368c6baac5a8ca5042a475c3d @mike-burns mike-burns committed with jyurek Aug 29, 2012
Showing with 26 additions and 57 deletions.
  1. +7 −24 lib/cocaine/command_line.rb
  2. +19 −33 spec/cocaine/command_line_spec.rb
@@ -52,19 +52,19 @@ def initialize(binary, params = "", options = {})
@environment = @options.delete(:environment) || {}
end
- def command
+ def command(interpolation_dictionary = {})
cmd = []
cmd << @binary
- cmd << interpolate(@params, @options)
+ cmd << interpolate(@params, interpolation_dictionary)
cmd << bit_bucket if @swallow_stderr
cmd.join(" ").strip
end
- def run
+ def run(interpolation_dictionary = {})
output = ''
begin
@logger.info("\e[32mCommand\e[0m :: #{command}") if @logger
- output = execute(command)
+ output = execute(command(interpolation_dictionary))
rescue Errno::ENOENT
raise Cocaine::CommandNotFoundError
ensure
@@ -93,26 +93,9 @@ def environment
self.class.environment.merge(@environment)
end
- def interpolate(pattern, vars)
- # interpolates :variables and :{variables}
- pattern.gsub(%r#:(?:\w+|\{\w+\})#) do |match|
- key = match[1..-1]
- key = key[1..-2] if key[0,1] == '{'
- if invalid_variables.include?(key)
- raise InterpolationError,
- "Interpolation of #{key} isn't allowed."
- end
- interpolation(vars, key) || match
- end
- end
-
- def invalid_variables
- %w(expected_outcodes swallow_stderr logger environment)
- end
-
- def interpolation(vars, key)
- if vars.key?(key.to_sym)
- shell_quote(vars[key.to_sym])
+ def interpolate(pattern, interpolation_dictionary)
+ interpolation_dictionary.inject(pattern) do |command_string, (key, value)|
+ command_string.gsub(%r|:\{?#{key}\}?|) { shell_quote(value) }
end
end
@@ -52,48 +52,49 @@
it "can interpolate quoted variables into the command line's parameters" do
cmd = Cocaine::CommandLine.new("convert",
":one :{two}",
- :one => "a.jpg",
- :two => "b.png",
:swallow_stderr => false)
- cmd.command.should == "convert 'a.jpg' 'b.png'"
+
+ command_string = cmd.command(:one => "a.jpg", :two => "b.png")
+ command_string.should == "convert 'a.jpg' 'b.png'"
+ end
+
+ it "interpolates when running a command" do
+ command = Cocaine::CommandLine.new("echo", ":hello_world")
+ command.run(:hello_world => "Hello, world").should match(/Hello, world/)
end
it "quotes command line options differently if we're on windows" do
on_windows!
cmd = Cocaine::CommandLine.new("convert",
":one :{two}",
- :one => "a.jpg",
- :two => "b.png",
:swallow_stderr => false)
- cmd.command.should == 'convert "a.jpg" "b.png"'
+ command_string = cmd.command(:one => "a.jpg", :two => "b.png")
+ command_string.should == 'convert "a.jpg" "b.png"'
end
it "can quote and interpolate dangerous variables" do
cmd = Cocaine::CommandLine.new("convert",
":one :two",
- :one => "`rm -rf`.jpg",
- :two => "ha'ha.png",
:swallow_stderr => false)
- cmd.command.should == "convert '`rm -rf`.jpg' 'ha'\\''ha.png'"
+ command_string = cmd.command(:one => "`rm -rf`.jpg", :two => "ha'ha.png")
+ command_string.should == "convert '`rm -rf`.jpg' 'ha'\\''ha.png'"
end
it "can quote and interpolate dangerous variables even on windows" do
on_windows!
cmd = Cocaine::CommandLine.new("convert",
":one :two",
- :one => "`rm -rf`.jpg",
- :two => "ha'ha.png",
:swallow_stderr => false)
- cmd.command.should == %{convert "`rm -rf`.jpg" "ha'ha.png"}
+ command_string = cmd.command(:one => "`rm -rf`.jpg", :two => "ha'ha.png")
+ command_string.should == %{convert "`rm -rf`.jpg" "ha'ha.png"}
end
it "quotes blank values into the command line's parameters" do
cmd = Cocaine::CommandLine.new("curl",
"-X POST -d :data :url",
- :data => "",
- :url => "http://localhost:9000",
:swallow_stderr => false)
- cmd.command.should == "curl -X POST -d '' 'http://localhost:9000'"
+ command_string = cmd.command(:data => "", :url => "http://localhost:9000")
+ command_string.should == "curl -X POST -d '' 'http://localhost:9000'"
end
it "allows colons in parameters" do
@@ -118,21 +119,6 @@
cmd.command.should == "convert a.jpg b.png 2>NUL"
end
- it "raises if trying to interpolate :swallow_stderr" do
- cmd = Cocaine::CommandLine.new("convert", ":swallow_stderr", :swallow_stderr => false)
- lambda { cmd.command }.should raise_error(Cocaine::CommandLineError)
- end
-
- it "raises if trying to interpolate :expected_outcodes" do
- cmd = Cocaine::CommandLine.new("convert", ":expected_outcodes", :expected_outcodes => [0])
- lambda { cmd.command }.should raise_error(Cocaine::CommandLineError)
- end
-
- it "raises if trying to interpolate :logger" do
- cmd = Cocaine::CommandLine.new("convert", ":logger", :logger => stub)
- lambda { cmd.command }.should raise_error(Cocaine::CommandLineError)
- end
-
it "runs the command it's given and return the output" do
cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
cmd.stubs(:execute).with("convert a.jpg b.png").returns(:correct_value)
@@ -174,17 +160,17 @@
end
it "detects that the system is unix" do
- Cocaine::CommandLine.new("convert").unix?.should be_true
+ Cocaine::CommandLine.new("convert").should be_unix
end
it "detects that the system is windows" do
on_windows!
- Cocaine::CommandLine.new("convert").unix?.should be_false
+ Cocaine::CommandLine.new("convert").should_not be_unix
end
it "detects that the system is windows (mingw)" do
on_mingw!
- Cocaine::CommandLine.new("convert").unix?.should be_false
+ Cocaine::CommandLine.new("convert").should_not be_unix
end
it "logs the command to a supplied logger" do

0 comments on commit ec9efa2

Please sign in to comment.