godenv

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2022 License: MIT Imports: 6 Imported by: 0

README

Go Reference Go Report Card codecov GitHub license CI

godenv — a proper package to read .env files

The Twelve-Factor App Manifest promotes using environment variables as the only configuration approach. We follow and love this principle.

The .env files are just a genuine extension of env vars. The .env files simplify local debug, exposing variables to a Docker containers, and so on.

Godenv is a tiny package that reads those .env files.

Motivation

We took inspiration from the godotenv repository. The goal we pursued was to write a parser without using regular expressions but with a lexer/parser approach.

If you are curious about learning more about the approach, see the following links:

Installation

go get github.com/youla-dev/godenv

Usage

Add a configuration to your .env file:

HTTP_ADDRESS=":8080"
LOG_LEVEL="info"

Then in the Go app read the file and parse its content:

package main

import (
	"fmt"
	"os"

	"github.com/youla-dev/godenv"
)

func main() {
	f, err := os.Open(".env")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	vars, err := godenv.Parse(f)
	if err != nil {
		panic(err)
	}

	fmt.Println(vars)
}

Pronunciation

godenv stands for go-dot-env. It is pronounced as goh denv, not as gahd env.

Specification

The complete specification can be found here: SPECIFICATION.md.

Documentation

Overview

Package godenv is a tiny module that parses .env files.

Motivation

We took inspiration from the godotenv (https://github.com/joho/godotenv) repository. The goal we pursued was to write a parser without using regular expressions but with a lexer/parser approach.

The current specification of .env files format is listed in SPECIFICATION.md (https://github.com/youla-dev/godenv/blob/main/SPECIFICATION.md).

If you are curious about learning more about the approach, see the following links:

Usage

Let's assume, you have a .env file with the following content:

HTTP_LISTEN=":8080"
LOG_LEVEL="info"

You can easily open the file and parse its content into map[string]string:

f, err := os.Open(".env")
if err != nil {
	panic(err)
}
defer f.Close()

vars, err := godenv.Parse(f)
if err != nil {
	panic(err)
}
fmt.Println(vars)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(r io.Reader) (map[string]string, error)

Parse reads an env file from io.Reader, returning a map of keys and values.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/youla-dev/godenv"
)

func main() {
	envContent := `VARIABLE_1='This is variable 1'
# This is comment
VARIABLE_2="This is variable 2"
VARIABLE_TAB_1='Tab is not escaped\t'
VARIABLE_TAB_2="Tab is escaped\t"`

	buf := bytes.NewBufferString(envContent)

	result, err := godenv.Parse(buf)
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
}
Output:

map[VARIABLE_1:This is variable 1 VARIABLE_2:This is variable 2 VARIABLE_TAB_1:Tab is not escaped\t VARIABLE_TAB_2:Tab is escaped	]

Types

This section is empty.

Directories

Path Synopsis
internal
ast
Package ast declares the types used to represent syntax trees for the .env file.
Package ast declares the types used to represent syntax trees for the .env file.
parser
Package parser implements a parser for the .env files.
Package parser implements a parser for the .env files.
token
Package token defines constants representing the lexical tokens of the .env file.
Package token defines constants representing the lexical tokens of the .env file.

Jump to

Keyboard shortcuts

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