optional variable expansions

This commit is contained in:
Tordarus 2025-02-01 19:31:57 +01:00
parent 740b8c6df2
commit d5e25c5a81
2 changed files with 35 additions and 16 deletions

View File

@ -4,17 +4,15 @@ Loads environment files before running a command without polluting your environm
## Source code ## Source code
You can find the source code here: [git.milar.in](https://git.milar.in/milarin/loadenv) You can find the source code here: [git.milar.in](https://git.tordarus.net/tordarus/loadenv)
## Installation ## Installation
If you have Go installed, you can simply go install the program: `go install git.milar.in/milarin/loadenv@latest` If you have Go installed, you can simply go install the program: `go install git.tordarus.net/tordarus/loadenv@latest`
There are pre-compiled executables for various platforms on the [repository](https://git.milar.in/milarin/loadenv/releases).
## License ## License
Distributed under the MIT License. See [LICENSE.md](https://git.milar.in/milarin/loadenv/src/branch/main/LICENSE.md) Distributed under the MIT License. See [LICENSE.md](https://git.tordarus.net/tordarus/loadenv/src/branch/main/LICENSE.md)
## Usage ## Usage
@ -31,39 +29,53 @@ The variables will only be available to the given command. They will be deleted
$ cat .env $ cat .env
# production server configuration # production server configuration
DATABASE_HOST=prod-server.com DATABASE_HOST=prod-server.com
DATABASE_USER=milarin DATABASE_USER=my-username
DATABASE_PASS=my-super-secure-password DATABASE_PASS=my-super-secure-password
$ # show the 3 last added env variables $ # show the 3 last added env variables
$ loadenv | tail -n 3 $ loadenv | tail -n 3
DATABASE_HOST=prod-server.com DATABASE_HOST=prod-server.com
DATABASE_USER=milarin DATABASE_USER=my-username
DATABASE_PASS=my-super-secure-password DATABASE_PASS=my-super-secure-password
$ cat dev.env $ cat dev.env
# development server configuration # development server configuration
DATABASE_HOST=dev-server.com DATABASE_HOST=dev-server.com
DATABASE_USER=milarin DATABASE_USER=my-username
DATABASE_PASS=my-super-secure-password DATABASE_PASS=my-super-secure-password
$ # load dev.env into environment and run the command env. show last 3 lines $ # load dev.env into environment and run the command env. show last 3 lines
$ loadenv -f dev.env env | tail -n 3 $ loadenv -f dev.env env | tail -n 3
DATABASE_HOST=dev-server.com DATABASE_HOST=dev-my-username
DATABASE_USER=milarin
DATABASE_PASS=my-super-secure-password DATABASE_PASS=my-super-secure-password
``` ```
environment variables in that file will be expanded: ### Variable expansions
By default, environment variables in the env file will NOT be expanded:
```sh ```sh
$ env | grep USER # current value of $USER $ env | grep USER # current value of $USER
USER=milarin USER=my-username
$ cat .env $ cat .env
# expand $USER reference # expand $USER variable
USERNAME=$USER USERNAME=$USER
$ loadenv | tail -n 1 $ loadenv | tail -n 1
USERNAME=milarin USERNAME=$USER # contains literal "$USER" instead of "my-username"
``` ```
Use `-e` to expand variables in such cases:
```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
```

View File

@ -20,6 +20,7 @@ var (
var ( // flags var ( // flags
FlagEnvFilePath = flag.String("f", ".env", "environment file") FlagEnvFilePath = flag.String("f", ".env", "environment file")
FlagExpandRecursively = flag.Bool("e", false, "expand references in reference definitions")
FlagExpandEnvFiles = SliceFlag{} FlagExpandEnvFiles = SliceFlag{}
) )
@ -81,7 +82,13 @@ func parseEnvFile(r io.Reader) {
matches := EnvVarRegex.FindStringSubmatch(s.Text()) matches := EnvVarRegex.FindStringSubmatch(s.Text())
if len(matches) != 0 { if len(matches) != 0 {
os.Setenv(matches[1], matches[2]) key, value := matches[1], matches[2]
if *FlagExpandRecursively {
value = os.ExpandEnv(value)
}
os.Setenv(key, value)
} else if s.Text() != "" && !EnvCommentRegex.MatchString(s.Text()) { } else if s.Text() != "" && !EnvCommentRegex.MatchString(s.Text()) {
panic(fmt.Sprintf("invalid env syntax on line %d", l)) panic(fmt.Sprintf("invalid env syntax on line %d", l))
} }