Skip to content

Commit

Permalink
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions tool/jt.rb
Original file line number Diff line number Diff line change
@@ -112,23 +112,34 @@ module ShellUtils
private

def raw_sh(*args)
puts "$ #{printable_cmd(args) * ' '}"
puts "$ #{printable_cmd(args)}"
result = system(*args)
unless result
$stderr.puts "FAILED (#{$?}): #{printable_cmd(args) * ' '}"
$stderr.puts "FAILED (#{$?}): #{printable_cmd(args)}"
exit $?.exitstatus
end
end

def printable_cmd(args)
if Hash === args[0]
if args[0].empty?
args[1..-1]
env = {}
if Hash === args.first
env, *args = args
end
env = env.map { |k,v| "#{k}=#{shellescape(v)}" }.join(' ')
args = args.map { |a| shellescape(a) }.join(' ')
env.empty? ? args : "#{env} #{args}"
end

def shellescape(str)
if str.include?(' ')
if str.include?("'")
require 'shellwords'
Shellwords.escape(str)
else
[args[0].map { |k, v| "#{k}=#{v}" }.join(' '), *args[1..-1]]
"'#{str}'"
end
else
args
str
end
end

0 comments on commit ce1ec97

Please sign in to comment.