Skip to content

Commit

Permalink
Add example in README.md
Browse files Browse the repository at this point in the history
Signed-off-by: Ulysses Souza <[email protected]>
  • Loading branch information
ulyssessouza committed Mar 23, 2024
1 parent d282730 commit db80fdf
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,68 @@ It's meant to be configurable through a simple API that lets you define your own
go get -u github.com/ulyssessouza/envlang
```

### Usage

Given the following environment file `input.env`:
```env
FOO=foo
BAR="bar"
BAZ = 1
EMPTY=
UNSET
VAR1=$FOO
VAR2=${BAR}
VAR3=${EMPTY:-default_value_for_empty}
VAR4=${UNSET-default_value_for_unset}
```

Example code:
```go
package main

import (
"fmt"
"os"

"github.com/ulyssessouza/envlang"
"github.com/ulyssessouza/envlang/dao"
)

func main() {
file, err := os.Open("input.env")
if err != nil {
return
}
d := dao.NewDefaultDaoFromMap(nil)
vars := envlang.GetVariablesFromInputStream(d, file)
fmt.Printf("FOO=%q\n", *vars["FOO"])
fmt.Printf("BAR=%q\n", *vars["BAR"])
fmt.Printf("BAZ=%q\n", *vars["BAZ"])
fmt.Printf("EMPTY=%q\n", *vars["EMPTY"]) // Use %q to print empty strings
fmt.Printf("UNSET=%q\n", vars["UNSET"])
fmt.Printf("VAR1=%q\n", *vars["VAR1"])
fmt.Printf("VAR2=%q\n", *vars["VAR2"])
fmt.Printf("VAR3=%q\n", *vars["VAR3"])
fmt.Printf("VAR4=%q\n", *vars["VAR4"])
}
```

Output:
```
FOO="foo"
BAR="bar"
BAZ="1"
EMPTY=""
UNSET=%!q(*string=<nil>)
VAR1="foo"
VAR2="bar"
VAR3="default_value_for_empty"
VAR4="default_value_for_unset"
```

Please note that it used `%q` to print empty strings, as the default `fmt.Printf` will not print them.

## License

This project is licensed under the [MIT](LICENSE) License - see the LICENSE.md file for details

0 comments on commit db80fdf

Please sign in to comment.