Skip to content

Commit

Permalink
migrate to v021
Browse files Browse the repository at this point in the history
  • Loading branch information
7underlines committed Jan 11, 2021
1 parent 89aff61 commit 239c91c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 40 deletions.
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
POSTGRES_HOST=localhost
POSTGRES_USER=admin
POSTGRES_PASSWORD=postgres_password_goes_here
POSTGRES_DB=admin

JWT_SECRET=jwt_secret_goes_here
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# These are supported funding model platforms

github: [logTom]
github: [treffner]
37 changes: 24 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ People configure their app variables via JSON, YAML, or even gitignored .v files
Further reading:
[12 factor apps](https://12factor.net/config)

## Usage:
# Features

- fully compatible with docker-compose .env
- useful helper function dotenv.get()

# Usage:
```shell
v install logTom.dotenv
v install treffner.dotenv
```

Create a file called .env in the root folder of your application.
Add it to your .gitignore file.
Add it to your .gitignore file. (best practice)
Fill it with key=value pairs.

```shell
POSTGRES_HOST = localhost
POSTGRES_USER = admin
POSTGRES_PASSWORD = postgres_password_goes_here
POSTGRES_DB = admin
POSTGRES_HOST=localhost
POSTGRES_USER=admin
POSTGRES_PASSWORD=postgres_password_goes_here
POSTGRES_DB=admin

JWT_SECRET = jwt_secret_goes_here
JWT_SECRET=jwt_secret_goes_here
```

Then in your v source:
```v
module main
import logtom.dotenv
import treffner.dotenv
import os
fn main()
Expand All @@ -49,11 +54,17 @@ These syntax rules apply to the .env file:
- Lines beginning with # are processed as comments and ignored.
- Blank lines are ignored.
- There is no special handling of quotation marks. This means that they are part of the VAL.
- Environment variables may not contain whitespace.

## Test with docker-compose
```shell
docker-compose run --rm v
println(os.getenv('POSTGRES_HOST'))
```
This should print "localhost".

## Todo
- auto add example .env file
- auto add .env to .gitignore
# Todo/ideas
- dotenv.required() method to let people know what variables are needed

## License
# License
[GPL-3.0](LICENSE)
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'
services:
v:
image: taojy123/vlang
env_file:
- .env
31 changes: 19 additions & 12 deletions dotenv.v
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
module dotenv

import os
import io
// import io

pub fn load() {
file := os.dir(os.executable()) + os.path_separator + '.env'
if !os.exists(file) {
return
}
mut f := os.open_file(file, 'r', 0) or {
contents := os.read_file(file) or {
return
}
defer {
f.close()
}
mut reader := io.new_buffered_reader({
reader: io.make_reader(f)
})
for {
line := reader.read_line() or {
break
}
lines := contents.split_into_lines()
for line in lines {
parse_line(line.trim_space())
}
// mut f := os.open_file(file, 'r', 0) or {
// return
// }
// defer {
// f.close()
// }
// mut reader := io.new_buffered_reader({
// reader: io.make_reader(f)
// })
// for {
// line := reader.read_line() or {
// break
// }
// parse_line(line.trim_space())
// }
}

fn parse_line(line string) {
Expand Down
16 changes: 16 additions & 0 deletions dotenv_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module dotenv

import os

// fn test_load() {
// load()
// host := os.getenv('POSTGRES_HOST')
// assert host == 'localhost'
// }

fn test_parse() {
text := 'POSTGRES_HOST=localhost_parse'
parse_line(text)
host := os.getenv('POSTGRES_HOST')
assert host == 'localhost_parse'
}
17 changes: 5 additions & 12 deletions examples/.env
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
POSTGRES_HOST = localhost
POSTGRES_USER = admin
POSTGRES_PASSWORD = postgres_password_goes_here
POSTGRES_DB = admin
POSTGRES_HOST=localhost
POSTGRES_USER=admin
POSTGRES_PASSWORD=postgres_password_goes_here
POSTGRES_DB=admin

JWT_SECRET = jwt_secret_goes_here
=
# =
#4=7
5 =
= 9
# example comment
# JWT_SECRET = jwt_secret_dev_goes_here
JWT_SECRET=jwt_secret_goes_here
2 changes: 1 addition & 1 deletion examples/simple.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
println(os.getenv('POSTGRES_HOST'))
// you can also use dotenv.get() if you need fallback handling
secret := dotenv.get('JWT_SECRET') or {
'default_dev_token'
'default_dev_token' // default, not found, or simply the same on all environments
}
println(secret)
}
2 changes: 1 addition & 1 deletion v.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Module {
name: 'dotenv'
description: 'Loads environment variables from `.env` to `os.getenv()` automagically.'
version: '1.1.0'
version: '0.2.1'
dependencies: []
}

0 comments on commit 239c91c

Please sign in to comment.