Skip to content

ffais/yaml-sort

Repository files navigation

YAML Sort

License Go Report Card Go Reference

A command-line tool to sort YAML files by keys while preserving comments, structure, and formatting.

Features

  • Preserves comments - Maintains all inline and standalone comments
  • Customizable sorting - Choose between alphabetical or custom key order
  • Format preservation - Keeps original indentation and style
  • Multi-document support - Handles YAML files with multiple documents (--- separators)
  • Strict mode - Optionally validates YAML during processing

Installation

Using Go:

go install github.com/ffais/yaml-sort@latest

Pre-built binaries:

Download from the Releases page

Usage

Basic sorting:

yaml-sort -f input.yaml -o sorted.yaml

Sort with custom key order:

yaml-sort -f input.yaml -o sorted.yaml -k "name,version,dependencies"

Sort in-place (modify file directly):

yaml-sort -f input.yaml -i

Options

  -f, --file string       Input YAML file
  -o, --output string     Output file (defaults to stdout if not specified)
  -i, --in-place          Modify input file directly
  -k, --keys string       Comma-separated list of keys for custom ordering
  -s, --strict            Enable strict YAML parsing
  -v, --version           Display version information
  -h, --help              Show help message

Examples

  1. Sort a Kubernetes manifest:
yaml-sort -f deployment.yaml -o sorted-deployment.yaml
  1. Sort with specific key priority:
yaml-sort -f config.yaml -k "apiVersion,kind,metadata,spec"
  1. Pipe input/output:
cat input.yaml | yaml-sort > sorted.yaml

Contributing

Pull requests are welcome! Please ensure:

  • Tests pass (go test ./...)
  • Code is formatted (go fmt)
  • New features include appropriate tests

License

MIT - See LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •