DEV Community

Kelly Stannard
Kelly Stannard

Posted on • Edited on

6

dotenv but 3 lines of ruby

edit: Thanks to Ben Curtis for inspiring me to cut this from 7 lines to 3.
edit2: More complex regexp to handle multiline vars.

Make an executable file with the following contents:

#!/usr/bin/env ruby

regexp = /^(?<a>\w+)=(?:'(?<b>.+?)'|"\g<b>"|\g<b>)$/m

ENV.update File.read(".env").scan(regexp).to_h if File.exist?(".env")
exec(ENV, *ARGV)
Enter fullscreen mode Exit fullscreen mode

Make an alias. Here is one with bundle exec also included.

alias be="/path/to/executable bundle exec"

Make an .env file.

echo HELLO=WORLD > .env

Run be bash -c 'echo $HELLO'

edit3: this has an added benefit of being language independent over the regular ruby dotenv as this is a shell program and ruby dotenv runs inside your ruby program.

edit4: do the following to have a .gitconfig like transversal up the parent directories.

#!/usr/bin/env ruby

require 'pathname'
env = Pathname.pwd.ascend.map{|x| x.join(".env")}.select(&:exist?).map(&:read).reverse.join

regexp = /^(?<a>\w+)=(?:'(?<b>.+?)'|"\g<b>"|\g<b>)$/m
ENV.update env.scan(regexp).to_h
exec(ENV, *ARGV)

Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (4)

Collapse
 
stympy profile image
Ben Curtis

This is great! Here's an alternative to the 2 lines of code in the loop:

    ENV.update l.chomp.scan(/^(?:export )?(\w+)\s*=\s*(?:['"])?([^'"]+)/i).to_h

This change also handles .env files that have lines in the format of export FOO=bar.

Collapse
 
kwstannard profile image
Kelly Stannard

How about 3 lines or fairly readable ruby?

#!/usr/bin/env ruby

regexp = /^(\w+)=['"]?(.+?)['"]?$/

ENV.update File.read(".env").scan(regexp).to_h if File.exist?(".env")
exec(ENV, *ARGV)
Collapse
 
kwstannard profile image
Kelly Stannard

Nice!

I will personally leave out the export stuff because I don't actually need to be compatible with dotenv, but it would be nice for people who do.

Collapse
 
mgrachev profile image
Grachev Mikhail

In addition to using environment variables I can recommend the tool github.com/dotenv-linter/dotenv-li... - it’s a lightning-fast linter for .env files. Written in Rust.
Maybe it would be useful for you.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More