Class: SystemCommand Private

Inherits:
Object
  • Object
show all
Extended by:
Predicable, T::Sig
Includes:
Context
Defined in:
system_command.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. 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 Predicable

attr_predicate

Methods included from Context

current, current=, #quiet?, #with_context

Constructor Details

#initialize(executable, args: [], sudo: false, env: {}, input: [], must_succeed: false, print_stdout: false, print_stderr: true, debug: nil, verbose: false, secrets: [], chdir: T.unsafe(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, URI::Generic>) (defaults to: [])
  • sudo (Boolean) (defaults to: false)
  • env (Hash{String => String}) (defaults to: {})
  • input (String, Array<String>) (defaults to: [])
  • must_succeed (Boolean) (defaults to: false)
  • print_stdout (Boolean) (defaults to: false)
  • print_stderr (Boolean) (defaults to: true)
  • debug (Boolean, nil) (defaults to: nil)
  • verbose (Boolean, nil) (defaults to: false)
  • secrets (String, Array<String>) (defaults to: [])
  • chdir (String, Pathname) (defaults to: T.unsafe(nil))
  • timeout (Integer, Float, nil) (defaults to: nil)


86
87
88
89
90
91
92
93
94
95
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
# File 'system_command.rb', line 86

def initialize(
  executable,
  args: [],
  sudo: false,
  env: {},
  input: [],
  must_succeed: false,
  print_stdout: false,
  print_stderr: true,
  debug: nil,
  verbose: false,
  secrets: [],
  chdir: T.unsafe(nil),
  timeout: nil
)
  require "extend/ENV"
  @executable = executable
  @args = args
  @sudo = sudo
  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
  @timeout = timeout
end

Class Method Details

.run(executable, **options) ⇒ 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.



39
40
41
# File 'system_command.rb', line 39

def self.run(executable, **options)
  T.unsafe(self).new(executable, **options).run!
end

.run!(command, **options) ⇒ 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.



43
44
45
# File 'system_command.rb', line 43

def self.run!(command, **options)
  T.unsafe(self).run(command, **options, must_succeed: true)
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:



123
124
125
# File 'system_command.rb', line 123

def command
  [*sudo_prefix, *env_args, 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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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
      $stdout << redact_secrets(line, @secrets) if print_stdout?
      @output << [:stdout, line]
    when :stderr
      $stderr << redact_secrets(line, @secrets) if print_stderr?
      @output << [:stderr, line]
    end
  end

  result = Result.new(command, @output, @status, secrets: @secrets)
  result.assert_success! if must_succeed?
  result
end