Module: Command
- Defined in:
- lib/local/command.rb
Defined Under Namespace
Class Method Summary collapse
-
.command(cmd, stdin: nil, stderr: nil, fail: true) ⇒ Object
Execute the shell command ‘cmd’ and return standard-output as an array of strings.
-
.command?(cmd, expect: 0, stdin: nil, stderr: false) ⇒ Boolean
Like command but returns true if the command exited with the expected status.
-
.exception ⇒ Object
Stored exception when #command is called with :fail true.
-
.status ⇒ Object
Exit status of the last command.
Class Method Details
.command(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 standard-error but if :stderr is true, #command will return a tuple of standard-output, standard-error lines. If :stderr is false, standard-error is ignored and is the same as adding “2>/dev/null” to the command
#command raises a Command::Error exception if the command return with an exit code != 0 unless :fail is false. In that case the the exit code can be fetched from Command::status
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 172 |
# File 'lib/local/command.rb', line 109 def command(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 exec(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 pw[1].close end @status = Process.waitpid2(pid)[1].exitstatus out = pr[0].readlines.map(&:chomp) err = pe[0].readlines.map(&:chomp) pw[1].close if !stdin 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
184 185 186 187 |
# File 'lib/local/command.rb', line 184 def command?(cmd, expect: 0, stdin: nil, stderr: false) command(cmd, stdin: stdin, stderr: stderr, fail: false) @status == expect end |
.exception ⇒ Object
Stored exception when #command is called with :fail true
179 |
# File 'lib/local/command.rb', line 179 def exception() @exception end |
.status ⇒ Object
Exit status of the last command. FIXME: This is not usable because the methods raise an exception if the command exited with anything but 0
176 |
# File 'lib/local/command.rb', line 176 def status() @status end |