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: Mixin Classes: Result
Class Method Summary collapse
- .run(executable, **options) ⇒ Object internal
- .run!(command, **options) ⇒ Object internal
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: false, secrets: [], chdir: T.unsafe(nil), reset_uid: false, timeout: nil) ⇒ void constructor private
- #run! ⇒ SystemCommand::Result private
Methods included from Context
current, current=, #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: false, secrets: [], chdir: T.unsafe(nil), reset_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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'system_command.rb', line 96 def initialize( executable, args: [], sudo: false, sudo_as_root: false, env: {}, input: [], must_succeed: false, print_stdout: false, print_stderr: true, debug: nil, verbose: false, secrets: [], chdir: T.unsafe(nil), reset_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 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 = Array(input) @must_succeed = must_succeed @print_stdout = print_stdout @print_stderr = print_stderr @debug = debug @verbose = verbose @secrets = (Array(secrets) + ENV.sensitive_environment.values).uniq @chdir = chdir @reset_uid = reset_uid @timeout = timeout end |
Class Method Details
.run(executable, **options) ⇒ Object
This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
37 38 39 |
# File 'system_command.rb', line 37 def self.run(executable, **) new(executable, **).run! end |
.run!(command, **options) ⇒ Object
This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
41 42 43 |
# File 'system_command.rb', line 41 def self.run!(command, **) run(command, **, must_succeed: true) 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.
147 148 149 |
# File 'system_command.rb', line 147 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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'system_command.rb', line 46 def run! $stderr.puts redact_secrets(command.shelljoin.gsub('\=', "="), @secrets) if verbose? || debug? @output = [] each_output_line do |type, line| case type when :stdout case @print_stdout when true $stdout << redact_secrets(line, @secrets) when :debug $stderr << redact_secrets(line, @secrets) if debug? end @output << [:stdout, line] when :stderr case @print_stderr when true $stderr << redact_secrets(line, @secrets) when :debug $stderr << 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 |