Doc
#struct DocThe contents of an env file.
| Name | Type | Description |
|---|---|---|
m | collections.OrderedMap | The keys and values, in order. |
flags | Vector<int> | For each key, whether its value takes substitution; single quotes turn it off. |
import encoding.envfile
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")
mut raw := envfile.ParseRaw(text)
println envfile.Get(raw, "URL"), envfile.RefNames(raw)
mut base := envfile.New()
mut 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")
try:
_ := envfile.ParseStrict("=no key\n")
catch e:
println e.ToStr()
end
println 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)
end4 ["HOST", "PORT", "NAME", "URL"]
hello world false false
HOST 8080
http://localhost:8080/
http://${HOST}:${PORT}/ ["HOST", "PORT"]
http://localhost:8080/
localhost/api ["A", "B"]
true
HOST=localhost
PORT=8080
NAME="hello world"
DEBUG=true
true "a b"
9090 prod
envfile: expected KEY=VALUE (line 1, column 1)
true
a b x