dotenv

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package dotenv supports reading and loading environment variables from .env files based on active environment (e.g. prod, dev etc.). The order or reading is as follows:

  • .env
  • .env.local
  • .env.{active-env}
  • .env.{active-env}.local

> It is recommended to not commit any .local files to the repository as these represent variables that are specific to your local environment.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load added in v0.4.0

func Load(dir string, ae ActiveEnvironment) error

Load sets the environment variables from the active environment using env.Load.

func LoadFS added in v0.4.0

func LoadFS(fsys fs.FS, dir string, ae ActiveEnvironment) error

LoadFS sets the environment variables from the active environment using env.Load.

func Overload added in v0.4.0

func Overload(dir string, ae ActiveEnvironment) error

Overload sets and overwrites the environment variables from the active environment using env.Overload.

func OverloadFS added in v0.4.0

func OverloadFS(fsys fs.FS, dir string, ae ActiveEnvironment) error

OverloadFS sets and overwrites the environment variables from the active environment using env.Overload.

Types

type ActiveEnvironment added in v0.4.0

type ActiveEnvironment string

ActiveEnvironment is the active environment.

const (
	None        ActiveEnvironment = ""
	Development ActiveEnvironment = "dev"
	Testing     ActiveEnvironment = "test"
	Production  ActiveEnvironment = "prod"

	LongFlag = "active-env"
)

func GetActiveEnvironment added in v0.4.0

func GetActiveEnvironment(args []string) (ActiveEnvironment, []string)

GetActiveEnvironment returns the active environment and the remaining arguments from the provided args.

args := os.Args[1:]
env, args := dotenv.GetActiveEnvironment(args...)

func GetActiveEnvironmentOr added in v0.4.0

func GetActiveEnvironmentOr(args []string, def ActiveEnvironment) (ActiveEnvironment, []string)

func (ActiveEnvironment) String added in v0.4.0

func (ae ActiveEnvironment) String() string

type NoFilesLoadedError added in v0.4.2

type NoFilesLoadedError struct {
	FS  fs.FS
	Dir string
}

func (*NoFilesLoadedError) Error added in v0.4.2

func (e *NoFilesLoadedError) Error() string

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

A Reader reads .env files from a filesystem and provides the mechanism to lookup environment variables. Its zero value is ready to use and reads from the current working directory.

func Read

func Read(dir string, ae ActiveEnvironment) *Reader

Read reads .env files from dir, depending on the provided ActiveEnvironment.

var cfg MyConfig
dec := env.NewDecoder(dotenv.Read("./", dotenv.Development))
dec.Decode(&cfg)
Example

This example reads .env files from the "example" directory and decodes the found variables into a struct.

package main

import (
	"embed"
	"github.com/davecgh/go-spew/spew"
	"github.com/go-pogo/env"
	"time"
)

//go:embed example/*
var fsys embed.FS

// This example reads .env files from the "example" directory and decodes the
// found variables into a struct.
func main() {
	type Config struct {
		Foo     string
		Timeout time.Duration `default:"10s"`
	}

	var conf Config
	if err := env.NewDecoder(ReadFS(fsys, "example", None)).Decode(&conf); err != nil {
		panic(err)
	}

	spew.Dump(conf)
}
Output:

(dotenv.Config) {
 Foo: (string) (len=3) "bar",
 Timeout: (time.Duration) 2s
}

func ReadFS added in v0.4.0

func ReadFS(fsys fs.FS, dir string, ae ActiveEnvironment) *Reader

ReadFS reads .env files at dir from fsys, depending on the provided ActiveEnvironment.

func (*Reader) Close

func (r *Reader) Close() error

Close closes all opened .env files.

func (*Reader) Environ added in v0.4.0

func (r *Reader) Environ() (env.Map, error)

Environ reads and returns all environment variables from the loaded .env files.

func (*Reader) Lookup

func (r *Reader) Lookup(key string) (env.Value, error)

Lookup key by reading from .env files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL