Skip to content

awsqed/config-formatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Config Formatter

License: MIT

A modular CLI tool for formatting YAML configuration files with consistent indentation and directive ordering. Currently supports Docker Compose and Traefik configurations.

Features

  • Multi-Format Support: Automatically detects and formats different config types
    • Docker Compose files
    • Traefik configuration files
    • Extensible architecture for adding more formats
  • Auto-Detection: Automatically identifies config type based on filename and content
  • Consistent Indentation: Configurable space-based indentation (default: 2 spaces)
  • Smart Directive Ordering: Format-specific ordering rules for better readability
  • Comment Preservation: Comments are preserved in their original positions
  • Multiple Output Options: Print to stdout, write to file, or modify in-place
  • Format Checking: Verify if files are already formatted

Installation

go install github.com/awsqed/config-formatter@latest

Or build from source:

git clone https://github.com/awsqed/config-formatter
cd config-formatter
go build -o config-formatter

Usage

Basic Usage

Format a file and print to stdout (auto-detects format):

config-formatter -input docker-compose.yml
config-formatter -input traefik.yml

Specify Format Type

config-formatter -input myfile.yml -type docker-compose
config-formatter -input myfile.yml -type traefik

Write to Output File

config-formatter -input docker-compose.yml -output formatted.yml

Format In-Place

config-formatter -input docker-compose.yml -w

Custom Indentation

config-formatter -input traefik.yml -indent 4

Check if File is Formatted

config-formatter -input docker-compose.yml -check

This will exit with code 0 if the file is formatted, or 1 if it needs formatting.

Command-Line Flags

  • -input (required): Input config file path
  • -output: Output file path (if not specified, prints to stdout)
  • -w: Write result to source file instead of stdout
  • -indent: Number of spaces for indentation (default: 2)
  • -check: Check if file is formatted without making changes
  • -type: Formatter type to use (docker-compose, traefik). Auto-detected if not specified

Supported Formats

Docker Compose

Formats Docker Compose files with best-practice directive ordering.

Top-Level Directives:

  1. version
  2. name
  3. networks
  4. volumes
  5. configs
  6. secrets
  7. services (placed last as it's typically the longest section)

Service-Level Directives:

  1. image
  2. build
  3. container_name
  4. hostname
  5. command
  6. entrypoint
  7. environment
  8. env_file
  9. ports
  10. expose
  11. volumes
  12. networks
  13. depends_on
  14. And many more...

Example:

Before formatting:

services:
  web:
    ports:
      - "8080:80"
    image: nginx:latest
    volumes:
      - ./html:/usr/share/nginx/html
    restart: always
networks:
  default:

After formatting:

networks:
  default:

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
    restart: always

Traefik

Formats Traefik configuration files with logical grouping and ordering.

Top-Level Directives:

  1. Global configuration (log, api, metrics, etc.)
  2. entryPoints
  3. providers
  4. certificatesResolvers
  5. Protocol sections (http, tcp, udp, tls)

HTTP Section:

  1. routers
  2. services
  3. middlewares
  4. serversTransports

Keys not in the predefined order are sorted alphabetically within their group.

Architecture

The formatter uses a modular plugin architecture:

  • formatter/formatter.go: Core interface and base functionality
  • modules/dockercompose/: Docker Compose formatter implementation
  • modules/traefik/: Traefik formatter implementation

Adding New Formatters

To add support for a new config format:

  1. Create a new module directory under modules/
  2. Implement the Formatter interface:
    • Format(data []byte, indent int) ([]byte, error) - Format the config
    • Name() string - Return formatter name
    • CanHandle(filename string, data []byte) bool - Detect if file matches this format
  3. Register the formatter in main.go

Development

Running Without Building

go run main.go -input compose.yml
go run main.go -input traefik.yml -type traefik

Building for Multiple Platforms

Build for all platforms:

make build-all

Build for specific platforms:

make build-linux-amd64
make build-darwin-arm64
make build-windows-amd64

See all available commands:

make help

Testing

Run tests:

make test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

To contribute a new formatter module:

  1. Implement the Formatter interface
  2. Add detection logic in CanHandle()
  3. Define format-specific ordering rules
  4. Update this README with format documentation

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors