Class: SystemCommand

Inherits:
Object show all
Extended by:
Attrable
Includes:
Context
Defined in:
system_command.rb,
sorbet/rbi/dsl/system_command.rbi

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 Attrable

attr_predicate, attr_rw

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.

Parameters:

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

Raises:

  • (ArgumentError)


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, **options)
  new(executable, **options).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, **options)
  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:



149
150
151
# File 'system_command.rb', line 149

def command
  [*command_prefix, executable.to_s, *expanded_args]
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.

Returns:

  • (Boolean)


10
# File 'sorbet/rbi/dsl/system_command.rbi', line 10

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.

Returns:

  • (Boolean)


13
# File 'sorbet/rbi/dsl/system_command.rbi', line 13

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.

Returns:

  • (Boolean)


16
# File 'sorbet/rbi/dsl/system_command.rbi', line 16

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.

Returns:

  • (Boolean)


19
# File 'sorbet/rbi/dsl/system_command.rbi', line 19

def sudo_as_root?; end