With this small library it should be possible to update environment variables in a dot env file. With this library it is not possible to read out the variables, because there are already very good other libraries for this.
go get github.com/gowizzard/updenv
Here is an example of how to update a dot env file. Of course, only variables that exactly match the key can be updated. New variables cannot be added.
If you prefer not to use quotes for your variables, then you can set this in the config struct by setting the quotes value to false, or omitting it directly.
c := updenv.Config{
Path: filepath.Join("path", "to", "your", "file", ".env"),
Quotes: true,
}
err := c.Read()
if err != nil {
log.Fatal(err)
}
c.Updates = make(map[string]string)
c.Updates["EXAMPLE"] = "true"
err = c.Update()
if err != nil {
log.Fatal(err)
}
Thanks to JetBrains for supporting me with this and other open source projects.