Skip to content

env Yaml is a configuration loader from Yaml file with enviromental variable that helps to have secret placeholders in yaml file.

License

Notifications You must be signed in to change notification settings

yuseferi/envyaml

Repository files navigation

envYaml (Yaml with environment value loader)

codecov Check & Build License: AGPL v3 GitHub release (latest SemVer) Go Report Card

Storing application configuration in YAML files offers a clean and straightforward solution, but it's crucial to avoid exposing sensitive data like passwords and API keys. Environment variables provide a secure way to store secrets, preventing them from leaking into your codebase.

The envyaml package bridges this gap, allowing you to seamlessly integrate environment variables into your YAML configuration. No more plain-text secrets or complex workarounds! Simply reference environment variables within your YAML file using placeholders, and envyaml will handle the rest, securely substituting the values during parsing.

With envyaml, you can:

  • Keep your configuration clean and organized in YAML.
  • Protect sensitive data by storing it in environment variables.
  • Enjoy a simple and intuitive integration process.
  • No more compromising between convenience and security. envyaml empowers you to manage your application configuration effectively while keeping your secrets safe.

Installation

To install envyaml, use go get:

go get github.com/yuseferi/envyaml@latest

Usage

Example 1: Error on required env variable

type TestConfig struct {
    Host     string `yaml:"host" env:"TEST_HOST"`
    Port     int    `yaml:"port" env:"TEST_PORT"`
    Password string `yaml:"password" env:"TEST_PASSWORD,required"`
}

// Load the configuration
var cfg TestConfig
// assume your configs are in `config.yml` file and this is its content:
//host: localhost
//port: 3606
//password: ${TEST_PASSWORD}
err := envyaml.LoadConfig("config.yml", &cfg)
if err != nil {
    log.Fatalln(err)
}
fmt.Println(cfg)

Error failed to parse environment variables: env: required environment variable "TEST_PASSWORD" is not set is expected because this variable has not been set.

Example 2: When env is defined

type TestConfig struct {
    Host     string `yaml:"host" env:"TEST_HOST"`
    Port     int    `yaml:"port" env:"TEST_PORT"`
    Password string `yaml:"password" env:"TEST_PASSWORD,required"`
}

// Load the configuration
var cfg TestConfig
// assume your configs are in `config.yml` file and this is its content:
//host: localhost
//port: 3606
//password: ${TEST_PASSWORD}
_ = os.Setenv("TEST_PASSWORD", "envyaml_pass")
err := envyaml.LoadConfig("config.yml", &cfg)
if err != nil {
log.Fatalln(err)
log.Fatalln(err)

    log.Fatalln(err)

}
fmt.Println(cfg)

Expected output:

{localhost 3606 envyaml_pass}

Development

This project uses Task for managing development tasks. Make sure you have Task installed on your system.

Available Tasks

  • task build: Build the project
  • task test: Run tests
  • task test-coverage: Run tests with coverage and generate a coverage report
  • task clean: Clean up generated files
  • task all: Run all tasks (build, test, and coverage)

To run a task, use the task command followed by the task name. For example:

task build

Running Tests

To run tests:

task test

To run tests with coverage:

task test-coverage

This will generate a coverage report in HTML format (coverage.html).

Contributing

We strongly believe in open-source ❤️😊. Please feel free to contribute by raising issues and submitting pull requests to make envYaml even better!

License

Released under the GNU GENERAL PUBLIC LICENSE.

About

env Yaml is a configuration loader from Yaml file with enviromental variable that helps to have secret placeholders in yaml file.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages