Class: SystemCommand Private

Inherits:
Object
  • Object
show all
Extended by:
Predicable
Includes:
Context
Defined in:
Library/Homebrew/system_command.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class for running sub-processes and capturing their output and exit status.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Predicable

attr_predicate

Methods included from Context

current, current=, #debug?, #quiet?, #with_context

Constructor Details

#initialize(executable, args: [], sudo: false, env: {}, input: [], must_succeed: false, print_stdout: false, print_stderr: true, verbose: false, secrets: [], **options) ⇒ SystemCommand

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SystemCommand.


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'Library/Homebrew/system_command.rb', line 63

def initialize(executable, args: [], sudo: false, env: {}, input: [], must_succeed: false,
               print_stdout: false, print_stderr: true, verbose: false, secrets: [], **options)

  require "extend/ENV"
  @executable = executable
  @args = args
  @sudo = sudo
  @input = Array(input)
  @print_stdout = print_stdout
  @print_stderr = print_stderr
  @verbose = verbose
  @secrets = (Array(secrets) + ENV.sensitive_environment.values).uniq
  @must_succeed = must_succeed
  options.assert_valid_keys!(:chdir)
  @options = options
  @env = env

  @env.each_key do |name|
    next if /^[\w&&\D]\w*$/.match?(name)

    raise ArgumentError, "Invalid variable name: '#{name}'"
  end
end

Instance Attribute Details

#pidObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


32
33
34
# File 'Library/Homebrew/system_command.rb', line 32

def pid
  @pid
end

Class Method Details

.run(executable, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


34
35
36
# File 'Library/Homebrew/system_command.rb', line 34

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

.run!(command, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


38
39
40
# File 'Library/Homebrew/system_command.rb', line 38

def self.run!(command, **options)
  run(command, **options, must_succeed: true)
end

Instance Method Details

#commandObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


87
88
89
# File 'Library/Homebrew/system_command.rb', line 87

def command
  [*sudo_prefix, *env_args, executable.to_s, *expanded_args]
end

#run!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'Library/Homebrew/system_command.rb', line 42

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

  @output = []

  each_output_line do |type, line|
    case type
    when :stdout
      $stdout << line if print_stdout?
      @output << [:stdout, line]
    when :stderr
      $stderr << line if print_stderr?
      @output << [:stderr, line]
    end
  end

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