Skip to content

Latest commit

 

History

History

goconfig

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

goconfig

import "github.com/altessa-s/go-atlas/tools/codegen/goconfig/commands"

Configuration file converter for the go-atlas config loader (config/loader).

Overview

goconfig converts between different configuration formats, understanding the config/loader environment structure with proper field name transformations and nesting.

Installation

go install github.com/altessa-s/go-atlas/cmd/goconfig@latest

Usage

Convert YAML to Environment Variables

goconfig convert --from config.yaml --to .env

Convert Environment Variables to YAML

goconfig convert --from .env --to config.yaml

Convert TOML to Environment Variables

goconfig convert --from config.toml --to .env

Supported Formats

Format Extensions Description
YAML .yaml, .yml YAML configuration files
TOML .toml TOML configuration files
Env .env Environment variable files
Markdown .md Documentation tables

Conversion Rules

Field Name Transformation

  • camelCase → SCREAMING_SNAKE_CASE
  • Nested structures use __ (double underscore) delimiter
  • Arrays stay in one variable as a bracketed list: ARRAY=[a,b,c]

Examples

YAML Structure Environment Variable
grpc.interceptors.realIP.enabled GRPC__INTERCEPTORS__REAL_IP__ENABLED
database.connectionPool.maxSize DATABASE__CONNECTION_POOL__MAX_SIZE
logging.outputs: [console, file] LOGGING__OUTPUTS=[console,file]

Commands

convert

Convert configuration between formats:

goconfig convert [flags]

Flags:
  -f, --from string           Source file or directory (required)
  -t, --to string             Destination file (required)
      --from-format string    Format of source (auto-detect by extension)
      --to-format string      Format of destination (auto-detect by extension)
      --parse-comments        Include commented lines in conversion
      --use-claude            Generate Markdown field descriptions with the Claude API
      --claude-api-key string Claude API key (or ANTHROPIC_API_KEY)

Configuration Examples

YAML to .env

Input (config.yaml):

database:
  host: localhost
  port: 5432
  pool:
    maxSize: 10
    timeout: 30s

logging:
  level: info
  outputs:
    - console
    - file

Output (.env):

DATABASE__HOST=localhost
DATABASE__PORT=5432
DATABASE__POOL__MAX_SIZE=10
DATABASE__POOL__TIMEOUT=30s
LOGGING__LEVEL=info
LOGGING__OUTPUTS=[console,file]

The real output also groups the variables by top-level section under # <Section> Configuration comment headers.

.env to YAML

Input (.env):

SERVER__HOST=0.0.0.0
SERVER__PORT=8080
SERVER__TLS__ENABLED=true
SERVER__TLS__CERT_FILE=/etc/ssl/cert.pem
SERVER__TLS__KEY_FILE=/etc/ssl/key.pem

Output (config.yaml):

server:
  host: 0.0.0.0
  port: 8080
  tls:
    enabled: true
    certFile: /etc/ssl/cert.pem
    keyFile: /etc/ssl/key.pem

Directory Processing

Convert all configuration files in a directory:

# Merge configs matching the detected source format in ./configs/ into one .env
goconfig convert --from ./configs/ --to merged.env

# Convert each file individually
for f in configs/*.yaml; do
  goconfig convert --from "$f" --to "env/$(basename "$f" .yaml).env"
done

Integration with config/loader

This converter produces environment variables compatible with the config/loader parser:

// Load environment variables generated by goconfig
cfg := &MyConfig{}
_, err := loader.New(&yaml3.Backend{}).Load(cfg)

Format Detection

File format is automatically detected by extension:

  • .yaml, .yml → YAML
  • .toml → TOML
  • .env → Environment variables
  • .md → Markdown tables

Override with --from-format and --to-format flags.

See Also