Class: GerritSeed::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/gerrit_seed/shell.rb

Defined Under Namespace

Classes: CommandError

Instance Method Summary collapse

Constructor Details

#initialize(chdir: Dir.pwd) ⇒ Shell

Returns a new instance of Shell.



17
18
19
# File 'lib/gerrit_seed/shell.rb', line 17

def initialize(chdir: Dir.pwd)
  @chdir = chdir
end

Instance Method Details

#call(*cmd, stdin: nil, **kwargs) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gerrit_seed/shell.rb', line 21

def call(*cmd, stdin: nil, **kwargs)
  buffer, exit_status = Open3.capture2e(*cmd, stdin_data: stdin, chdir: @chdir)

  # buffer = []
  # exit_status = nil
  #
  # Open3.popen2e(*cmd, stdin_data: stdin, chdir: @chdir) do |_stdin, stdout_stderr, wait_thr|
  #   # _stdin.write(stdin) unless stdin.nil?

  #   lines = stdout_stderr.to_a
  #   buffer.concat(lines)

  #   # lines.each do |line|
  #   #   puts line
  #   # end

  #   exit_status = wait_thr.value
  # end

  # buffer = buffer.join

  unless exit_status.success?
    raise CommandError.new(
      message: (
        <<~MESSAGE
          Shell command failed: #{exit_status}:

              #{cmd}

          Output:

              #{buffer}

        MESSAGE
      ),
      command: cmd,
      exit_status: exit_status,
      output: buffer
    )
  end

  buffer
end