optional variable expansions
This commit is contained in:
parent
740b8c6df2
commit
d5e25c5a81
40
README.md
40
README.md
@ -4,17 +4,15 @@ Loads environment files before running a command without polluting your environm
|
||||
|
||||
## 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
|
||||
|
||||
If you have Go installed, you can simply go install the program: `go install git.milar.in/milarin/loadenv@latest`
|
||||
|
||||
There are pre-compiled executables for various platforms on the [repository](https://git.milar.in/milarin/loadenv/releases).
|
||||
If you have Go installed, you can simply go install the program: `go install git.tordarus.net/tordarus/loadenv@latest`
|
||||
|
||||
## 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
|
||||
|
||||
@ -31,39 +29,53 @@ The variables will only be available to the given command. They will be deleted
|
||||
$ cat .env
|
||||
# production server configuration
|
||||
DATABASE_HOST=prod-server.com
|
||||
DATABASE_USER=milarin
|
||||
DATABASE_USER=my-username
|
||||
DATABASE_PASS=my-super-secure-password
|
||||
|
||||
$ # show the 3 last added env variables
|
||||
$ loadenv | tail -n 3
|
||||
DATABASE_HOST=prod-server.com
|
||||
DATABASE_USER=milarin
|
||||
DATABASE_USER=my-username
|
||||
DATABASE_PASS=my-super-secure-password
|
||||
|
||||
$ cat dev.env
|
||||
# development server configuration
|
||||
DATABASE_HOST=dev-server.com
|
||||
DATABASE_USER=milarin
|
||||
DATABASE_USER=my-username
|
||||
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
|
||||
DATABASE_HOST=dev-server.com
|
||||
DATABASE_USER=milarin
|
||||
DATABASE_HOST=dev-my-username
|
||||
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
|
||||
$ env | grep USER # current value of $USER
|
||||
USER=milarin
|
||||
USER=my-username
|
||||
|
||||
$ cat .env
|
||||
# expand $USER reference
|
||||
# expand $USER variable
|
||||
USERNAME=$USER
|
||||
|
||||
$ 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
|
||||
```
|
9
main.go
9
main.go
@ -20,6 +20,7 @@ var (
|
||||
|
||||
var ( // flags
|
||||
FlagEnvFilePath = flag.String("f", ".env", "environment file")
|
||||
FlagExpandRecursively = flag.Bool("e", false, "expand references in reference definitions")
|
||||
|
||||
FlagExpandEnvFiles = SliceFlag{}
|
||||
)
|
||||
@ -81,7 +82,13 @@ func parseEnvFile(r io.Reader) {
|
||||
|
||||
matches := EnvVarRegex.FindStringSubmatch(s.Text())
|
||||
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()) {
|
||||
panic(fmt.Sprintf("invalid env syntax on line %d", l))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user