Skip to content

Commit

Permalink
Small doc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Oskin committed Mar 27, 2021
1 parent ad05f7b commit 590e739
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
75 changes: 49 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,51 +56,74 @@ println(cfg)

### Options

#### Path
#### Paths

Default: `.env`

You can specify a custom path for your `.env` file.
By default `dotenv` use local `.env` file, but you can specify a custom path for your `.env` file.

```julia
using ConfigEnv

dotenv(path = "custom.env")
dotenv("custom.env") # Loads `custom.env` file
```

## Manual Parsing
You can supply more than one configuration file

```julia
dotenv("custom1.env", "custom2.env")
```

`ConfigEnv.parse` accepts a `String` or an `IOBuffer`, and it will return a `Dict` with the parsed keys and values.
Alternatively, you can combine different configuration files together using `merge` function or multiplication sign `*`

```julia
import ConfigEnv
cfg1 = dotenv("custom1.env")
cfg2 = dotenv("custom2.env")

cfg = merge(cfg1, cfg2)

buff = IOBuffer("BASIC=basic")
cfg = ConfigEnv.parse(buff) # will return a Dict
println(cfg) # Dict("BASIC"=>"basic")
# or equivalently

cfg = cfg1 * cfg2
```
if duplicate keys encountered, then values from the rightmost dictionary is used.

### Rules
Take note that `dotenv` function replace previous `ENV` environment variables by default. If you want to keep original version of `ENV` you should use `overwrite` argument

You can write your `.env` file using the following rules:
```julia
using ConfigEnv

- `BASIC=basic` becomes `Dict("BASIC"=>"basic")`
- empty lines are skipped
- `#` are comments
- empty content is treated as an empty string (`EMPTY=` -> `Dict("EMPTY"=>"")`)
- external single and double quotes are removed (`SINGLE_QUOTE='quoted'` -> `Dict("SINGLE_QUOTE"=>"quoted")`)
- inside double quotes, new lines are expanded (`MULTILINE="new\nline"` ->
```
Dict("MULTILINE"=>"new
line")
ENV["FOO"] = "BAR"
cfg = dotenv(overwrite = false)

cfg["FOO"] # "BAZ"
ENV["FOO"] # "BAR"
```
- inner quotes are maintained (like JSON) (`JSON={"foo": "bar"}` -> `Dict("JSON"=>"{\"foo\": \"bar\"}")"`)
- extra spaces are removed from both ends of the value (`FOO=" some value "` -> `Dict("FOO"=>"some value")`)

- previous `ENV` environment variables are replaced. If you want to keep original version of `ENV` use:
Since many dotenv packages uses another default setting when environment is not overwritten, function `dotenvx` was introduced. This function is just an alias to `dotenv(overwrite = false)`, but it can be more convenient to use.

```julia
using ConfigEnv

cfg = dotenv(override = false)
ENV["FOO"] = "BAR"
cfg = dotenvx() # Same as `dotenv(overwrite = false)`

cfg["FOO"] # "BAZ"
ENV["FOO"] # "BAR"
```

### Rules

You can write your `.env` file using the following rules:

- `FOO = BAR` becomes `ENV["FOO"] = "BAR"`
- empty lines are skipped
- `#` are comments
- empty content is treated as an empty string, i.e. `EMPTY=` becomes `ENV["EMPTY"] = ""`
- external single and double quotes are removed, i.e. `SINGLE_QUOTE='quoted'` becomes `ENV["SINGLE_QUOTE"] = "quoted"`
- inside double quotes, new lines are expanded, i.e.
```
MULTILINE = "new
line"
```
becomes `ENV["MULTILINE"] = "new\nline"`
- inner quotes are automatically escaped, i.e. `JSON={"foo": "bar"}` becomes `ENV["JSON"] = "{\"foo\": \"bar\"}"`
- extra spaces are removed from both ends of the value, i.e. `FOO=" some value "` becomes `ENV["FOO"] = "some value"`
4 changes: 2 additions & 2 deletions src/ConfigEnv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ and finally return a `EnvProxyDict` with the content. If no `path` argument is g
then value from the rightmost dictionary is used.
By default if key already exists in `ENV` it is overwritten with the values in .env file.
This behaviour can be changed by setting `overwrite` flag to `false` or using complementary `dotenvx` function.
This behaviour can be changed by setting `overwrite` flag to `false` or using dual `dotenvx` function.
Examples
========
Expand Down Expand Up @@ -121,7 +121,7 @@ and finally return a `EnvProxyDict` with the content. If no `path` argument is g
then value from the rightmost dictionary is used.
By default if key already exists in `ENV` it is overwritten with the values in .env file.
This behaviour can be changed by setting `overwrite` flag to `true` or using complementary `dotenv` function.
This behaviour can be changed by setting `overwrite` flag to `true` or using dual `dotenv` function.
Examples
========
Expand Down

0 comments on commit 590e739

Please sign in to comment.