Skip to content

Commit

Permalink
2023 article on ENV::Util
Browse files Browse the repository at this point in the history
  • Loading branch information
garu committed Dec 16, 2023
1 parent 7a022ab commit 65fdf7c
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions 2023/incoming/a-dotenv-carol.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Author: Breno G. de Oliveira <garu@cpan.org>
Title: A Dotenv Carol
Topic: dotenv, .env, configuration, setup, config, microservices, docker, containers, devops, deployment

=encoding utf8

In the chilling corridors of Scrooge & Marley Software Solutions, Ebenezer
Scrooge sat hunched over his laptop, hands clutching his hair, overwhelmed
by failed deployments of his containers. Suddenly, a ghostly figure
materialized before him – the Ghost of Configurations Past. It pointed at
the stack of old, unwieldy JSON and YAML files on Scrooge's applications.
I<< "Behold, Ebenezer," >> moaned the specter, I<< "the complexity and
confusion of your past configurations. Remember the days of endless nested
structures and tedious manual edits." >>

As the ghost swept Scrooge through the digital corridors of time, he saw
himself wrestling with intricate configurations, wasting precious hours on
syntax errors and misalignments. I<< "There must be a simpler way," >> the
ghost whispered.

Next appeared the Ghost of Configurations Present, adorned with snippets of
Perl code and a merry demeanor. It took Scrooge to witness scenes of joyous
developers using L<dotenv|https://12factor.net/config> files – simple,
readable, and devoid of unnecessary complexity. I<< "Behold the present,
Ebenezer!" >> the ghost declared. I<< "Look at the ease with which
developers manage all those different app environments. No more verbosity,
no more headaches. Dotenv files bring clarity and simplicity to configuration
management." >>.

I<< "But how were they even able to change all that code to use .env
files?" >>, Ebenezer scoffed. I<< "It must have cost a fortune to
migrate!" >>. The spectre quietly directed him to an open IDE, where a
single line simply read:

use L<ENV::Util> -load_dotenv;

As Scrooge squinted at the line, the ghostly voice resonated.
I<< "L<< ENV::Util >> is a lightweight module with zero dependencies. And
just by adding that line, your C<< %ENV >> will be populated with all the
variables defined in your dotenv file, making it ready to use throughout
your app." >> The old man remained skeptical. I<< "Fine, but what if I want
a configuration hash that's specific to my app, that I can for example
iterate on, knowing it will only contain relevant settings?" >> The ethereal
entity waved him to another terminal. I<< "You mean like so?" >>

my %config = ENV::Util::prefix2hash( 'MYAPP_' )

I<< "This will parse C<%ENV> and return a key/value hash containing just the
variables prefixed by 'MYAPP_'. It will even remove the prefix and lowercase
variable names so they become more Perlish. This way a dotenv file with a
line like this:" >>

MYAPP_DB_SERVER_IP=127.0.0.1

I<< "Can be accessed inside that hash as:" >>

$config{db_server_ip}

Ebenezer was speechless. Then he remembered one of his past hurdles with
configuration files. I<< "That doesn't solve my main debugging issue! When
thing go wrong, and in my line of business you know they will eventually,
you sometimes need to dump all your configuration data, but that puts
a lot of secrets in plain text on my logfiles!" >>. With an unchanged
demeanor, the apparition replied: I<< "Then instead of dumping %ENV you can
dump C<< ENV::Util::redacted_env >>, which will mask the values of any keys
that look sensitive, like the ones matching 'ID', 'EMAIL', 'TOKEN' or
'PASS':" >>

use ENV::Util -load_dotenv;
use L<Data::Printer>;

my %redacted = ENV::Util::redacted_env();
p %redacted;

I<< "You can even add your own rules as to what gets redacted and what
doesn't. With ENV::Util, there really is no excuse to avoid dotenv files." >>

I<< "It's a Christmas miracle!" >> Scrooge exclaimed, his eyes gleaming with
newfound understanding.

But the Ghost of Configurations Yet to Come loomed ominously. It revealed a
desolate future where Scrooge clung stubbornly to traditional configuration
files in his apps, leading to confusion, inefficiency, disgruntled developers
and... overtime.

I<< "No more!" >> cried Scrooge, awakening from his spectral journey.
I<< "I have seen the error of my ways. With L<ENV::Util>, I shall embrace
the simplicity of dotenv files and bid farewell to the chains of complexity
that bind my code." >>

And so, on that Christmas morning, Scrooge used L<ENV::Util> and replaced his
verbose configurations with a neat .env file. As the clock struck midnight,
signaling the dawn of a new day, Scrooge's software team gathered, their
faces beaming with joy. The office, once shrouded in the darkness of
cumbersome configurations, now radiated the festive glow of efficient and
readable dotenv files.

0 comments on commit 65fdf7c

Please sign in to comment.