Skip to content

Commit

Permalink
Merge pull request #4 from euantorano/fix/3-directory-paths
Browse files Browse the repository at this point in the history
Overhaul parsing and alter API surface
  • Loading branch information
euantorano committed Feb 8, 2022
2 parents 08b82a6 + 84b520d commit fa3e62c
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 1,876 deletions.
26 changes: 0 additions & 26 deletions .circleci/config.yml

This file was deleted.

9 changes: 5 additions & 4 deletions .env.example
@@ -1,9 +1,10 @@
# This is an exampe `.env` file.
# This is an example `.env` file.
# Comments start with `#`. Values are assigned as follows:

SIMPLE_VAL = test
ANOTHER_SIMPLE_VAL = "test" # the value will just be `test`, without quotes
MULTILINE_VAL = """This value
SIMPLE_VAL=test
ANOTHER_SIMPLE_VAL="test" # the value will just be `test`, without quotes
MULTILINE_VAL="""This value

will span multiple lines, just like in Nim

"""
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,18 @@
name: Build And Test

on:
push:
pull_request:

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
nim-versions: ['1.6.2', '1.4.8', 'stable']
steps:
- uses: actions/checkout@v2
- uses: jiro4989/setup-nim-action@v1
with:
nim-version: ${{ matrix.nim-versions }}
- run: nimble test -y
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2016, Euan T.
Copyright (c) 2016, 2022, Euan Torano.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
44 changes: 22 additions & 22 deletions README.md
@@ -1,4 +1,4 @@
# dotenv.nim [![CircleCI](https://circleci.com/gh/euantorano/dotenv.nim/tree/master.svg?style=svg)](https://circleci.com/gh/euantorano/dotenv.nim/tree/master)
# dotenv.nim

[dotenv](https://github.com/bkeepers/dotenv) implementation for Nim. Loads environment variables from `.env`

Expand All @@ -17,11 +17,9 @@ Or add the following to your `.nimble` file:
```
# Dependencies
requires "dotenv >= 1.1.0"
requires "dotenv >= 2.0.0"
```

## [Documentation](https://htmlpreview.github.io/?https://github.com/euantorano/dotenv.nim/blob/master/docs/dotenv.html)

## Usage

### Create a `.env` file
Expand Down Expand Up @@ -53,26 +51,33 @@ You can also add comments, using the `#` symbol:
DB_NAME=test # Or they can follow an assignment
```

Variable values can reference other variables - either from the same `.env` file, or from the existing environment variables:

```
CONFIG_DIR=${HOME}/.config
CONFIG_FILE=${CONFIG_DIR}/config.json
```

* Variables are referenced either using `${VARIABLE}` or as simply `$VARIABLE`.
* Variables do not need to be defined before usage.
* Unknown variables are replaced with empty strings.

### Loading the `.env` file

You can load the `.env` file from the current working directory as follows:

```nim
import dotenv
let env = initDotEnv()
env.load()
# You can now access the variables using os.getEnv()
load()
```

Or, you can specify the path to the directory and/or file:

```nim
import dotenv
let env = initDotEnv("/some/directory/path", "custom_file_name.env")
env.load()
load("/some/directory/path", "custom_file_name.env")
# You can now access the variables using os.getEnv()
```
Expand All @@ -82,26 +87,21 @@ By default, `dotenv` does not overwrite existing environment variables, though t
```nim
import dotenv
let env = initDotEnv()
env.overload()
overload()
# You can now access the variables using os.getEnv()
```

### Loading from a string

You can also load environment variables directly from a string without instantiating a `DotEnv` instance:
You can also load environment variables directly from a string using `std/streams`:

```nim
import dotenv, os
import dotenv, std/streams
load(newStringStream("""hello = world
foo = bar
"""))
loadEnvFromString("""hello = world
foo = bar
""")
assert getEnv("foo") == "bar"
```

## Planned features

* Allow the usage of other environment variables inside variable values.
* Add validation of variable values, specifying variables have to be integer, or boolean, or a value from a predefined set.

0 comments on commit fa3e62c

Please sign in to comment.