Skip to content

Commit

Permalink
Merge pull request #7 from Arkoniak/dual_fix
Browse files Browse the repository at this point in the history
Small doc fixes
  • Loading branch information
Arkoniak authored Mar 27, 2021
2 parents ad05f7b + 590e739 commit ca99b10
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

2 comments on commit ca99b10

@Arkoniak
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/33028

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" ca99b10fff02ead37027fb2103941eeb7cf90117
git push origin v0.2.0

Please sign in to comment.