A simple yet powerful tool written in Go for merging multiple YAML files. This tool allows you to merge complex YAML structures such as lists and maps, with the ability to ignore specific keys during the merge process.
From releases This installs binary.
- Linux
curl -LO "https://github.com/dhyanio/yaml-merger/releases/download/$(curl -s https://api.github.com/repos/dhyanio/yaml-merger/releases/latest | grep tag_name | cut -d '"' -f 4)/yaml-merger-linux-amd64"
chmod +x yaml-merger-linux-amd64
sudo mv yaml-merger-linux-amd64 /usr/local/bin/yaml-merger
- MacOS
curl -LO "https://github.com/dhyanio/yaml-merger/releases/download/$(curl -s https://api.github.com/repos/dhyanio/discache/releases/latest | grep tag_name | cut -d '"' -f 4)/yaml-merger-darwin-amd64"
chmod +x yaml-merger-darwin-amd64
sudo mv yaml-merger-darwin-amd64 /usr/local/bin/yaml-merger
From source
- Run
git clone <yaml-merger repo> && cd yaml-merger/
- Run
make build
Go install the tool, use the go install command:
go install github.com/dhyanio/yaml-merger@latest
This will install the yaml-merger binary, which you can run from your terminal.
A CLI tool has commands
- Swagger: Merge multiple swagger files into a swagger file, support JSON/YAML.
- Recursive Merging: Handles nested structures, including lists and maps.
- Ignore Keys: Optionally exclude specific keys from merging.
- Error-Resilient: Provides clear error messages for parsing and merging issues.
- Simple CLI Usage: Merge files directly from the command line.
- Dry Run: Preview merged content without saving.
- Output: Output merged content to the specified file.
- Custom Merge Strategies: Choose between merge (deep merge) and override for key conflicts.
After installing the tool, you can run it from the command line:
yaml-merger [options] <file1.yaml> <file2.yaml> [...]
yaml-merger config1.yaml config2.yaml config3.yaml
This will merge the contents of config1.yaml, config2.yaml, and config3.yaml and output the result to stdout.
-
--ignore
: A comma-separated list of keys to ignore during the merge process.yaml-merger --ignore=key1,key2 file1.yaml file2.yaml
This will merge file1.yaml and file2.yaml while ignoring the specified keys.
-
--dry-run
: Simulates the process without writing to a file. Instead, it prints the merged content.yaml-merger --dry-run file1.yaml file2.yaml
This will display the merged YAML without saving it to a file.
-
--merge-strategy
: Output merged YAML content to the specified file. If not, it prints the output to stdout.yaml-merger --merge-strategy merge file1.yaml file2.yaml
Deep Merge: Use the default "merge" strategy for recursive merging.
yaml-merger --merge-strategy override file1.yaml file2.yaml
Override: Use the "override" strategy to override values when keys clash.
-
--output
: Output merged YAML content to the specified file. If not, it prints the output to stdout.yaml-merger --output result.yaml file1.yaml file2.yaml
Merge YAML files and write to file (without dry run)
file1.yaml
app:
name: "MyApp"
version: "1.0"
environment: "production"
features:
- "logging"
- "monitoring"
file2.yaml
app:
version: "2.0"
author: "Dhyanio"
features:
- "caching"
- "alerts"
Merged Output:
app:
name: "MyApp"
version: "2.0"
author: "Dhyanio"
environment: "production"
features:
- "Logging"
- "Monitoring"
- "caching"
- "alerts"
The tool uses https://github.com/dhyanio/gogger for structured logging. Logs are set to INFO by default, but you can enable debug-level logging for detailed output.
LOG_LEVEL=debug yaml-merger file1.yaml file2.yaml
The tool provides detailed error messages in case of:
- File Read Errors: The tool will display an error message if a file cannot be read.
- YAML Parse Errors: If there are syntax issues in the YAML files.
- Type Mismatch: If incompatible types are encountered during merging (e.g., list vs map).
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
- Fork the repository.
- Create your feature branch (git checkout -b feature/new-feature).
- Commit your changes (git commit -am 'Add new feature').
- Push to the branch (git push origin feature/new-feature).
- Create a new Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.
Thanks to the Go community for their support and contributions.