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: Mixin Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

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: nil, secrets: [], chdir: 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.

Parameters:

  • executable (String, Pathname)
  • args (Array<String, Integer, Float, Pathname, URI::Generic>) (defaults to: [])
  • sudo (Boolean) (defaults to: false)
  • sudo_as_root (Boolean) (defaults to: false)
  • env (Hash{String => nil, String, Boolean}) (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 (nil, String, Pathname) (defaults to: nil)
  • reset_uid (Boolean) (defaults to: false)
  • timeout (Integer, Float, nil) (defaults to: nil)

Raises:

  • (ArgumentError)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'system_command.rb', line 185

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, 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
  @reset_uid = reset_uid
  @timeout = timeout
end

Class Method Details

.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, 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, URI::Generic>) (defaults to: [])
  • sudo (Boolean) (defaults to: false)
  • sudo_as_root (Boolean) (defaults to: false)
  • env (Hash{String => nil, String, Boolean}) (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 (nil, String, Pathname) (defaults to: nil)
  • reset_uid (Boolean) (defaults to: false)
  • timeout (Integer, Float, nil) (defaults to: nil)

Returns:



100
101
102
103
104
105
# File 'system_command.rb', line 100

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, timeout: nil)
  new(executable, args:, sudo:, sudo_as_root:, env:, input:, must_succeed:, print_stdout:, print_stderr:, debug:,
      verbose:, secrets:, chdir:, reset_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, 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, URI::Generic>) (defaults to: [])
  • sudo (Boolean) (defaults to: false)
  • sudo_as_root (Boolean) (defaults to: false)
  • env (Hash{String => nil, String, Boolean}) (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 (nil, String, Pathname) (defaults to: nil)
  • reset_uid (Boolean) (defaults to: false)
  • timeout (Integer, Float, nil) (defaults to: nil)

Returns:



126
127
128
129
130
131
# File 'system_command.rb', line 126

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



222
223
224
# File 'system_command.rb', line 222

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.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'system_command.rb', line 134

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

  @output = T.let([], T.nilable(T::Array[[Symbol, String]]))
  @output = T.must(@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, T.must(@status), secrets: @secrets)
  result.assert_success! if must_succeed?
  result
end