Class: SystemCommand

Inherits:
Object show all
Includes:
Context
Defined in:
system_command.rb

Overview

This class is part of an internal API. This class may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Class for running sub-processes and capturing their output and exit status.

Defined Under Namespace

Modules: Helpers, Mixin Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Context

current, current=, #deferred_environment_expansion?, #quiet?, #with_context

Constructor Details

#initialize(executable, args: [], sudo: false, sudo_as_root: false, env: {}, input: [], must_succeed: false, print_stdout: false, print_stderr: true, debug: nil, verbose: nil, secrets: [], chdir: nil, reset_uid: false, run_as_real_uid: false, timeout: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • executable (String, Pathname)
  • args (Array<String, Integer, Float, Pathname>) (defaults to: [])
  • sudo (Boolean) (defaults to: false)
  • sudo_as_root (Boolean) (defaults to: false)
  • env (Hash{String => String, Boolean, PATH, nil}) (defaults to: {})
  • input (String, Array<String>) (defaults to: [])
  • must_succeed (Boolean) (defaults to: false)
  • print_stdout (Boolean, Symbol) (defaults to: false)
  • print_stderr (Boolean, Symbol) (defaults to: true)
  • debug (Boolean, nil) (defaults to: nil)
  • verbose (Boolean, nil) (defaults to: nil)
  • secrets (String, Array<String>) (defaults to: [])
  • chdir (String, Pathname, nil) (defaults to: nil)
  • reset_uid (Boolean) (defaults to: false)
  • run_as_real_uid (Boolean) (defaults to: false)
  • timeout (Integer, Float, nil) (defaults to: nil)

Raises:

  • (ArgumentError)


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'system_command.rb', line 281

def initialize(executable, args: [], sudo: false, sudo_as_root: false, env: {}, input: [], must_succeed: false,
               print_stdout: false, print_stderr: true, debug: nil, verbose: nil, secrets: [], chdir: nil,
               reset_uid: false, run_as_real_uid: false, timeout: nil)
  require "extend/ENV"
  @executable = executable
  @args = args

  raise ArgumentError, "`sudo_as_root` cannot be set if sudo is false" if !sudo && sudo_as_root
  raise ArgumentError, "`reset_uid` and `run_as_real_uid` cannot both be true" if reset_uid && run_as_real_uid

  if print_stdout.is_a?(Symbol) && print_stdout != :debug
    raise ArgumentError, "`print_stdout` is not a valid symbol"
  end
  if print_stderr.is_a?(Symbol) && print_stderr != :debug
    raise ArgumentError, "`print_stderr` is not a valid symbol"
  end

  @sudo = sudo
  @sudo_as_root = sudo_as_root
  env.each_key do |name|
    next if /^[\w&&\D]\w*$/.match?(name)

    raise ArgumentError, "Invalid variable name: #{name}"
  end
  @env = env
  @input = T.let(Array(input), T::Array[String])
  @must_succeed = must_succeed
  @print_stdout = print_stdout
  @print_stderr = print_stderr
  @debug = debug
  @verbose = verbose
  @secrets = T.let((Array(secrets) + ENV.sensitive_environment.values).uniq, T::Array[String])
  @chdir = chdir
  @reset_uid = reset_uid
  @run_as_real_uid = run_as_real_uid
  @timeout = timeout
end

Class Method Details

.quiet_system(executable, *args, env: {}) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



187
188
189
190
191
192
193
# File 'system_command.rb', line 187

def self.quiet_system(executable, *args, env: {})
  return false if executable.nil? || args.any?(&:nil?)

  # Redirect output streams to `/dev/null` instead of closing as some programs
  # will fail to execute if they can't write to an open stream.
  attached_success?(executable, args.compact, env:, out: File::NULL, err: File::NULL)
end

.run(executable, args: [], sudo: false, sudo_as_root: false, env: {}, input: [], must_succeed: false, print_stdout: false, print_stderr: true, debug: nil, verbose: nil, secrets: [], chdir: nil, reset_uid: false, run_as_real_uid: false, timeout: nil) ⇒ SystemCommand::Result

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • executable (String, Pathname)
  • args (Array<String, Integer, Float, Pathname>) (defaults to: [])
  • sudo (Boolean) (defaults to: false)
  • sudo_as_root (Boolean) (defaults to: false)
  • env (Hash{String => String, Boolean, PATH, nil}) (defaults to: {})
  • input (String, Array<String>) (defaults to: [])
  • must_succeed (Boolean) (defaults to: false)
  • print_stdout (Boolean, Symbol) (defaults to: false)
  • print_stderr (Boolean, Symbol) (defaults to: true)
  • debug (Boolean, nil) (defaults to: nil)
  • verbose (Boolean, nil) (defaults to: nil)
  • secrets (String, Array<String>) (defaults to: [])
  • chdir (String, Pathname, nil) (defaults to: nil)
  • reset_uid (Boolean) (defaults to: false)
  • run_as_real_uid (Boolean) (defaults to: false)
  • timeout (Integer, Float, nil) (defaults to: nil)

