Skip to content

Commit

Permalink
Added README
Browse files Browse the repository at this point in the history
  • Loading branch information
campos-ddc committed Aug 23, 2021
1 parent 4c09642 commit a178fe6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
# yaml-patch
# yaml-patch

Apply patches to a yaml string, keeping most of the formatting and comments.

Some formatting is not kept due to underlying yaml library limitations:
- Indentation will be forced to two spaces
- Spacing before sequence dashes will be forced to two spaces
- Empty lines at the start of the string will be removed

## As a command line tool

You can pass any number of patches to be applied, they use the following syntax options:

### Patch a single value:
`<field>.<subfield>=<value>`

Example:
```bash
yaml-patch -f test.yml 'spec.replicas=2'
```

### Patch a value inside a single list item:
`<field>.[<position]>.<subfield>=<value>`

Example:
```bash
yaml-patch -f test.yml 'spec.template.containers.[0].image="mycontainer:latest"'
```

### Patch a value inside all list items:
`<field>.[].<subfield>=<value>`

Example:
```bash
yaml-patch -f test.yml 'spec.template.containers.[].image="mycontainer:latest"'
```

## As a Python library

To use `yaml-patch` as a library just import the function and pass patches as dictionary entries.

Example:

```python
from yaml_patch import patch
from textwrap import dedent

def override_list_all_values():
source_yaml = dedent(
"""\
some_list:
- alice
- bob
"""
)
patches = {"some_list.[]": "charlie"}
expected_yaml = dedent(
"""\
some_list:
- charlie
- charlie
"""
)
assert patch(source_yaml, patches) == expected_yaml
```
13 changes: 4 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@click.command(
help="""
Applies patches to a yaml string, keeping most of the formatting and comments.
Apply patches to a yaml string, keeping most of the formatting and comments.
\b
Some formatting is not kept due to underlying yaml library limitations:
Expand All @@ -22,24 +22,19 @@
Patch a single value:
<field>.<subfield>=<value>
Example:
spec.replicas=2
yaml-patch -f test.yml 'spec.replicas=2'
\b
Patch a value inside a single list item:
<field>.[<position]>.<subfield>=<value>
Example:
spec.template.containers.[0].image="mycontainer:latest"
yaml-patch -f test.yml 'spec.template.containers.[0].image="mycontainer:latest"'
\b
Patch a value inside all list items:
<field>.[].<subfield>=<value>
Example:
spec.template.containers.[].image="mycontainer:latest"
\b
When calling this tool from a command line, it's higly recommended that you quote all patches arguments to avoid terminal issues.
Example:
yaml-patch -f test.yml 'spec.template.containers.[0].image="mycontainer:latest"'
yaml-patch -f test.yml 'spec.template.containers.[].image="mycontainer:latest"'
""",
)
@click.option(
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ line-length = 120

[tool.poetry]
name = "yaml-patch"
version = "0.1.0"
version = "0.1.1"
description = "Patch yaml strings"
readme = "README.md"
authors = ["Diogo de Campos <[email protected]>"]
license = "MIT"

Expand Down

0 comments on commit a178fe6

Please sign in to comment.