envparse

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: MIT Imports: 7 Imported by: 0

README

EnvParse

Go Reference CI

EnvParse is a Go package designed for efficiently parsing environment variables from .env files. It provides a straightforward and performant way to load environment variables into your Go applications.

Features

  • Parse environment files from any io.Reader source
  • Parse environment files directly from a file
  • Handling of quoted values (double quotes, single quotes, and backticks)
  • Variable expansion in non-quoted and double-quoted values
  • Error reporting with line numbers for invalid syntax

Parsing Details

  • Double-quoted values are unescaped, including unicode characters
  • Single-quoted and backtick-quoted values are treated as literal strings
  • Variable expansion is performed in non-quoted and double-quoted values
  • Commented lines (lines prefixed with #), invalid lines (lines that are not comments and do not have an = sign), and lines with empty key will not be parsed
  • Inline comments (e.g., KEY=VALUE#inline comment) are removed from the value. If you want # in your value, you should quote it (e.g., KEY="VALUE#with hash")
  • Does not support multiline values

Installation

go get github.com/SameerJadav/envparse

Usage

Parse from io.Reader

package main

import (
	"log"
	"os"

	"github.com/SameerJadav/envparse"
)

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

	env, err := envparse.Parse(file)
	if err != nil {
		log.Fatal(err)
	}

	for key, value := range env {
		os.Setenv(key, value)
	}
}

Parse from File

package main

import (
	"log"
	"os"

	"github.com/SameerJadav/envparse"
)

func main() {
	env, err := envparse.ParseFile(".env")
	if err != nil {
		log.Fatal(err)
	}

	for key, value := range env {
		os.Setenv(key, value)
	}
}

Contributing

Contributions are welcome. Please open an issue or submit a pull request.

License

EnvParse is open-source and available under the MIT License.

Documentation

Index

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 environment variables file from the provided io.Reader and returns a map of key-value pairs. The function also returns an error if any issues are encountered during parsing.

func ParseFile added in v1.1.0

func ParseFile(filename string) (map[string]string, error)

ParseFile reads an environment variables file from the specified filename and returns a map of key-value pairs. The function also returns an error if any issues are encountered during file reading or parsing.

This function uses the Parse function internally to process the file contents.

Types

This section is empty.

Jump to

Keyboard shortcuts

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