Returns:



116
117
118
119
120
121
# File 'system_command.rb', line 116

def self.run(executable, args: [], sudo: false, sudo_as_root: false, env: {}, input: [], must_succeed: false,
             print_stdout: false, print_stderr: true, debug: nil, verbose: nil, secrets: [], chdir: nil,
             reset_uid: false, run_as_real_uid: false, timeout: nil)
  new(executable, args:, sudo:, sudo_as_root:, env:, input:, must_succeed:, print_stdout:, print_stderr:, debug:,
      verbose:, secrets:, chdir:, reset_uid:, run_as_real_uid:, timeout:).run!
end

.run!(executable, args: [], sudo: false, sudo_as_root: false, env: {}, input: [], must_succeed: true, print_stdout: false, print_stderr: true, debug: nil, verbose: nil, secrets: [], chdir: nil, reset_uid: false, run_as_real_uid: false, timeout: nil) ⇒ SystemCommand::Result

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • executable (String, Pathname)
  • args (Array<String, Integer, Float, Pathname>) (defaults to: [])
  • sudo (Boolean) (defaults to: false)
  • sudo_as_root (Boolean) (defaults to: false)
  • env (Hash{String => String, Boolean, PATH, nil}) (defaults to: {})
  • input (String, Array<String>) (defaults to: [])
  • must_succeed (Boolean) (defaults to: true)
  • print_stdout (Boolean, Symbol) (defaults to: false)
  • print_stderr (Boolean, Symbol) (defaults to: true)
  • debug (Boolean, nil) (defaults to: nil)
  • verbose (Boolean, nil) (defaults to: nil)
  • secrets (String, Array<String>) (defaults to: [])
  • chdir (String, Pathname, nil) (defaults to: nil)
  • reset_uid (Boolean) (defaults to: false)
  • run_as_real_uid (Boolean) (defaults to: false)
  • timeout (Integer, Float, nil) (defaults to: nil)

Returns:



143
144
145
146
147
148
# File 'system_command.rb', line 143

def self.run!(executable, args: [], sudo: false, sudo_as_root: false, env: {}, input: [], must_succeed: true,
              print_stdout: false, print_stderr: true, debug: nil, verbose: nil, secrets: [], chdir: nil,
              reset_uid: false, run_as_real_uid: false, timeout: nil)
  run(executable, args:, sudo:, sudo_as_root:, env:, input:, must_succeed:, print_stdout:, print_stderr:,
      debug:, verbose:, secrets:, chdir:, reset_uid:, run_as_real_uid:, timeout:)
end

.safe_system(executable, *args, env: {}, out: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Run a command attached to the caller's standard streams and terminal, raising if it fails. Unlike run! nothing is captured, so interactive programs such as editors and pagers work.

Parameters:

Raises:

  • (ArgumentError)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'system_command.rb', line 161

def self.safe_system(executable, *args, env: {}, out: nil)
  raise ArgumentError, "Missing executable" if executable.nil?
  raise ArgumentError, "Invalid nil command argument" if args.any?(&:nil?)

  args = args.compact
  if Context.current.verbose?
    command = "#{executable} #{args * " "}".gsub(RUBY_PATH.to_s, "ruby")
                                           .gsub($LOAD_PATH.join(File::PATH_SEPARATOR), "$LOAD_PATH")
    ((out == :err) ? $stderr : $stdout).puts Formatter.redact_secrets(command, secrets)
  end
  return if attached_success?(executable, args, env:, out:)

  raise ErrorDuringExecution.new([executable, *args], status: $CHILD_STATUS, secrets:)
end

Instance Method Details

#commandArray<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



320
321
322
# File 'system_command.rb', line 320

def command
  [*command_prefix, executable.to_s, *expanded_args]
end

#run!SystemCommand::Result

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'system_command.rb', line 230

def run!
  $stderr.puts Formatter.redact_secrets(command.shelljoin.gsub('\=', "="), @secrets) if verbose? && debug?

  output = T.let([], T::Array[[Symbol, String]])

  status = each_output_line do |type, line|
    case type
    when :stdout
      case @print_stdout
      when true
        $stdout << Formatter.redact_secrets(line, @secrets)
      when :debug
        $stderr << Formatter.redact_secrets(line, @secrets) if debug?
      end
      output << [:stdout, line]
    when :stderr
      case @print_stderr
      when true
        $stderr << Formatter.redact_secrets(line, @secrets)
      when :debug
        $stderr << Formatter.redact_secrets(line, @secrets) if debug?
      end
      output << [:stderr, line]
    end
  end

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