Skip to content

Commit

Permalink
Added --in-place option
Browse files Browse the repository at this point in the history
  • Loading branch information
campos-ddc committed Aug 23, 2021
1 parent 2edaf94 commit b9e188e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ line-length = 120

[tool.poetry]
name = "yaml-patch"
version = "0.1.3"
version = "0.2.0"
description = "Patch yaml strings"
readme = "README.md"
authors = ["Diogo de Campos <[email protected]>"]
Expand Down
14 changes: 12 additions & 2 deletions yaml_patch/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@
@click.option(
"--output", "-o", type=click.File("w"), default=sys.stdout, help="Path to output patched file. Defaults to stdout."
)
@click.option(
"--in-place", "-i", is_flag=True, help="Replace source file in-place. Overrides --output."
)
@click.argument("patches", nargs=-1)
def cli(file, output, patches):
def cli(file, output, in_place, patches):
# Split each patch into key+value separated by `=`. Use YAML to load the values coming from command line to ensure
# they are parsed into yaml syntax equivalents (automatically detect strings, ints, bools, etc).
yaml = YAML()
Expand All @@ -53,7 +56,14 @@ def cli(file, output, patches):
k, v = p.split("=")
dict_patches[k] = yaml.load(v)

patched = patch(file.read(), dict_patches)
with file:
patched = patch(file.read(), dict_patches)

if in_place:
if file == sys.stdin:
raise RuntimeError("Cannot use --in-place with stdin as the source")
output = open(file.name, 'w')

output.write(patched)


Expand Down

0 comments on commit b9e188e

Please sign in to comment.