Skip to content

Commit a178fe6

Browse files
committed
Added README
1 parent 4c09642 commit a178fe6

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
1-
# yaml-patch
1+
# yaml-patch
2+
3+
Apply patches to a yaml string, keeping most of the formatting and comments.
4+
5+
Some formatting is not kept due to underlying yaml library limitations:
6+
- Indentation will be forced to two spaces
7+
- Spacing before sequence dashes will be forced to two spaces
8+
- Empty lines at the start of the string will be removed
9+
10+
## As a command line tool
11+
12+
You can pass any number of patches to be applied, they use the following syntax options:
13+
14+
### Patch a single value:
15+
`<field>.<subfield>=<value>`
16+
17+
Example:
18+
```bash
19+
yaml-patch -f test.yml 'spec.replicas=2'
20+
```
21+
22+
### Patch a value inside a single list item:
23+
`<field>.[<position]>.<subfield>=<value>`
24+
25+
Example:
26+
```bash
27+
yaml-patch -f test.yml 'spec.template.containers.[0].image="mycontainer:latest"'
28+
```
29+
30+
### Patch a value inside all list items:
31+
`<field>.[].<subfield>=<value>`
32+
33+
Example:
34+
```bash
35+
yaml-patch -f test.yml 'spec.template.containers.[].image="mycontainer:latest"'
36+
```
37+
38+
## As a Python library
39+
40+
To use `yaml-patch` as a library just import the function and pass patches as dictionary entries.
41+
42+
Example:
43+
44+
```python
45+
from yaml_patch import patch
46+
from textwrap import dedent
47+
48+
def override_list_all_values():
49+
source_yaml = dedent(
50+
"""\
51+
some_list:
52+
- alice
53+
- bob
54+
"""
55+
)
56+
patches = {"some_list.[]": "charlie"}
57+
expected_yaml = dedent(
58+
"""\
59+
some_list:
60+
- charlie
61+
- charlie
62+
"""
63+
)
64+
assert patch(source_yaml, patches) == expected_yaml
65+
```

main.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@click.command(
1010
help="""
11-
Applies patches to a yaml string, keeping most of the formatting and comments.
11+
Apply patches to a yaml string, keeping most of the formatting and comments.
1212
1313
\b
1414
Some formatting is not kept due to underlying yaml library limitations:
@@ -22,24 +22,19 @@
2222
Patch a single value:
2323
<field>.<subfield>=<value>
2424
Example:
25-
spec.replicas=2
25+
yaml-patch -f test.yml 'spec.replicas=2'
2626
2727
\b
2828
Patch a value inside a single list item:
2929
<field>.[<position]>.<subfield>=<value>
3030
Example:
31-
spec.template.containers.[0].image="mycontainer:latest"
31+
yaml-patch -f test.yml 'spec.template.containers.[0].image="mycontainer:latest"'
3232
3333
\b
3434
Patch a value inside all list items:
3535
<field>.[].<subfield>=<value>
3636
Example:
37-
spec.template.containers.[].image="mycontainer:latest"
38-
39-
\b
40-
When calling this tool from a command line, it's higly recommended that you quote all patches arguments to avoid terminal issues.
41-
Example:
42-
yaml-patch -f test.yml 'spec.template.containers.[0].image="mycontainer:latest"'
37+
yaml-patch -f test.yml 'spec.template.containers.[].image="mycontainer:latest"'
4338
""",
4439
)
4540
@click.option(

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ line-length = 120
33

44
[tool.poetry]
55
name = "yaml-patch"
6-
version = "0.1.0"
6+
version = "0.1.1"
77
description = "Patch yaml strings"
8+
readme = "README.md"
89
authors = ["Diogo de Campos <campos.ddc@gmail.com>"]
910
license = "MIT"
1011

0 commit comments

Comments
 (0)