encoding/envfile
Reading and writing .env files, with variable substitution.
import encoding.envfile
A .env file holds a program's environment variables, one KEY=value per line. This package skips blank lines and # comments, accepts export before a key, strips the quotes, and keeps the keys in order.
Parse substitutes references such as ${HOST} from the same file; ParseRaw leaves them alone. The operating system's own environment variables are not read; for those see the env package.
Types
Doc
struct Doc| Name | Type | Description |
|---|---|---|
m | collections.OrderedMap | The keys and values, in order. |
flags | Vector | For each key, whether its value takes substitution; single quotes turn it off. |
import encoding.envfile
import encoding.encerr
func main:
text := "# settings\nHOST=localhost\nPORT=8080\nexport NAME=\"hello world\"\nURL=http://${HOST}:${PORT}/\n"
mut doc := envfile.Parse(text)
println envfile.Len(doc), envfile.Keys(doc)
println envfile.Get(doc, "NAME"), envfile.Has(doc, "DEBUG"), envfile.GetOr(doc, "DEBUG", "false")
println envfile.KeyAt(doc, 0), envfile.ValueAt(doc, 1)
println envfile.Get(doc, "URL")
raw := envfile.ParseRaw(text)
println envfile.Get(raw, "URL"), envfile.RefNames(raw)
mut base := envfile.New()
expanded := envfile.ExpandDoc(raw, base)
println envfile.Get(expanded, "URL")
println envfile.Expand("${HOST}/api", doc, base), envfile.RefNamesIn("$A and ${B}")
envfile.Set(doc, "DEBUG", "true")
println envfile.Remove(doc, "URL")
println envfile.Format(doc)
println envfile.FormatExport(base) == "", envfile.Quote("a b")
mut other := envfile.Parse("PORT=9090\nMODE=prod\n")
envfile.MergeInto(doc, other)
println envfile.Get(doc, "PORT"), envfile.Get(doc, "MODE")
mut e := encerr.NoError()
_ := envfile.ParseStrict("=no key\n", e)
println encerr.IsError(e), envfile.Valid("A=1\n")
println envfile.ParseScalar(`"a\tb"`), envfile.ParseScalarEx("x ; comment", true)
envfile.Free(doc)
envfile.Free(raw)
envfile.Free(base)
envfile.Free(expanded)
envfile.Free(other)
endFunctions
Quote
func Quote(v: str): strThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
Format
func Format(d &: Doc): strThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
FormatExport
func FormatExport(d &: Doc): strFormat, with export before each line, for a shell.The example for this item is shown with the ones above: encoding-envfile-Parse.salam
New
func New(): DocThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
Len
func Len(d &: Doc): intThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
Has
func Has(d &: Doc, key: str): boolThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
Get
func Get(d &: Doc, key: str): strThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
GetOr
func GetOr(d &: Doc, key: str, def: str): strThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
Set
func Set(d &: Doc, key: str, val: str)The example for this item is shown with the ones above: encoding-envfile-Parse.salam
Remove
func Remove(d &: Doc, key: str): boolThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
KeyAt
func KeyAt(d &: Doc, i: int): stri.The example for this item is shown with the ones above: encoding-envfile-Parse.salam
ValueAt
func ValueAt(d &: Doc, i: int): stri.The example for this item is shown with the ones above: encoding-envfile-Parse.salam
Keys
func Keys(d &: Doc): Vector<str>The example for this item is shown with the ones above: encoding-envfile-Parse.salam
MergeInto
func MergeInto(dst &: Doc, src &: Doc)src into dst, replacing any that are already there.The example for this item is shown with the ones above: encoding-envfile-Parse.salam
Free
func Free(d &: Doc)The example for this item is shown with the ones above: encoding-envfile-Parse.salam
ParseRaw
func ParseRaw(text: str): DocThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
Parse
func Parse(text: str): DocThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
ParseStrict
func ParseStrict(text: str, err &: encerr.EncodingError): DocParse, but a malformed line is an error and err is filled in.The example for this item is shown with the ones above: encoding-envfile-Parse.salam
Valid
func Valid(text: str): boolThe example for this item is shown with the ones above: encoding-envfile-Parse.salam
ParseScalar
func ParseScalar(raw: str): str=: it strips the quotes and the escapes.The example for this item is shown with the ones above: encoding-envfile-Parse.salam
ParseScalarEx
func ParseScalarEx(raw: str, semi: bool): strParseScalar; when semi is true, ; starts a comment as well as #.The example for this item is shown with the ones above: encoding-envfile-Parse.salam
ExpandDoc
func ExpandDoc(d &: Doc, base &: Doc): Docbase.The example for this item is shown with the ones above: encoding-envfile-Parse.salam
Expand
func Expand(raw: str, d &: Doc, base &: Doc): strbase.The example for this item is shown with the ones above: encoding-envfile-Parse.salam
RefNames
func RefNames(d &: Doc): Vector<str>The example for this item is shown with the ones above: encoding-envfile-Parse.salam
RefNamesIn
func RefNamesIn(raw: str): Vector<str>$A and the ${A} form.The example for this item is shown with the ones above: encoding-envfile-Parse.salam