From db80fdfda45921b5f8282214329fcaf5d401a804 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Sat, 23 Mar 2024 18:07:23 +0100 Subject: [PATCH] Add example in README.md Signed-off-by: Ulysses Souza --- README.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/README.md b/README.md index bdd04c4..96a5b28 100644 --- a/README.md +++ b/README.md @@ -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=) +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 \ No newline at end of file