When creating purposes, we at all times attempt to persist with the Manifesto of Twelve Components. This method lets you keep away from numerous the issues related to the continued help of purposes all through their lifecycle. 

One of many rules of the manifesto is that every one settings must be saved in atmosphere variables, permitting you to change them for various environments (improvement, automated testing, staging/QA, manufacturing) with out altering the code.

Nonetheless, throughout improvement, coping with all of those variables can rapidly turn into cumbersome. As a mission grows, it is typical for the checklist of atmosphere variables it makes use of to develop with it, leading to unwieldy command traces resembling these:

$ RAILS_ENV=improvement REDIS_URL=redis://redis:6379 DATABASE_URL=postgres://postgres:postgres@db:5432 bundle exec rails server

Happily, there is a answer: .env recordsdata. They supply a handy method to retailer all atmosphere variables in a single place, and there are easy libraries in many languages (together with Ruby, Elixir, Python, Go, and Rust) for working with them: all they do is load the variables from the file when the appliance begins.

Nonetheless, it is not at all times plain crusing. Similar to in the actual world, your atmosphere wants taking care of. Generally, issues creep in with out you noticing which can affect how your software works. What sort of issues? Nicely, for instance:

  • Key duplication. The extra variables that you want to set in your software, the upper the possibilities you’ve gotten of by chance setting the identical variable twice — when this occurs, the brand new variables will usually overwrite the outdated values, however which one did you imply? And when you want to make a change, will you notice the duplicate?
  • Incorrect phrase delimiter. Multi-word atmosphere variable names use an underscore as a separator character (if one is used in any respect) — but it surely’s simple to by chance kind a hyphen as an alternative of an underscore and never discover it till a lot later.
  • Key with out worth. A standard tactic is so as to add a placeholder to a .env file — a variable with no worth, to be stuffed in later; this works nice, so long as you bear in mind to return and add the worth in!
  • Invalid first character. Setting variables ought to start with a upper-case letter or an underscore.
  • Lowercase keys. By conference, software atmosphere variables are at all times all-uppercase.
  • Areas. Stray areas, particularly between the variable title and equals signal, could cause issues relying on how the variables are loaded.
  • Sorting by key. It isn’t a technical requirement for libraries to make use of .env recordsdata correctly, however we have discovered that after they’re sorted by key, it is quite a bit simpler to identify a number of the errors described above.

All the issues described are conditions we have run into engaged on actual initiatives. For that purpose, dotenv-linter was created, a device to verify .env recordsdata for issues, that might be used as a part of your day-to-day workflow! A lightning-fast checking device, or “linter”, for .env recordsdata, is developed as an open supply mission by Mikhail Grachev, Evrone’s software program engineer.


Linter instruments are named after the items of fluff that you just typically discover caught to your garments and use a lint curler or sticky tape to take away: they do not essentially do any hurt, however they don’t seem to be particularly fascinating, both.

In distinction to in search of real-life lint on garments, linter instruments typically search for issues which may do hurt, or on the very least are ambiguous sufficient to be firmly advisable in opposition to — resembling our checklist above, which in fact dotenv-linter handles all of (and blazingly rapidly).

Set up

dotenv-linter is written in Rust, however you need not have something to do with Rust to make use of it — it really works with any mission with .env recordsdata. Pre-built binaries can be found for Linux and macOS, which you’ll obtain from Github. Directions for fetching and unpacking the releases are proven under:

# Linux / macOS / Home windows (MINGW and and so forth). Installs it into ./bin/ by default.
$ curl -sSfL https://uncooked.githubusercontent.com/dotenv-linter/dotenv-linter/grasp/set up.sh | sh -s
# Specify set up listing and model.
$ curl -sSfL https://uncooked.githubusercontent.com/dotenv-linter/dotenv-linter/grasp/set up.sh | sh -s -- -b usr/native/bin v2.0.0
# Alpine Linux (wget)
$ wget -q -O - https://uncooked.githubusercontent.com/dotenv-linter/dotenv-linter/grasp/set up.sh | sh -s

Should you desire, you may set up dotenv-linter on macOS utilizing Homebrew:

$ brew set up dotenv-linter/faucet/dotenv-linter

Alternatively, you should utilize Docker to run it in a container:

$ docker run --rm -v `pwd`:/app -w /app dotenvlinter/dotenv-linter


With no parameters, dotenv-linter checks all .env recordsdata within the present listing:

$ dotenv-linter
.env:2 DuplicatedKey: The FOO key's duplicated
.env:three UnorderedKey: The BAR key ought to go earlier than the FOO key
.env.take a look at:1 LeadingCharacter: Invalid main character detected

To verify a special listing, or a selected file, simply specify their paths on the command-line to verify all of them:

$ dotenv-linter dir1 dir2/.my-env-file
dir1/.env:1 LeadingCharacter: Invalid main character detected
dir1/.env:three IncorrectDelimiter: The FOO-BAR key has incorrect delimiter
dir2/.my-env-file:1 LowercaseKey: The bar key must be in uppercase

You possibly can exclude recordsdata from the scan utilizing the --exclude argument:

$ dotenv-linter --exclude .env.take a look at
.env:2 DuplicatedKey: The FOO key's duplicated
.env:three UnorderedKey: The BAR key ought to go earlier than the FOO key

 dotenv-linter + reviewdog

You possibly can simply combine dotenv-linter with reviewdog, the automated code-review device that works with GitHub and GitLab. Simply pipe the output of dotenv-linter into reviewdog utilizing the dotenv-linter format:

$ dotenv-linter | reviewdog -f=dotenv-linter -diff="git diff grasp"

On GitHub, you may simply allow dotenv-linter and reviewdog in your mission with a ready-made GitHub Motion.

 Evrone: supporting open-source

We’re very pleased to help the open-source initiatives our builders work on in any means that we will, and dotenv-linter is in fact no exception, and we’re happy to have contributed the emblem and model id to the mission of our software program developer Mikhail Grachev. The brand consists of an upturned letter “L”, reshaped to resemble lightning — representing the sheer pace of dotenv-linter — and we have used a red-orange hue that symbolises Rust, the language the linter’s written in.

We have just lately launched the model v2.0.0 of dotenv-linter with:

  • four new checks
  • Skill to disable checks utilizing the --skip argument
  • Efficiency enchancment
  • Error correction

Additionally, GitHub launched a Tremendous-Linter, a supply code repository that is packaged right into a Docker container and known as up by GitHub Actions. We’re proud to have our dotenv-linter included within the checklist of supported linters, with which the builders can name the GitHub Motion to lint their code base.

If you already know Rust, and might consider ways in which dotenv-linter may be improved, we might be very pleased to see you be part of us and assist make it higher for everybody. And, if you end up in want of creating a brand new useful gizmo, like a linter for checking code in your mission, simply attain out to us!

Source link


Please enter your comment!
Please enter your name here