Class: SystemCommand

Inherits:
Object
  • Object
show all
Extended by:
Predicable
Defined in:
brew/Library/Homebrew/system_command.rb

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Predicable

attr_predicate

Constructor Details

#initialize(executable, args: [], sudo: false, env: {}, input: [], must_succeed: false, print_stdout: false, print_stderr: true, verbose: false, secrets: [], **options) ⇒ SystemCommand

Returns a new instance of SystemCommand



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'brew/Library/Homebrew/system_command.rb', line 57

def initialize(executable, args: [], sudo: false, env: {}, input: [], must_succeed: false,
               print_stdout: false, print_stderr: true, verbose: false, secrets: [], **options)

  require "extend/ENV"
  @executable = executable
  @args = args
  @sudo = sudo
  @input = [*input]
  @print_stdout = print_stdout
  @print_stderr = print_stderr
  @verbose = verbose
  @secrets = (Array(secrets) + ENV.sensitive_environment.values).uniq
  @must_succeed = must_succeed
  options.assert_valid_keys!(:chdir)
  @options = options
  @env = env

  @env.keys.grep_v(/^[\w&&\D]\w*$/) do |name|
    raise ArgumentError, "Invalid variable name: '#{name}'"
  end
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid



26
27
28
# File 'brew/Library/Homebrew/system_command.rb', line 26

def pid
  @pid
end

Class Method Details

.run(executable, **options) ⇒ Object



28
29
30
# File 'brew/Library/Homebrew/system_command.rb', line 28

def self.run(executable, **options)
  new(executable, **options).run!
end

.run!(command, **options) ⇒ Object



32
33
34
# File 'brew/Library/Homebrew/system_command.rb', line 32

def self.run!(command, **options)
  run(command, **options, must_succeed: true)
end

Instance Method Details

#commandObject



79
80
81
# File 'brew/Library/Homebrew/system_command.rb', line 79

def command
  [*sudo_prefix, *env_args, executable.to_s, *expanded_args]
end

#run!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'brew/Library/Homebrew/system_command.rb', line 36

def run!
  puts redact_secrets(command.shelljoin.gsub('\=', "="), @secrets) if verbose? || ARGV.debug?

  @output = []

  each_output_line do |type, line|
    case type
    when :stdout
      $stdout << line if print_stdout?
      @output << [:stdout, line]
    when :stderr
      $stderr << line if print_stderr?
      @output << [:stderr, line]
    end
  end

  result = Result.new(command, @output, @status, secrets: @secrets)
  result.assert_success! if must_succeed?
  result
end