Class: SystemCommand
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
Instance Attribute Summary collapse
- #sandbox ⇒ void writeonly private
- #sandbox_inheritance ⇒ IO writeonly private
Class Method Summary collapse
- .quiet_system(executable, *args, env: {}) ⇒ Object private
- .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 private
- .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 private
-
.safe_system(executable, *args, env: {}, out: nil) ⇒ void
private
Run a command attached to the caller's standard streams and terminal, raising if it fails.
-
.sudo_available? ⇒ Boolean
private
Check access once, when a command first needs sudo.
Instance Method Summary collapse
- #command ⇒ Array<String> private
- #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 constructor private
- #run! ⇒ SystemCommand::Result private
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.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'system_command.rb', line 311 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 @sandbox = T.let(nil, T.nilable(Sandbox)) @sandbox_inheritance = T.let(nil, T.nilable(IO)) end |
Instance Attribute Details
#sandbox=(value) ⇒ void (writeonly)
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.
103 104 105 |
# File 'system_command.rb', line 103 def sandbox=(value) @sandbox = value end |
#sandbox_inheritance=(value) ⇒ IO (writeonly)
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.
106 107 108 |
# File 'system_command.rb', line 106 def sandbox_inheritance=(value) @sandbox_inheritance = value 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.
219 220 221 222 223 224 225 |
# File 'system_command.rb', line 219 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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'system_command.rb', line 126 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) # Only use sudo: nil for operations that can safely be retried. if sudo.nil? result = new(executable, args:, sudo: false, sudo_as_root: false, env:, input:, must_succeed: false, print_stdout:, print_stderr: Homebrew::EnvConfig.no_sudo? ? print_stderr : false, debug:, verbose:, secrets:, chdir:, timeout:).run! if result.success? || !sudo_available? result.assert_success! if must_succeed return result end sudo = true end 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.
175 176 177 178 179 180 |
# File 'system_command.rb', line 175 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.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'system_command.rb', line 193 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 |
.sudo_available? ⇒ Boolean
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.
Check access once, when a command first needs sudo.
147 148 149 150 151 152 153 154 155 |
# File 'system_command.rb', line 147 def self.sudo_available? return false if Homebrew::EnvConfig.no_sudo? return true if ENV["HOMEBREW_SUDO_CHECKED"] == "1" ENV["HOMEBREW_NO_SUDO"] = "1" unless run("/bin/bash", args: [HOMEBREW_LIBRARY_PATH/"utils/sudo.sh"], print_stderr: false).success? ENV["HOMEBREW_SUDO_CHECKED"] = "1" !Homebrew::EnvConfig.no_sudo? end |
Instance Method Details
#command ⇒ Array<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.
349 350 351 |
# File 'system_command.rb', line 349 def command [*command_prefix, executable.to_s, *] 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.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'system_command.rb', line 262 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 |