GithubHelp home page GithubHelp logo

jonasi / go-envparse Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hashicorp/go-envparse

0.0 1.0 0.0 28 KB

Minimal environment variable parser for Go

License: Mozilla Public License 2.0

Go 100.00%

go-envparse's Introduction

GoDoc Build Status Coverage Status

go-envparse

A minimal Go environment variable parser. It's intended to be used to parse .env style files similar to godotenv or rubydotenv, but perform minimal allocations, handle more complex quoting, and be better tested.

Parsing a line does 2 allocations regardless of line length or complexity.

The parser supports JSON strings which allows for cross-language/platform encoding of arbitrarily complex data.

For example if you are parsing environment variables from a templated file, the template can JSON encode data that may contain newlines:

FOO={{ some_template_function | toJSON }}

...would be templated to:

FOO="The template value\nmay have included\nsome newlines!\n\ud83d\udd25"

...and envparse.Parse() would return:

map[string]string{
	"FOO": "The template value\nmay have included\nsome newlines!\n๐Ÿ”ฅ",
}

Minimal

The following common features are intentionaly missing:

  • Full shell quoting semantics
  • Full shell escape sequence support
    • Only JSON escape sequences are supported (see below)
  • Variable interpolation
  • Anything YAML related
    • No

However, comments, unquoted, single quoted, and double quoted text may all be used within a single value:

SOME_KEY = normal unquoted \text 'plus single quoted\' "\"double quoted " # EOL

...parses to:

map[string]string{
	"SOME_KEY": `normal unquoted \text plus single quoted\ "double quoted `
}

(Note the trailing space inside the double quote is kept, but the space between the final " and # is trimmed.)

Format

  • Keys should be of the form: [A-Za-z_][A-Za-z0-9_]?
    • Keys may be prefixed with export which will be ignored
    • Whitespace around keys will be trimmed
  • Values should be valid ASCII or UTF-8 encoded.
  • Newlines are always treated as delimiters, so newlines within values must be escaped.
  • Values may use one of more quoting styles:
    • Unquoted - FOO=bar baz
      • No escape sequences
      • Ends at #, ", ', or newline
      • Preceeding and trailing whitespace will be trimmed
    • Double Quotes - FOO="bar baz"
      • Supports JSON escape sequences: \uXXXX, \r, \n, \t, \\, and \"
      • Ends at unescaped "
      • No whitespace trimming
    • Single Quotes - FOO='bar baz'
      • No escape sequences
      • Ends at '
      • No whitespace trimming

See envparse_test.go for examples of valid and invalid data.

go-envparse's People

Contributors

angrycub avatar schmichael avatar sstent avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.