dotenv

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2020 License: MIT Imports: 6 Imported by: 0

README

dotenv -- a golang library (and tool) for using Docker-style dotenv files

Command installation

$ go poseur.com/dotenv/cmd/dotenv

The dotenv command reads an environment file (from .env by default) and executes the command given in the remaining arguments with the contents of the read file added to the environment. The status code of dotenv is the status code returned by the underlying command. If dotenv cannot execute the given command at all, it returns a status code of 127, based on the assumption that it could not be executed because it does not exist (in $PATH, at least).

Like Docker, dotenv skips lines beginning with octothorpes and empty lines. Perhaps unlike Docker (but then again perhaps like Docker) whitespace from the beginning and end of every line is trimmed before the line is judged as a comment, an empty line, or an environment variable definition.

Package usage

It's important to keep in mind that initialization code that depends on the state of the environment will not see the environment variables established by this library. Here's an example of using dotenv in a program. It shows how your command can support environment files with a few lines of code:


package main

import (
        "flag"
        "log"
        "os"
             
        "poseur.com/dotenv"
)                         
        
var envfile = flag.String("env", ".env", "environment file")
var initialFoo = os.Getenv("FOO")                 
                                          
func main() {
        flag.Parse()
        _ = dotenv.SetenvFile(*envfile)
        key := flag.Arg(0)
        val := os.Getenv(key)
        log.Printf("Env var FOO=%s (pre SetenvFile())\n", initialFoo)
        log.Printf("Env var %s=%s\n", key, val)
}

Notes

This library is hosted at poseur.com/dotenv thanks to Pkgbot.dev.

Documentation

Overview

Package dotenv implements support for dotenv files as described at <https://docs.docker.com/compose/env-file/>. Note that this means that comments may appear only at the beginning of lines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetenvFile

func SetenvFile(filename string) error

SetenvFile parses the contents of filename as a Docker-style dotenv file and loads the values into the current environment. If a variable name has an empty value, the variable is unset.

Types

type Env

type Env []string

Env is an array of name-value pairs from a Dockerfile-style dotenv file.

func ReadFile

func ReadFile(filename string) (Env, error)

ReadFile reads filename and returns a Env object containing each nonblank, not comment line. The lines are not guaranteed to be sensical.

func (Env) Dump

func (e Env) Dump(out io.Writer) error

Dump writes the content of the passed environment.

func (Env) Exec

func (e Env) Exec(args []string) int

func (Env) Setenv

func (e Env) Setenv() error

Setenv aguments the current environment with values from the passed Env object.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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