Skip to content

Latest commit

 

History

History
112 lines (88 loc) · 3.33 KB

README.md

File metadata and controls

112 lines (88 loc) · 3.33 KB
env

streamline and simplify the way you work with environment variables


Features

  • .env File Support: Load variables from a .env file and/or any other file(s)
  • Type Conversions: Easily convert environment variables to Go types
  • Validation: Check common configuration errors (e.g. as.PortNo to enforce 0 <= X <= 65535)
  • Testing: Convenient testing utilities

Installation

go get github.com/blugnu/env

Example Usage

Override Default Configuration

Demonstrates the use of the env.Override function to replace a default configuration value with a value parsed from an environment variable:

    port := 8080
    if _, err := env.Override(&port, "SERVICE_PORT", as.PortNo); err != nil {
        log.Fatal(err)
    }
    log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))

Parse a Required Configuration Value

Demonstrates the use of the env.Parse function to parse a required configuration value from an environment variable:

    authURL, err := env.Parse("AUTH_SERVICE_URL", as.AbsoluteURL)
    if err != nil {
        log.Fatal(err)
    }

Load Configuration from a File

Demonstrates the use of the env.Load function to load configuration:

by default, with no filename(s) specified, the Load() function loads configuration from a .env file.

    if err := env.Load(); err != nil {
        log.Fatal(err)
    }

Preserve Environment Variables in a Test

Demonstrates the use of defer env.State().Reset() to preserve environment variables during a test:

    func TestSomething(t *testing.T) {
        // ARRANGE
        defer env.State().Reset()
        env.Vars{
            "SOME_VAR": "some value",
            "ANOTHER_VAR": "another value",
        }.Set()

        // ACT
        SomeFuncUsingEnvVars()

        // ASSERT
        ...
    }

Contributing

Contributions are welcome! Please feel free to submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.