DEV Community

printfapps
printfapps

Posted on

Switch your whole stack between dev and test without editing .env files

Your project runs on dev ports and a dev database. Today you need to run it against test.

How do you do it?

You ask an AI, and get the usual answer: one .env.dev, one .env.test, and set -a; source .env.test; set +a before you start anything.

It works, until you have four services in four terminal tabs. Three of them have sourced .env.test, and one still has dev from this morning. The API on test talks to the dev database, and nothing tells you.

So you ask for a start.sh test that loads the right file for every service. Now the env files hold your API keys in plain text, one git add . away from being committed. And when you add a new variable, you add it to both files, and forget one.

With cmdbook, the values live on the folder, and switching is one click.

Preset settings

Secret supported

Switching presets

Here's how

This uses the same four services as the previous part: postgres, jobs, api and web.

1. Put the variables on the folder. Every command in the folder can use them, like ${API_PORT}:

DB_NAME    todo_dev
DB_PORT    55432
JOBS_PORT  17233
API_PORT   18787
WEB_PORT   15173
Enter fullscreen mode Exit fullscreen mode

2. Add a preset for each environment. A preset overrides only the values that change. test overrides all five; dev overrides none and keeps the folder's values:

test:  DB_NAME=todo_test  DB_PORT=55433  JOBS_PORT=17234  API_PORT=18788  WEB_PORT=15174
Enter fullscreen mode Exit fullscreen mode

3. Keep secrets out of files. Add an API key as a secret variable. It's stored in your operating system's keychain, not in a file, and given to the command only when it runs.

4. Switch with one click. Pick test on the folder. Every command in it now shows test, so you can see at a glance which environment you're about to run.

5. Run. Click web. The dependency checks from the previous part use the variables too, so they wait on the test ports:

Waiting for dependencies
  jobs   port 127.0.0.1:17234
  api    http://127.0.0.1:18788/health
dependencies ready in 6.5s
Local: http://localhost:15174/
Enter fullscreen mode Exit fullscreen mode

and the job server's log says database=todo_test. No tab left behind on dev.

Switching back is one click too.

Presets, folder variables and secrets are part of cmdbook Pro.

Try cmdbook here: printf.app/cmdbook

Top comments (0)