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, 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)
  • timeout (Integer, Float, nil) (defaults to: nil)

Raises:

  • (ArgumentError)


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'system_command.rb', line 271

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,
               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

  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
  @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.



179
180
181
182
183
184
185
# File 'system_command.rb', line 179

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, 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)
  • timeout (Integer, Float, nil) (defaults to: nil)

Returns:



110
111
112
113
114
115
# File 'system_command.rb', line 110

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,
             timeout: nil)
  new(executable, args:, sudo:, sudo_as_root:, env:, input:, must_succeed:, print_stdout:, print_stderr:, debug:,
      verbose:, secrets:, chdir:, 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, 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)
  • timeout (Integer, Float, nil) (defaults to: nil)

Returns:



135
136
137
138
139
140
# File 'system_command.rb', line 135

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,
              timeout: nil)
  run(executable, args:, sudo:, sudo_as_root:, env:, input:, must_succeed:, print_stdout:, print_stderr:,
      debug:, verbose:, secrets:, chdir:, 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)


153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'system_command.rb', line 153

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:



307
308
309
# File 'system_command.rb', line 307

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.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'system_command.rb', line 222

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