show: relay cli-pretty's user-facing error messages

cli-pretty prints a friendly message on stdout before any sys.exit(1)
— 'Interface "w" not found', 'Error, top level "ietf-routing:routing"
missing', etc.  The wrapper used subprocess.run(..., check=True) and
on non-zero exit caught CalledProcessError, throwing away the captured
stdout and printing the generic exception message instead.

Drop the check=True / try-except dance, always relay stdout, and only
fall back to the generic 'Error running cli-pretty' line when the
subprocess crashed without producing any output.

Before:
  admin@bpi:/> show interface w
  Error running cli-pretty: Command '['/usr/libexec/statd/cli-pretty',
  'show-interfaces', '-n', 'w']' returned non-zero exit status 1.

After:
  admin@bpi:/> show interface w
  Interface "w" not found

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2026-05-27 20:53:31 +02:00
parent 65eb4c072a
commit 6958c9bb1a
+12 -7
View File
@@ -40,15 +40,20 @@ def cli_pretty(json_data: dict, command: str, *args: str):
return
safe_args = [shlex.quote(arg) for arg in args]
json_input = json.dumps(json_data)
result = subprocess.run([
"/usr/libexec/statd/cli-pretty", command, *safe_args
], input=json_input, capture_output=True, text=True)
try:
json_input = json.dumps(json_data) # Keep as string, not bytes
result = subprocess.run([
"/usr/libexec/statd/cli-pretty", command, *safe_args
], input=json_input, capture_output=True, text=True, check=True)
# cli-pretty prints a user-facing message on stdout before any
# sys.exit(1) (e.g. 'Interface "w" not found'). Relay it regardless
# of the exit status, and only surface the generic exec error when
# nothing useful was produced.
if result.stdout:
print(result.stdout, end="")
except subprocess.CalledProcessError as e:
print(f"Error running cli-pretty: {e}")
elif result.returncode != 0:
msg = result.stderr.strip() or f"exit status {result.returncode}"
print(f"Error running cli-pretty: {msg}")
def dhcp(args: List[str]) -> None: