aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Reiter2017-10-14 00:03:14 +0200
committerGitHub2017-10-14 00:03:14 +0200
commite1808bf0e35d522cda4ee4803f13746b604713f5 (patch)
treec67819242437e83237419f1e5ad3402f4906ab98
parentc3ff7486095055d8b02ba17d5ec3a4db5603e838 (diff)
parentc931a1be38485b57c9d50b852da0fcaa43cb180d (diff)
downloadbrew-e1808bf0e35d522cda4ee4803f13746b604713f5.tar.bz2
Merge pull request #3309 from reitermarkus/system-command
Refactor `SystemCommand`.
-rw-r--r--Library/Homebrew/cask/lib/hbc/system_command.rb58
1 files changed, 33 insertions, 25 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/system_command.rb b/Library/Homebrew/cask/lib/hbc/system_command.rb
index 6b9a28ab2..9ce3de907 100644
--- a/Library/Homebrew/cask/lib/hbc/system_command.rb
+++ b/Library/Homebrew/cask/lib/hbc/system_command.rb
@@ -8,14 +8,14 @@ require "hbc/utils/hash_validator"
module Hbc
class SystemCommand
- attr_reader :command
+ extend Predicable
def self.run(executable, **options)
new(executable, **options).run!
end
def self.run!(command, **options)
- run(command, **options.merge(must_succeed: true))
+ run(command, **options, must_succeed: true)
end
def run!
@@ -26,38 +26,49 @@ module Hbc
case type
when :stdout
processed_output[:stdout] << line
- ohai line.chomp if options[:print_stdout]
+ ohai line.chomp if print_stdout?
when :stderr
processed_output[:stderr] << line
- ohai line.chomp if options[:print_stderr]
+ ohai line.chomp if print_stderr?
end
end
- assert_success if options[:must_succeed]
+ assert_success if must_succeed?
result
end
- def initialize(executable, **options)
+ def initialize(executable, args: [], sudo: false, input: [], print_stdout: false, print_stderr: true, must_succeed: false, **options)
+ executable, *args = Shellwords.shellescape(executable) if args.empty?
+
@executable = executable
+ @args = args
+ @sudo = sudo
+ @input = input
+ @print_stdout = print_stdout
+ @print_stderr = print_stderr
+ @must_succeed = must_succeed
+ options.extend(HashValidator).assert_valid_keys(:chdir)
@options = options
- process_options!
+ end
+
+ def command
+ @command ||= [
+ *sudo_prefix,
+ executable,
+ *args,
+ ].freeze
end
private
- attr_reader :executable, :options, :processed_output, :processed_status
-
- def process_options!
- options.extend(HashValidator)
- .assert_valid_keys :input, :print_stdout, :print_stderr, :args, :must_succeed, :sudo, :chdir
- sudo_prefix = %w[/usr/bin/sudo -E --]
- sudo_prefix = sudo_prefix.insert(1, "-A") unless ENV["SUDO_ASKPASS"].nil?
- @command = [executable]
- options[:print_stderr] = true unless options.key?(:print_stderr)
- @command.unshift(*sudo_prefix) if options[:sudo]
- @command.concat(options[:args]) if options.key?(:args) && !options[:args].empty?
- @command[0] = Shellwords.shellescape(@command[0]) if @command.size == 1
- nil
+ attr_reader :executable, :args, :input, :options, :processed_output, :processed_status
+
+ attr_predicate :sudo?, :print_stdout?, :print_stderr?, :must_succeed?
+
+ def sudo_prefix
+ return [] unless sudo?
+ askpass_flags = ENV.key?("SUDO_ASKPASS") ? ["-A"] : []
+ ["/usr/bin/sudo", *askpass_flags, "-E", "--"]
end
def assert_success
@@ -76,11 +87,8 @@ module Hbc
end
def each_output_line(&b)
- opts = {}
- opts[:chdir] = options[:chdir] if options[:chdir]
-
raw_stdin, raw_stdout, raw_stderr, raw_wait_thr =
- Open3.popen3(*expanded_command, **opts)
+ Open3.popen3(*expanded_command, **options)
write_input_to(raw_stdin)
raw_stdin.close_write
@@ -90,7 +98,7 @@ module Hbc
end
def write_input_to(raw_stdin)
- [*options[:input]].each { |line| raw_stdin.print line }
+ [*input].each(&raw_stdin.method(:print))
end
def each_line_from(sources)