https://github.com/thecodingmachine/yaml-tools
A set of CLI tools to manipulate YAML files (merge, edit, etc...)
https://github.com/thecodingmachine/yaml-tools
Last synced: 4 months ago
JSON representation
A set of CLI tools to manipulate YAML files (merge, edit, etc...)
- Host: GitHub
- URL: https://github.com/thecodingmachine/yaml-tools
- Owner: thecodingmachine
- Created: 2018-05-11T12:57:09.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T02:05:32.000Z (almost 3 years ago)
- Last Synced: 2025-04-11T18:59:53.377Z (7 months ago)
- Language: Python
- Size: 60.5 KB
- Stars: 20
- Watchers: 9
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://travis-ci.org/thecodingmachine/yaml-tools)
[](https://coveralls.io/github/thecodingmachine/yaml-tools?branch=master)
# Yaml-tools
A set of CLI tools to manipulate YAML files (merge, delete, etc...) with comment preservation, based on [ruamel.yaml](http://yaml.readthedocs.io/en/latest/)
## Getting Started
### Prerequisites
- Python3 (with pip)
### Installation
*(For development see section at the end)*
```
$ pip install ruamel.yaml
$ export YAML_TOOLS_VERSION=0.0.6
$ sudo wget https://raw.githubusercontent.com/thecodingmachine/yaml-tools/${YAML_TOOLS_VERSION}/src/yaml_tools.py -O /usr/local/bin/yaml-tools
$ sudo chmod +x /usr/local/bin/yaml-tools
```
## Usage
```
$ yaml-tools []
```
There are 4 commands at the moment :
### 1) merge
Merges two or more yaml files and preserves the comments.
```
$ yaml-tools merge -i INPUTS [INPUTS ...] [-o OUTPUT]
```
- **INPUTS**: paths to input yaml files, which will be merged from the last to the first.
- **OUTPUT**: path to output yaml file (or sys.stdout by default).
### 2) delete
Deletes one item/block (**and its preceding comments**) from the input yaml file.
```
$ yaml-tools delete PATH_TO_KEY -i INPUT [-o OUTPUT]
```
- **PATH_TO_KEY**: "path" to access the yaml item/block which will be deleted, e.g. `key1 0 key2`
- **INPUT**: path to input yaml file.
- **OUTPUT**: path to output yaml file (or sys.stdout by default).
### 3) normalize-docker-compose
Normalize the input docker-compose file by and converting all key-value string (e.g. 'foo=bar' or '80:8080')
to key-value dicts inside the services' `labels` and `environment` fields,
also delete all duplicated volumes and env_file (**and its preceding comments**) for each services
```
$ yaml-tools normalize-docker-compose -i INPUT [-o OUTPUT]
```
- **INPUT**: path to input yaml file.
- **OUTPUT**: path to output yaml file (or sys.stdout by default).
### 4) comment (/!\ EXPERIMENTAL)
Comments one item/block from the input yaml file and preserves the comments.
/!\ There are somme issues with comments which are at the end of any intermediate level/block,
and also commenting the last item from a list, so use it with caution.
```
$ yaml-tools comment PATH_TO_KEY -i INPUT [-o OUTPUT]
```
- **PATH_TO_KEY**: "path" to access the yaml item which will be commented, e.g. `key1 0 key2`
- **INPUT**: path to input yaml file.
- **OUTPUT**: path to output yaml file (or sys.stdout by default).
## Dev
### Installing
- Open a terminal console on this project's root folder
- Create a virtual environment with `python -m venv venv` (or `python3 -m venv venv`)
- Activate your venv with `.\venv\Scripts\activate` (Windows) or `source ./venv/bin/activate` (Linux or MacOS)
- Install all required packages with `pip install -r requirements.txt`
## Running tests
```
$ cd src/tests/
$ python -m unittest discover
or
$ coverage run --rcfile=../../.coveragerc --source=.,.. -m unittest discover && coverage report -m
```
##