Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Quote blank command line values.

  • Loading branch information...
commit a2d01c4532290b7267c1d99bfdd7165413d2dbf3 1 parent 87c9625
Daniel Mircea authored
4  lib/cocaine/command_line.rb
@@ -78,9 +78,9 @@ def interpolation(vars, key)
78 78
     end
79 79
 
80 80
     def shell_quote(string)
81  
-      return "" if string.nil? or string.empty?
  81
+      return "" if string.nil?
82 82
       if self.class.unix?
83  
-        string.split("'").map{|m| "'#{m}'" }.join("\\'")
  83
+        string.empty? ? "''" : string.split("'").map{|m| "'#{m}'" }.join("\\'")
84 84
       else
85 85
         %{"#{string}"}
86 86
       end
9  spec/cocaine/command_line_spec.rb
@@ -65,6 +65,15 @@
65 65
     cmd.command.should == %{convert "`rm -rf`.jpg" "ha'ha.png"}
66 66
   end
67 67
 
  68
+  it "quotes blank values into the command line's parameters" do
  69
+    cmd = Cocaine::CommandLine.new("curl",
  70
+                                   "-X POST -d :data :url",
  71
+                                   :data => "",
  72
+                                   :url => "http://localhost:9000",
  73
+                                   :swallow_stderr => false)
  74
+    cmd.command.should == "curl -X POST -d '' 'http://localhost:9000'"
  75
+  end
  76
+
68 77
   it "allows colons in parameters" do
69 78
     cmd = Cocaine::CommandLine.new("convert", "'a.jpg' xc:black 'b.jpg'", :swallow_stderr => false)
70 79
     cmd.command.should == "convert 'a.jpg' xc:black 'b.jpg'"

0 notes on commit a2d01c4

Mike Burns

Instead of this escape continuation, how about using an elsif?

Daniel Mircea

I think you're reffering to something like this:

def shell_quote(string)
  if string.nil?
    ""
  elsif self.class.unix?
    if string.empty?
      "''"
    else
      string.split("'").map{|m| "'#{m}'" }.join("\\'")
    end
  else
    %{"#{string}"}
  end
end

Personally I'm hesitant on refactoring something on my first commit, that's why I tried to keep it as short as possible.

Please sign in to comment.
Something went wrong with that request. Please try again.