From 65fdf7c9fdca684547c34e53b46ca903ce905868 Mon Sep 17 00:00:00 2001 From: "Breno G. de Oliveira" Date: Sat, 16 Dec 2023 02:47:45 -0300 Subject: [PATCH] 2023 article on ENV::Util --- 2023/incoming/a-dotenv-carol.pod | 96 ++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 2023/incoming/a-dotenv-carol.pod diff --git a/2023/incoming/a-dotenv-carol.pod b/2023/incoming/a-dotenv-carol.pod new file mode 100644 index 000000000..cf8e27ac9 --- /dev/null +++ b/2023/incoming/a-dotenv-carol.pod @@ -0,0 +1,96 @@ +Author: Breno G. de Oliveira +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 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 -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; + + 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, 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 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.