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
- #must_succeed? ⇒ Boolean private
- #reset_uid? ⇒ Boolean private
- #run! ⇒ SystemCommand::Result private
- #sudo? ⇒ Boolean private
- #sudo_as_root? ⇒ Boolean private
Methods included from Attrable
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.
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 145 146 |
# File 'system_command.rb', line 98 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.
39 40 41 |
# File 'system_command.rb', line 39 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.
43 44 45 |
# File 'system_command.rb', line 43 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.
149 150 151 |
# File 'system_command.rb', line 149 def command [*command_prefix, executable.to_s, *] end |
#must_succeed? ⇒ 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.
278 |
# File 'sorbet/rbi/parlour.rbi', line 278 def must_succeed?; end |
#reset_uid? ⇒ 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.
281 |
# File 'sorbet/rbi/parlour.rbi', line 281 def reset_uid?; 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.
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 76 77 |
# File 'system_command.rb', line 48 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 |
#sudo? ⇒ 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.
272 |
# File 'sorbet/rbi/parlour.rbi', line 272 def sudo?; end |
#sudo_as_root? ⇒ 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.
275 |
# File 'sorbet/rbi/parlour.rbi', line 275 def sudo_as_root?; end |