Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does File.read("| echo mystring") work?

Tags:

ruby

I found this in some code I was working on. It was designed to work around a requirement to read a keyfile from disk. In production the contents of the keyfile were in an environment variable.

Old code:

key = File.read('path/to/key.pem')

New code:

key = File.read('| echo $KEY_VARIABLE')

How does this work?

like image 358
djb Avatar asked Dec 16 '16 15:12

djb


1 Answers

From the IO docs:

A string starting with “|” indicates a subprocess. The remainder of the string following the “|” is invoked as a process with appropriate input/output channels connected to it.

The "channels connected" bit means that the output of the process will become the input for read. So in this example the result from the echo of the environment variable can be read.

like image 145
mikej Avatar answered Nov 01 '22 17:11

mikej