envparse

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2024 License: MIT Imports: 6 Imported by: 0

README

GoEnvParse

Go Reference CI

GoEnvParse is a Go package for parsing environment variables from .env files. It provides a simple and efficient way to load environment variables from .env file into your Go applications.

Features

  • Parse environment files from any io.Reader source
  • 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
  • Does not support multiline values

Installation

go get github.com/SameerJadav/go-envparse

Usage

package main

import (
	"log"
	"os"

	"github.com/SameerJadav/go-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)
	}
}

Contributing

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

License

GoEnvParse 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 env file from io.Reader, returning a map of keys-value pairs and an error.

Only double-quoted values are escaped. Single-quoted and backquoted values are treated as literal strings. Variable expansion (${...} and $...) is performed in non-quoted and double-quoted values.

Note: This function does not support multiline values. Each key-value pair must be on a single line.

Types

This section is empty.

Jump to

Keyboard shortcuts

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