2025-02-01 19:24:56 +01:00
|
|
|
# loadenv
|
|
|
|
|
|
|
|
Loads environment files before running a command without polluting your environment
|
|
|
|
|
|
|
|
## Source code
|
|
|
|
|
2025-02-01 19:31:57 +01:00
|
|
|
You can find the source code here: [git.milar.in](https://git.tordarus.net/tordarus/loadenv)
|
2025-02-01 19:24:56 +01:00
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
2025-02-01 19:31:57 +01:00
|
|
|
If you have Go installed, you can simply go install the program: `go install git.tordarus.net/tordarus/loadenv@latest`
|
2025-02-01 19:24:56 +01:00
|
|
|
|
|
|
|
## License
|
|
|
|
|
2025-02-01 19:31:57 +01:00
|
|
|
Distributed under the MIT License. See [LICENSE.md](https://git.tordarus.net/tordarus/loadenv/src/branch/main/LICENSE.md)
|
2025-02-01 19:24:56 +01:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
`loadenv` loads the environment file provided via `-f` into the environment and runs the command provided after the arguments.
|
|
|
|
If no file is provided, `.env` is used by default.
|
|
|
|
If no command is provided, `loadenv` prints all environment variables to stdout.
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
The variables will only be available to the given command. They will be deleted after the command exits.
|
|
|
|
`loadenv` can be useful when working with programs which heavily use environment variables.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ cat .env
|
|
|
|
# production server configuration
|
|
|
|
DATABASE_HOST=prod-server.com
|
2025-02-01 19:31:57 +01:00
|
|
|
DATABASE_USER=my-username
|
2025-02-01 19:24:56 +01:00
|
|
|
DATABASE_PASS=my-super-secure-password
|
|
|
|
|
|
|
|
$ # show the 3 last added env variables
|
|
|
|
$ loadenv | tail -n 3
|
|
|
|
DATABASE_HOST=prod-server.com
|
2025-02-01 19:31:57 +01:00
|
|
|
DATABASE_USER=my-username
|
2025-02-01 19:24:56 +01:00
|
|
|
DATABASE_PASS=my-super-secure-password
|
|
|
|
|
|
|
|
$ cat dev.env
|
|
|
|
# development server configuration
|
|
|
|
DATABASE_HOST=dev-server.com
|
2025-02-01 19:31:57 +01:00
|
|
|
DATABASE_USER=my-username
|
2025-02-01 19:24:56 +01:00
|
|
|
DATABASE_PASS=my-super-secure-password
|
|
|
|
|
|
|
|
$ # load dev.env into environment and run the command env. show last 3 lines
|
|
|
|
$ loadenv -f dev.env env | tail -n 3
|
2025-02-01 19:31:57 +01:00
|
|
|
DATABASE_HOST=dev-my-username
|
2025-02-01 19:24:56 +01:00
|
|
|
DATABASE_PASS=my-super-secure-password
|
|
|
|
```
|
|
|
|
|
2025-02-01 19:31:57 +01:00
|
|
|
### Variable expansions
|
|
|
|
|
|
|
|
By default, environment variables in the env file will NOT be expanded:
|
2025-02-01 19:24:56 +01:00
|
|
|
|
|
|
|
```sh
|
|
|
|
$ env | grep USER # current value of $USER
|
2025-02-01 19:31:57 +01:00
|
|
|
USER=my-username
|
2025-02-01 19:24:56 +01:00
|
|
|
|
|
|
|
$ cat .env
|
2025-02-01 19:31:57 +01:00
|
|
|
# expand $USER variable
|
2025-02-01 19:24:56 +01:00
|
|
|
USERNAME=$USER
|
|
|
|
|
|
|
|
$ loadenv | tail -n 1
|
2025-02-01 19:31:57 +01:00
|
|
|
USERNAME=$USER # contains literal "$USER" instead of "my-username"
|
2025-02-01 19:24:56 +01:00
|
|
|
```
|
|
|
|
|
2025-02-01 19:33:07 +01:00
|
|
|
Use `-r` to expand variables in such cases:
|
2025-02-01 19:31:57 +01:00
|
|
|
|
|
|
|
```sh
|
|
|
|
$ env | grep USER # current value of $USER
|
|
|
|
USER=my-username
|
|
|
|
|
|
|
|
$ cat .env
|
|
|
|
# expand $USER variable
|
|
|
|
USERNAME=$USER
|
|
|
|
|
|
|
|
$ loadenv | tail -n 1
|
|
|
|
USERNAME=my-username # contains original value of $USER variable
|
|
|
|
```
|