Skip to content

HanDaber/cfg2env

Repository files navigation

cfg2env

A plugin-based tool for converting any config format to .env

Go MIT License


🔌 Supported Formats

Built-in plugins handle common configuration formats:

  • YAML - Complex nested structures
  • JSON - Modern API configs
  • SQLite - Database-driven settings
  • Your format here! - Add a plugin

✨ Core Features

  • Plugin-based architecture for unlimited format support
  • Smart key flattening for nested structures
  • Preserves array indices
  • Type-safe conversions
  • Clean .env output

🚀 Quick Start

# Install
go install github.com/handaber/cfg2env@latest

# Use with any supported format
cat config.yaml | cfg2env > .env
cat config.json | cfg2env --format json > .env
cat config.db | cfg2env --format sqlite > .env

📋 Examples

YAML Plugin
database:
  host: localhost
  port: 5432
  credentials:
    username: admin
    password: secret
api:
  features:
    - logging
    - metrics
JSON Plugin
{
  "database": {
    "host": "localhost",
    "port": 5432,
    "credentials": {
      "username": "admin",
      "password": "secret"
    }
  },
  "api": {
    "features": ["logging", "metrics"]
  }
}
SQLite Plugin
CREATE TABLE config (
  key TEXT PRIMARY KEY,
  value TEXT
);

INSERT INTO config (key, value) VALUES
  ('database_host', 'localhost'),
  ('database_port', '5432');

-- Custom queries supported:
-- cfg2env --format sqlite --query "SELECT name as key, value FROM settings"
Output (.env)
API_FEATURES_0=logging
API_FEATURES_1=metrics
DATABASE_CREDENTIALS_PASSWORD=secret
DATABASE_CREDENTIALS_USERNAME=admin
DATABASE_HOST=localhost
DATABASE_PORT=5432

🛠️ Development

make build  # Build the core and plugins
make test   # Run the test suite

🔌 Adding Plugins

The plugin system makes it easy to add support for new formats:

package myplugin

type Plugin struct {
    plugin.BasePlugin
}

func (p *Plugin) Parse(r io.Reader) (map[string]string, error) {
    // 1. Read your format
    // 2. Convert to key-value pairs
    // 3. Return the mapping
    return map[string]string{}, nil
}

MIT License • Built with 🍑 using Go

About

Structured yaml/json/sqlite/... → Flat .env

Resources

Stars

Watchers

Forks

Packages

No packages published