Skip to content

Commit

Permalink
fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
klahap committed Jan 28, 2025
1 parent b4c9f55 commit 489bbba
Showing 1 changed file with 14 additions and 53 deletions.
67 changes: 14 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,69 +23,30 @@ plugins {
}
```

## Usage
## Example `build.gradle.kts`

Here's how you can use the `Dotenv` plugin in your Kotlin project:
Assume you have a `.env` files:

```kotlin
val envVariables: Map<String, String> = dotEnv {
addSystemEnv()
addFile(".env1")
addFile(".env2", priority = -1)
addEnv("MY_ENV_VAR", "hello world")
addEnvs(
mapOf(
"ANOTHER_ENV_VAR" to "hi",
),
priority = -2,
)
}
```

### Priority

The priority determines the order in which the environment variables are loaded. Variables from files or entries with a higher priority (numerically greater) will override those with a lower priority.

## Example

Assume you have two `.env` files:

**.env1**
**.env**
```
# this is a comment
foobar=hello world
foobarA=hello
```

**.env2**
```
foobar=howdy guys
foobarB=howdy
```

Using the plugin as follows:

```kotlin
dotEnv {
addFile(".env1")
addFile(".env2") // .env2 overwrites variables from .env1
}
/* Result:
foobar -> howdy guys
foobarA -> hello
foobarB -> howdy
*/


dotEnv {
addFile(".env1", priority = 1)
addFile(".env2") // .env2 does not overwrite anything because of lower priority (default priority = 0)
import io.github.klahap.dotenv.DotEnvBuilder

val envVars = DotEnvBuilder.dotEnv {
// addSystemEnv()
addFile("$rootDir/.env")
}
/* Result:
foobar -> hello world
foobarA -> hello
foobarB -> howdy
*/

envVars.get("foobar") // = null
envVars.getOrThrow("foobar") // = "hello world"

envVars.get("test") // = null
envVars.getOrThrow("test") // ERROR
```

## License
Expand Down

0 comments on commit 489bbba

Please sign in to comment.