Module: Command

Defined in:
lib/prick/local/command.rb

Defined Under Namespace

Classes: Error, Pipe

Class Method Summary collapse

Class Method Details

.command(env = {}, cmd, stdin: nil, stderr: nil, fail: true) ⇒ Object

Execute the shell command ‘cmd’ and return standard-output as an array of strings. If :stdin is a string or an array of lines if will be fed to the command on standard-input, if it is a IO object that IO object is piped to the command

By default #command pass through stderr but if :stderr is true, #command will instead return a tuple of stdout/stderr lines. If :stderr is false, stderr is ignored and is the same as adding “2>/dev/null” to the command

#command raises a Command::Error exception if the command returns with an exit code != 0 unless :fail is false. In that case the the exit code can be fetched from Command::status



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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/prick/local/command.rb', line 108

def command(env = {}, cmd, stdin: nil, stderr: nil, fail: true)
  cmd = "set -o errexit\nset -o pipefail\n#{cmd}"

  pw = IO::pipe # pipe[0] for read, pipe[1] for write
  pr = IO::pipe
  pe = IO::pipe

  STDOUT.flush

  pid = fork {
    pw[1].close
    pr[0].close
    pe[0].close

    STDIN.reopen(pw[0])
    pw[0].close

    STDOUT.reopen(pr[1])
    pr[1].close

    STDERR.reopen(pe[1])
    pe[1].close

    env
    exec(env.map { |k,v| [k.to_s, v.to_s] }.to_h, cmd)
  }

  pw[0].close
  pr[1].close
  pe[1].close

  if stdin
    case stdin
      when IO; pw[1].write(stdin.read)
      when String; pw[1].write(stdin)
      when Array; pw[1].write(stdin.join("\n") + "\n")
    end
    pw[1].flush
  end
  pw[1].close # Closing standard input so the command doesn't hang on read

  @status = Process.waitpid2(pid)[1].exitstatus

  out = pr[0].readlines.map(&:chomp)
  err = pe[0].readlines.map(&:chomp)

  pr[0].close
  pe[0].close

  if @status != 0
    @exception = Command::Error.new(cmd, @status, stdin, out, err)
    raise @exception if fail
  else
    @exception = nil
  end

  case stderr
    when true; [out, err]
    when false; out
    when nil
      $stderr.puts err
      out
  end
end

.command?(cmd, expect: 0, stdin: nil, stderr: false) ⇒ Boolean

Like command but returns true if the command exited with the expected status. Note that it suppresses standard-error by default

Returns:

  • (Boolean)


176
177
178
179
# File 'lib/prick/local/command.rb', line 176

def command?(cmd, expect: 0, stdin: nil, stderr: false)
  command(cmd, stdin: stdin, stderr: stderr, fail: false)
  @status == expect
end

.exceptionObject

Exception of the last command if it failed, otherwise nil TODO What is this used for?



185
# File 'lib/prick/local/command.rb', line 185

def exception() @exception end

.statusObject

Exit status of the last command



182
# File 'lib/prick/local/command.rb', line 182

def status() @status end