Skip to content

Commit be27517

Browse files
committed
cmd-diff: support cd strategy for diff_cmd_outputs
This strategy simply changes directory into the given path before running the provided command rather than replacing a templated `{}` with the path. Useful for commands that operate more cleanly when operated on in the directory where you want the operation to occur.
1 parent 956ec8b commit be27517

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/cmd-diff

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class OSTreeImport(IntEnum):
3737
FULL = 3
3838

3939

40+
class DiffCmdOutputStrategy(IntEnum):
41+
CD = 1
42+
TEMPLATE = 2
43+
44+
4045
@dataclass
4146
class Differ:
4247
name: str
@@ -401,16 +406,20 @@ def diff_metal(diff_from, diff_to):
401406
shutdown_process(p_to)
402407

403408

404-
def diff_cmd_outputs(cmd, path_from, path_to):
409+
def diff_cmd_outputs(cmd, path_from, path_to, strategy: DiffCmdOutputStrategy = DiffCmdOutputStrategy.TEMPLATE):
410+
workingdir = None
405411
with tempfile.NamedTemporaryFile(prefix=cmd[0] + '-') as from_output, \
406412
tempfile.NamedTemporaryFile(prefix=cmd[0] + '-') as to_output:
407413
for path, output in (path_from, from_output), (path_to, to_output):
408414
c = list(cmd)
409-
if '{}' not in c:
410-
c += ['{}']
411-
idx = c.index('{}')
412-
c[idx] = path
413-
subprocess.run(c, check=True, stdout=output).stdout
415+
if strategy == DiffCmdOutputStrategy.TEMPLATE:
416+
if '{}' not in c:
417+
c += ['{}']
418+
idx = c.index('{}')
419+
c[idx] = path
420+
elif strategy == DiffCmdOutputStrategy.CD:
421+
workingdir = path
422+
subprocess.run(c, cwd=workingdir, check=True, stdout=output).stdout
414423
git_diff(from_output.name, to_output.name)
415424

416425

0 commit comments

Comments
 (0)