An open API service indexing awesome lists of open source software.

https://github.com/yuseferi/envyaml

env Yaml is a configuration loader from Yaml file with enviromental variable that helps to have secret placeholders in yaml file.
https://github.com/yuseferi/envyaml

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# πŸ” envYaml

### Seamlessly merge YAML configuration with environment variables

[![Go Version](https://img.shields.io/badge/Go-1.25+-00ADD8?style=for-the-badge&logo=go&logoColor=white)](https://go.dev/)
[![codecov](https://img.shields.io/codecov/c/github/yuseferi/envyaml?style=for-the-badge&logo=codecov&logoColor=white)](https://codecov.io/github/yuseferi/envyaml)
[![CI](https://img.shields.io/github/actions/workflow/status/yuseferi/envyaml/ci.yml?style=for-the-badge&logo=github&label=CI)](https://github.com/yuseferi/envyaml/actions/workflows/ci.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/yuseferi/envyaml?style=for-the-badge)](https://goreportcard.com/report/github.com/yuseferi/envyaml)
[![License](https://img.shields.io/badge/License-GPL_v3-blue?style=for-the-badge)](LICENSE)
[![Release](https://img.shields.io/github/v/release/yuseferi/envyaml?style=for-the-badge&logo=github)](https://github.com/yuseferi/envyaml/releases)


envYaml Logo

**Keep your configuration clean. Keep your secrets safe.**

[Installation](#-installation) β€’
[Quick Start](#-quick-start) β€’
[Features](#-features) β€’
[Examples](#-examples) β€’
[Contributing](#-contributing)

---

## 🎯 The Problem

You love YAML for configurationβ€”it's clean, readable, and organized. But what about sensitive data like API keys, database passwords, and tokens? Hardcoding them is a security nightmare. 😱

## ✨ The Solution

**envYaml** bridges the gap between clean YAML configuration and secure environment variable management. Reference environment variables directly in your YAML files, and envYaml handles the rest!

```yaml
# config.yml - Clean and secure! πŸ”’
database:
host: localhost
port: 5432
password: ${DB_PASSWORD} # Loaded from environment

api:
key: ${API_KEY} # Never committed to git
secret: ${API_SECRET} # Always secure
```

## πŸš€ Installation

```bash
go get github.com/yuseferi/envyaml@latest
```

**Requirements:** Go 1.25+

## ⚑ Quick Start

**1. Create your YAML configuration:**

```yaml
# config.yml
host: localhost
port: 3606
password: ${DB_PASSWORD}
```

**2. Define your config struct:**

```go
type Config struct {
Host string `yaml:"host" env:"HOST"`
Port int `yaml:"port" env:"PORT"`
Password string `yaml:"password" env:"DB_PASSWORD,required"`
}
```

**3. Load and use:**

```go
package main

import (
"fmt"
"log"

"github.com/yuseferi/envyaml"
)

func main() {
var cfg Config

if err := envyaml.LoadConfig("config.yml", &cfg); err != nil {
log.Fatal(err)
}

fmt.Printf("Connected to %s:%d\n", cfg.Host, cfg.Port)
}
```

## 🎨 Features

| Feature | Description |
|---------|-------------|
| πŸ”„ **Seamless Integration** | Combine YAML files with environment variables effortlessly |
| βœ… **Required Variables** | Mark critical env vars as required with automatic validation |
| 🏷️ **Struct Tags** | Use familiar `yaml` and `env` struct tags |
| πŸ›‘οΈ **Type Safety** | Full Go type safety with automatic type conversion |
| πŸ“¦ **Zero Config** | Works out of the box with sensible defaults |
| πŸͺΆ **Lightweight** | Minimal dependencies, maximum performance |

## πŸ“š Examples

### Required Environment Variables

Mark sensitive variables as required to fail fast if they're missing:

```go
type Config struct {
Host string `yaml:"host" env:"HOST"`
Port int `yaml:"port" env:"PORT"`
Password string `yaml:"password" env:"DB_PASSWORD,required"` // πŸ‘ˆ Required!
}

var cfg Config
err := envyaml.LoadConfig("config.yml", &cfg)
if err != nil {
// Error: failed to parse environment variables: env: required environment variable "DB_PASSWORD" is not set
log.Fatal(err)
}
```

### Complete Working Example

```go
package main

import (
"fmt"
"log"
"os"

"github.com/yuseferi/envyaml"
)

type DatabaseConfig struct {
Host string `yaml:"host" env:"DB_HOST"`
Port int `yaml:"port" env:"DB_PORT"`
Username string `yaml:"username" env:"DB_USER"`
Password string `yaml:"password" env:"DB_PASSWORD,required"`
Database string `yaml:"database" env:"DB_NAME"`
}

type Config struct {
Database DatabaseConfig `yaml:"database"`
Debug bool `yaml:"debug" env:"DEBUG"`
}

func main() {
// Set environment variables (in production, these come from your environment)
os.Setenv("DB_PASSWORD", "super_secret_password")

var cfg Config
if err := envyaml.LoadConfig("config.yml", &cfg); err != nil {
log.Fatalf("Failed to load config: %v", err)
}

fmt.Printf("Database: %s@%s:%d/%s\n",
cfg.Database.Username,
cfg.Database.Host,
cfg.Database.Port,
cfg.Database.Database,
)
}
```

With this `config.yml`:

```yaml
database:
host: localhost
port: 5432
username: admin
password: ${DB_PASSWORD}
database: myapp

debug: false
```

**Output:**
```
Database: admin@localhost:5432/myapp
```

## πŸ—οΈ How It Works

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ YAML File β”‚ β”‚ Environment β”‚ β”‚ Go Struct β”‚
β”‚ β”‚ β”‚ Variables β”‚ β”‚ β”‚
β”‚ host: localhostβ”‚ β”‚ β”‚ β”‚ Host: localhostβ”‚
β”‚ port: 3606 β”‚ ──► β”‚ DB_PASSWORD= β”‚ ──► β”‚ Port: 3606 β”‚
β”‚ password: ${..}β”‚ β”‚ "secret123" β”‚ β”‚ Password: ... β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
envYaml
```

1. **Read** - Parse your YAML configuration file
2. **Merge** - Overlay environment variables using struct tags
3. **Validate** - Ensure required variables are present
4. **Return** - Provide a fully populated, type-safe config struct

## πŸ› οΈ Development

This project uses [Task](https://taskfile.dev) for managing development tasks.

```bash
# Build the project
task build

# Run tests
task test

# Run tests with coverage
task test-coverage

# Clean generated files
task clean

# Run all tasks
task all
```

## 🀝 Contributing

We love contributions! ❀️

1. 🍴 Fork the repository
2. 🌿 Create your feature branch (`git checkout -b feature/amazing-feature`)
3. πŸ’Ύ Commit your changes (`git commit -m 'Add amazing feature'`)
4. πŸ“€ Push to the branch (`git push origin feature/amazing-feature`)
5. πŸŽ‰ Open a Pull Request

Please feel free to:
- πŸ› Report bugs
- πŸ’‘ Suggest new features
- πŸ“– Improve documentation
- ⭐ Star the project if you find it useful!

## πŸ“„ License

This project is licensed under the **GNU General Public License v3.0** - see the [LICENSE](LICENSE) file for details.

---

**Made with ❀️ by [Yusef Mohamadi](https://github.com/yuseferi)**

If this project helped you, consider giving it a ⭐!

[![GitHub stars](https://img.shields.io/github/stars/yuseferi/envyaml?style=social)](https://github.com/yuseferi/envyaml/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/yuseferi/envyaml?style=social)](https://github.com/yuseferi/envyaml/network/members)