A modular CLI tool for formatting YAML configuration files with consistent indentation and directive ordering. Currently supports Docker Compose and Traefik configurations.
- 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
go install github.com/awsqed/config-formatter@latestOr build from source:
git clone https://github.com/awsqed/config-formatter
cd config-formatter
go build -o config-formatterFormat a file and print to stdout (auto-detects format):
config-formatter -input docker-compose.yml
config-formatter -input traefik.ymlconfig-formatter -input myfile.yml -type docker-compose
config-formatter -input myfile.yml -type traefikconfig-formatter -input docker-compose.yml -output formatted.ymlconfig-formatter -input docker-compose.yml -wconfig-formatter -input traefik.yml -indent 4config-formatter -input docker-compose.yml -checkThis will exit with code 0 if the file is formatted, or 1 if it needs formatting.
-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
Formats Docker Compose files with best-practice directive ordering.
Top-Level Directives:
versionnamenetworksvolumesconfigssecretsservices(placed last as it's typically the longest section)
Service-Level Directives:
imagebuildcontainer_namehostnamecommandentrypointenvironmentenv_fileportsexposevolumesnetworksdepends_on- 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: alwaysFormats Traefik configuration files with logical grouping and ordering.
Top-Level Directives:
- Global configuration (
log,api,metrics, etc.) entryPointsproviderscertificatesResolvers- Protocol sections (
http,tcp,udp,tls)
HTTP Section:
routersservicesmiddlewaresserversTransports
Keys not in the predefined order are sorted alphabetically within their group.
The formatter uses a modular plugin architecture:
formatter/formatter.go: Core interface and base functionalitymodules/dockercompose/: Docker Compose formatter implementationmodules/traefik/: Traefik formatter implementation
To add support for a new config format:
- Create a new module directory under
modules/ - Implement the
Formatterinterface:Format(data []byte, indent int) ([]byte, error)- Format the configName() string- Return formatter nameCanHandle(filename string, data []byte) bool- Detect if file matches this format
- Register the formatter in
main.go
go run main.go -input compose.yml
go run main.go -input traefik.yml -type traefikBuild for all platforms:
make build-allBuild for specific platforms:
make build-linux-amd64
make build-darwin-arm64
make build-windows-amd64See all available commands:
make helpRun tests:
make testContributions are welcome! Please feel free to submit a Pull Request.
To contribute a new formatter module:
- Implement the
Formatterinterface - Add detection logic in
CanHandle() - Define format-specific ordering rules
- Update this README with format documentation