Documentation
¶
Overview ¶
Package dotenv wraps the joho/godotenv package to load environment files.
The env must be one of "development", "test", "production", or "agent". The "agent" environment is reserved for the coding agent's local work.
Load reads the following files depending on env. The first file has the highest precedence and .env has the lowest:
Priority Filename .gitignore? Secrets? Notes
Highest .env.{{env}}.local Yes Yes Environment-specific local overrides
2nd .env.local Yes Yes Local overrides
3rd .env.{{env}} No Never Shared environment-specific variables
Lowest .env No Never Shared for all environments
A file that does not exist is skipped, and so is a name that is not a regular file, such as a directory.
Errors ¶
Load returns ErrMissingEnvironment or ErrUnknownEnvironment for a bad env, before it looks at any file.
Load returns ErrClosedDirectory when the directory itself may not be searched, as when root runs a command as another user from a directory that user is closed out of, such as /root. Nothing has been loaded then. A caller that should take its settings from the process environment and its flags in that case can ignore it:
if err := dotenv.Load(env); err != nil && !errors.Is(err, dotenv.ErrClosedDirectory) {
return err
}
Any other failure to stat, read, or parse a file that is there is returned wrapped with the file's name, and errors.Is matches its cause, such as fs.ErrPermission for a file that may not be read. Loading stops at that file: the files of higher precedence have already been applied, and those of lower precedence have not. ErrClosedDirectory also matches fs.ErrPermission, so test for it first.
Load never overwrites a variable that is already set, even if it is empty. The real process environment always wins over every file, and the first file in the table above that defines a key wins over the later ones.
Variable references such as ${NAME} are expanded by godotenv, which sees only the keys defined earlier in the same file. A reference to a key from another file, or from the process environment, expands to the empty string.
WARNING: we are incompatible with bkeepers/dotenv since we load `.env.local` in test. Read https://github.com/bkeepers/dotenv/issues/418 for the history of this decision.
Index ¶
Constants ¶
const ( // ErrMissingEnvironment is returned when env is the empty string. ErrMissingEnvironment = Error("missing environment") // ErrUnknownEnvironment is returned when env is not one of // "development", "test", "production", or "agent". The match is // case-sensitive. ErrUnknownEnvironment = Error("unknown environment") // ErrClosedDirectory is returned when the directory that holds the // environment files may not be searched, as when root runs a command // as another user from /root. No file has been loaded when it is // returned, so the process environment is unchanged. // // The error also wraps the underlying *fs.PathError, so errors.Is // reports it as fs.ErrPermission too. A caller that wants to carry on // without the files must test for ErrClosedDirectory, not // fs.ErrPermission, which also matches a single unreadable file. ErrClosedDirectory = Error("closed directory") )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Error ¶
type Error string
Error is a string that implements the error interface, allowing sentinel errors to be declared as untyped constants.