lib/git_dump/cmd.rb in git_dump-0.1.0 vs lib/git_dump/cmd.rb in git_dump-0.1.1
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'shellwords'
require 'English'
class GitDump
# Running commands using system and popen
@@ -44,40 +46,53 @@
parse_options(options) if options
end
# Construct command string
def to_s
- cmd = args.shelljoin
+ parts = [args.shelljoin]
if env
env_str = env.map{ |k, v| "#{k}=#{v}" }.shelljoin
- cmd = "export #{env_str}; #{cmd}"
+ parts.unshift "export #{env_str};"
end
- cmd = "cd #{chdir}; #{cmd}" if chdir
- cmd << ' < /dev/null' if no_stdin
- cmd << ' > /dev/null' if no_stdout
- cmd << ' 2> /dev/null' if no_stderr
- cmd
+ parts.unshift "cd #{chdir};" if chdir
+ parts.push '< /dev/null' if no_stdin
+ parts.push '> /dev/null' if no_stdout
+ parts.push '2> /dev/null' if no_stderr
+ parts.join(' ')
end
# Run using system
def run
success = system(*arguments)
return true if success
+
fail Failure, "`#{self}` failed with #{$CHILD_STATUS.exitstatus}"
end
# Run using popen
def popen(mode = 'r', &block)
if block
result = IO.popen(arguments, mode, &block)
return result if $CHILD_STATUS.success?
+
fail Failure, "`#{self}` failed with #{$CHILD_STATUS.exitstatus}"
else
IO.popen(arguments, mode)
end
end
+ # Write input to pipe and return output
+ def pipe(input)
+ popen(input ? 'r+' : 'r') do |f|
+ if input
+ f.write input
+ f.close_write
+ end
+ f.read
+ end
+ end
+
# Capture output
def capture
popen(&:read)
end
@@ -105,9 +120,10 @@
# For ruby < 1.9 and jruby use to_s, otherwise return args with env hash
# prepanded and options hash appended
def arguments
return to_s if RUBY_VERSION < '1.9' || defined?(JRUBY_VERSION)
+
options = {}
options[:chdir] = chdir if chdir
options[:in] = '/dev/null' if no_stdin
options[:out] = '/dev/null' if no_stdout
options[:err] = '/dev/null' if no_stderr