Skip to content

Commit 43d8503

Browse files
committed
refactor(shellflow): simplify output handling and improve verbosity logic
1 parent 9880066 commit 43d8503

1 file changed

Lines changed: 40 additions & 27 deletions

File tree

src/shellflow.py

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -849,15 +849,15 @@ def _execute_single_command(
849849
stdout = result.stdout.strip() if result.stdout else ""
850850
stderr = result.stderr.strip() if result.stderr else ""
851851
combined = _combine_output(stdout, stderr)
852-
return combined, result.returncode, stdout, stderr
853-
854852
except subprocess.TimeoutExpired as e:
855853
stdout = _stringify_subprocess_stream(e.output).strip()
856854
stderr = _stringify_subprocess_stream(e.stderr).strip()
857855
combined = _combine_output(stdout, stderr)
858856
return combined, -1, stdout, stderr
859857
except (OSError, subprocess.SubprocessError) as e:
860858
return str(e), -1, "", str(e)
859+
else:
860+
return combined, result.returncode, stdout, stderr
861861

862862

863863
def execute_local(
@@ -1289,8 +1289,8 @@ def _execute_block_commands_sequential(
12891289
context: ExecutionContext,
12901290
no_input: bool,
12911291
verbose: bool,
1292-
_block_index: int, # Unused but kept for API consistency
1293-
_total_blocks: int, # Unused but kept for API consistency
1292+
block_index: int,
1293+
total_blocks: int,
12941294
) -> ExecutionResult:
12951295
"""Execute block commands sequentially, printing output after each command.
12961296
@@ -1300,55 +1300,58 @@ def _execute_block_commands_sequential(
13001300
# ANSI color codes
13011301
RED = "\033[91m"
13021302
DIM = "\033[90m"
1303+
BLUE = "\033[94m"
1304+
YELLOW = "\033[93m"
13031305
RESET = "\033[0m"
13041306

1307+
# Print block header if verbose
1308+
if verbose:
1309+
if block.is_local:
1310+
print(f"{BLUE}[{block_index}/{total_blocks}] LOCAL{RESET}")
1311+
else:
1312+
host = block.host or "unknown"
1313+
print(f"{YELLOW}[{block_index}/{total_blocks}] REMOTE: {host}{RESET}")
1314+
13051315
all_outputs: list[str] = []
13061316
all_stdout: list[str] = []
13071317
all_stderr: list[str] = []
13081318
final_exit_code = 0
13091319
success = True
13101320

13111321
commands_to_execute = _iter_display_commands(block.commands)
1312-
ssh_config = None
1313-
if block.is_remote:
1314-
host = block.host
1315-
if host:
1316-
ssh_config = read_ssh_config(host)
13171322

13181323
for cmd in commands_to_execute:
13191324
# Print the command being executed
13201325
if verbose:
13211326
print(f"{DIM}$ {cmd}{RESET}")
13221327

1323-
# Execute single command
1324-
output, exit_code, stdout, stderr = _execute_single_command(
1325-
command=cmd,
1326-
context=context,
1328+
# Execute single command using a single-command block
1329+
single_block = Block(
1330+
target=block.target,
1331+
commands=[cmd],
13271332
shell=block.shell,
1328-
no_input=no_input,
1329-
is_remote=block.is_remote,
1330-
host=block.host,
1331-
ssh_config=ssh_config,
1333+
timeout_seconds=block.timeout_seconds,
13321334
)
1335+
result = _execute_block_once(single_block, context, no_input=no_input)
13331336

13341337
# Store outputs
1335-
all_outputs.append(output)
1336-
if stdout:
1337-
all_stdout.append(stdout)
1338-
if stderr:
1339-
all_stderr.append(stderr)
1338+
all_outputs.append(result.output)
1339+
if result.stdout:
1340+
all_stdout.append(result.stdout)
1341+
if result.stderr:
1342+
all_stderr.append(result.stderr)
13401343

13411344
# Print output immediately with truncation
1342-
if verbose and output:
1343-
truncated = _truncate_output_lines(output, MAX_OUTPUT_LINES)
1345+
if verbose and result.output:
1346+
truncated = _truncate_output_lines(result.output, MAX_OUTPUT_LINES)
13441347
print(truncated)
13451348

13461349
# Check for failure
1347-
if exit_code != 0:
1348-
final_exit_code = exit_code
1350+
if not result.success:
1351+
final_exit_code = result.exit_code
13491352
success = False
13501353
if verbose:
1351-
print(f"{RED}✗ Command failed with exit code {exit_code}{RESET}\n")
1354+
print(f"{RED}✗ Command failed with exit code {result.exit_code}{RESET}\n")
13521355
break
13531356

13541357
combined_output = "\n".join(filter(None, all_outputs))
@@ -1440,6 +1443,10 @@ def run_script( # noqa: PLR0915
14401443

14411444
# Execute the block
14421445
if sequential_output and verbose:
1446+
# Print context before executing
1447+
for env_line in _iter_display_context(context):
1448+
print(f"{DIM}@env {env_line}{RESET}")
1449+
14431450
# Use sequential execution with per-command output
14441451
attempt_count = 0
14451452
max_attempts = block.retry_count + 1
@@ -1457,6 +1464,12 @@ def run_script( # noqa: PLR0915
14571464
if verbose:
14581465
print(f"{YELLOW}↻ Retrying attempt {attempt_count + 1}/{max_attempts}{RESET}")
14591466

1467+
# Print success/failure status
1468+
if result.success:
1469+
print(f"{GREEN}✓ Success{RESET}\n")
1470+
else:
1471+
print(f"{RED}✗ Failed: {result.error_message}{RESET}\n")
1472+
14601473
block_results.append(result)
14611474
events.append(_make_block_finished_event(run_id, result, block, total_blocks))
14621475
blocks_executed += 1

0 commit comments

Comments
 (0)