Skip to content

Commit e1f5794

Browse files
committed
Added --diff option to CLI
1 parent 8b638a7 commit e1f5794

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

pyinfra/api/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ConfigDefaults:
5353
IGNORE_ERRORS: bool = False
5454
# Shell to use to execute commands
5555
SHELL: str = "sh"
56+
DIFF: bool = False
5657

5758

5859
config_defaults = {key: value for key, value in ConfigDefaults.__dict__.items() if key.isupper()}

pyinfra/operations/files.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,21 +1033,22 @@ def put(
10331033
# File exists, check sum and check user/group/mode/atime/mtime if supplied
10341034
else:
10351035
if not _file_equal(local_sum_path, dest):
1036-
# Generate diff when contents change
1037-
current_contents = host.get_fact(FileContents, path=dest)
1038-
if current_contents:
1039-
current_lines = [line + "\n" for line in current_contents]
1040-
else:
1041-
current_lines = []
1036+
if state.config.DIFF:
1037+
# Generate diff when contents change
1038+
current_contents = host.get_fact(FileContents, path=dest)
1039+
if current_contents:
1040+
current_lines = [line + "\n" for line in current_contents]
1041+
else:
1042+
current_lines = []
10421043

1043-
logger.info(f"\n Will modify {click.style(dest, bold=True)}")
1044+
logger.info(f"\n Will modify {click.style(dest, bold=True)}")
10441045

1045-
with get_file_io(src, "r") as f:
1046-
desired_lines = f.readlines()
1046+
with get_file_io(src, "r") as f:
1047+
desired_lines = f.readlines()
10471048

1048-
for line in generate_color_diff(current_lines, desired_lines):
1049-
logger.info(f" {line}")
1050-
logger.info("")
1049+
for line in generate_color_diff(current_lines, desired_lines):
1050+
logger.info(f" {line}")
1051+
logger.info("")
10511052

10521053
yield FileUploadCommand(
10531054
local_file,

pyinfra_cli/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ def _print_support(ctx, param, value):
6767
default=False,
6868
help="Don't execute operations on the target hosts.",
6969
)
70+
@click.option(
71+
"--diff",
72+
is_flag=True,
73+
default=False,
74+
help="Show the differences when changing text files and templates.",
75+
)
7076
@click.option(
7177
"-y",
7278
"--yes",
@@ -267,6 +273,7 @@ def _main(
267273
group_data,
268274
config_filename: str,
269275
dry: bool,
276+
diff: bool,
270277
yes: bool,
271278
limit: Iterable,
272279
no_wait: bool,
@@ -310,6 +317,7 @@ def _main(
310317
shell_executable,
311318
fail_percent,
312319
yes,
320+
diff,
313321
)
314322
override_data = _set_override_data(
315323
data,
@@ -549,6 +557,7 @@ def _set_config(
549557
shell_executable,
550558
fail_percent,
551559
yes,
560+
diff,
552561
):
553562
logger.info("--> Loading config...")
554563

@@ -583,6 +592,9 @@ def _set_config(
583592
if fail_percent is not None:
584593
config.FAIL_PERCENT = fail_percent
585594

595+
if diff:
596+
config.DIFF = True
597+
586598
return config
587599

588600

0 commit comments

Comments
 (0)