Skip to content

HanDaber/cfg2env

Repository files navigation

cfg2env Logo

cfg2env

config to env

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

Go MIT License


💡 Use Cases

# Filter configs for specific environments or services
cat config.yaml | cfg2env --include "PROD_*" --exclude "*_PASSWORD,*_SECRET" > prod.env
cat config.yaml | cfg2env --include "DATABASE_*,CACHE_*" > data-services.env

# Decrypt sops gpg-encrypted configs
sops -d secrets.yaml | cfg2env > .env
# or age-encrypted
SOPS_AGE_KEY_FILE=~/.config/sops/age/keys.txt sops -d secrets.yaml | cfg2env > .env

# Extract Kubernetes ConfigMap data
kubectl get configmap my-config -o json | jq .data | cfg2env --format json > .env

# Convert Terraform outputs
terraform output -json | cfg2env --format json > .env

# Process remote configs
curl -s https://api.example.com/config | cfg2env --format json > .env

🔌 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
  • Customizable underscore handling with --dunder parameter
  • Flexible filtering with --include and --exclude glob patterns

🚀 Installation

Quick Install (Recommended)

# Install latest version
curl -fsSL https://raw.githubusercontent.com/HanDaber/cfg2env/main/install.sh | sh

# Install specific version
curl -fsSL https://raw.githubusercontent.com/HanDaber/cfg2env/main/install.sh | VERSION=v0.1.0 sh

# Custom install directory
curl -fsSL https://raw.githubusercontent.com/HanDaber/cfg2env/main/install.sh | INSTALL_DIR=~/bin sh

Alternative Methods

# Via Go
go install github.com/handaber/cfg2env@latest

# From source
git clone https://github.com/HanDaber/cfg2env.git
cd cfg2env
./install.sh --local

🚀 Quick Start

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

# Control underscore handling
cat config.yaml | cfg2env --dunder 1 > .env  # Remove 1 underscore from consecutive sequences
cat config.yaml | cfg2env --dunder 3 > .env  # Remove 3 underscores from consecutive sequences

# Filter output keys
cat config.yaml | cfg2env --include "DATABASE_*,API_*" > .env      # Only DATABASE_* and API_* keys
cat config.yaml | cfg2env --exclude "*_PASSWORD,*_SECRET" > .env   # Exclude sensitive keys
cat config.yaml | cfg2env --include "DATABASE_*" --exclude "*_PASSWORD" > .env  # Combine both

📋 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
Dunder Examples

With --dunder 1:

# Input: example_key
EXAMPLEKEY=value

# Input: example__key
EXAMPLE_KEY=value

# Input: example____key
EXAMPLE___KEY=value

With --dunder 3:

# Input: example_key
EXAMPLEKEY=value

# Input: example__key
EXAMPLEKEY=value

# Input: example____key
EXAMPLE_KEY=value
Filtering Examples
# Include only DATABASE keys
cat config.yaml | cfg2env --include "DATABASE_*"
# Output: DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD

# Exclude sensitive keys
cat config.yaml | cfg2env --exclude "*_PASSWORD,*_SECRET,*_TOKEN"
# Output: All keys except those ending in _PASSWORD, _SECRET, or _TOKEN

# Combine include and exclude (include first, then exclude)
cat config.yaml | cfg2env --include "DATABASE_*" --exclude "*_PASSWORD"
# Output: DATABASE_HOST, DATABASE_PORT (excludes DATABASE_PASSWORD)

# Patterns are case-insensitive and normalized
cat config.yaml | cfg2env --include "database_*"  # Same as "DATABASE_*"

# No matches produces empty output with comment
cat config.yaml | cfg2env --include "NONEXISTENT_*"
# Output: # No keys matched the specified filters

🛠️ 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

 
 
 

Contributors