dotenv

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2021 License: MIT Imports: 10 Imported by: 0

README

dotenv

build status Go Report Card godocs

A Go (golang) implementation of dotenv (inspired by: https://github.com/joho/godotenv).

Installation

As a Library:

go get github.com/fairyhunter13/dotenv

Usage

In your environment file (canonically named .env):

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE

MESSAGE="A message containing important spaces."
ESCAPED='You can escape you\'re strings too.'

# A comment line that will be ignored
GIT_PROVIDER=github.com
LIB=${GIT_PROVIDER}/fairyhunter13/dotenv # variable interpolation (plus ignored trailing comment)

In your application:

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/fairyhunter13/dotenv"
)

func main() {
  err := dotenv.Load()
  if err != nil {
    log.Fatalf("Error loading .env file: %v", err)
  }

  s3Bucket := os.Getenv("S3_BUCKET")
  secretKey := os.Getenv("SECRET_KEY")

  fmt.Println(os.Getenv("MESSAGE"))
}

Documentation

https://godoc.org/github.com/fairyhunter13/dotenv

Documentation

Overview

Package dotenv is an implementation of the Ruby dotenv library. The purpose of the library is to load variables from a file into the environment.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidln = errors.New("invalid line")
	ErrEmptyln   = errors.New("empty line")
	ErrCommentln = errors.New("comment line")
)

Errors occurred during parsing.

Functions

func Load

func Load(paths ...string) (err error)

Load will load a variadic number of environment config files. Will not overwrite currently set env vars.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/fairyhunter13/dotenv"
)

func main() {
	err := dotenv.Load("fixtures/example.env")
	if err != nil {
		log.Fatal(err)
	}

	envKeys := []string{"S3_BUCKET", "SECRET_KEY", "MESSAGE"}
	for _, key := range envKeys {
		fmt.Printf("%s : %s\n", key, os.Getenv(key))
	}
}
Output:
S3_BUCKET : YOURS3BUCKET
SECRET_KEY : YOURSECRETKEYGOESHERE
MESSAGE : A message containing important spaces.

func Load2 added in v1.1.0

func Load2(opts ...FnOption) (err error)

Load2 is the version 2 of Load.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/fairyhunter13/dotenv"
)

func main() {
	err := dotenv.Load2(
		dotenv.WithPaths("fixtures/example.env"),
	)
	if err != nil {
		log.Fatal(err)
	}

	envKeys := []string{"S3_BUCKET", "SECRET_KEY", "MESSAGE"}
	for _, key := range envKeys {
		fmt.Printf("%s : %s\n", key, os.Getenv(key))
	}
}
Output:
S3_BUCKET : YOURS3BUCKET
SECRET_KEY : YOURSECRETKEYGOESHERE
MESSAGE : A message containing important spaces.

func LoadMap

func LoadMap(envMap map[string]string, overload bool)

LoadMap loads a map into the os environment, optionally overwriting existing vars.

func LoadReader

func LoadReader(r io.Reader) error

LoadReader will load an environment config from a reader interface. Will not overwrite currently set env vars.

func LoadReader2 added in v1.1.0

func LoadReader2(r io.Reader) error

LoadReader2 is the version 2 of LoadReader.

func Overload

func Overload(paths ...string) (err error)

Overload will load a variadic number of environment config files. Overwrites currently set env vars.

func ParseString

func ParseString(s string) (key, value string, err error)

ParseString parses a given string into a key, value pair. Returns the key, value, and an error.

Example
package main

import (
	"fmt"

	"github.com/fairyhunter13/dotenv"
)

func main() {
	envStrs := []string{
		`FOO=bar`,
		`FOO="escaped\"bar with quote"`,
		`FOO="bar\nbaz"`,
		`FOO.BAR=foobar`,
		`FOO="bar#baz" # comment`,
		`INVALID LINE`,
	}

	for _, s := range envStrs {
		k, v, err := dotenv.ParseString(s)
		if err != nil {
			fmt.Printf("parsing error: %v", err)
			continue
		}
		fmt.Printf("%s : %s\n", k, v)
	}
}
Output:
FOO : bar
FOO : escaped"bar with quote
FOO : bar
baz
FOO.BAR : foobar
FOO : bar#baz
parsing error: invalid line

func Read

func Read(rd io.Reader) (map[string]string, error)

Read parses the given reader's contents and return values as a map.

func Read2 added in v1.1.0

func Read2(rd io.Reader) (map[string]string, error)

Read2 is the version 2 of Read.

func ReadFile

func ReadFile(path string) (map[string]string, error)

ReadFile reads an env file at a given path, and return values as a map.

Example
package main

import (
	"fmt"
	"log"

	"github.com/fairyhunter13/dotenv"
)

func main() {
	env, err := dotenv.ReadFile("fixtures/example.env")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s : %s\n", "LIB", env["LIB"])
}
Output:
LIB : github.com/fairyhunter13/dotenv

func ReadFile2 added in v1.1.0

func ReadFile2(path string) (map[string]string, error)

ReadFile2 is the version 2 of ReadFile.

Types

type FnOption added in v1.1.0

type FnOption func(*Option)

FnOption is a functional option for this package

func WithOverload added in v1.1.0

func WithOverload(overload bool) FnOption

WithOverload overdrives the current set environment.

func WithPaths added in v1.1.0

func WithPaths(paths ...string) FnOption

WithPaths specifies all paths of the environment file.

type Option added in v1.1.0

type Option struct {
	Overload bool
	Paths    []string
}

Option is the option struct.

func (*Option) Default added in v1.1.0

func (o *Option) Default()

Default specifies the default values of the option struct.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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