dotenv

package module
v0.0.0-...-090a4d1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2017 License: MIT Imports: 9 Imported by: 15

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/alexsasharegan/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}/alexsasharegan/dotenv # variable interpolation (plus ignored trailing comment)

In your application:

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/alexsasharegan/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/alexsasharegan/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/alexsasharegan/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 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 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/alexsasharegan/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 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/alexsasharegan/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/alexsasharegan/dotenv

Types

This section is empty.

Jump to

Keyboard shortcuts

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