From 34f871ae75e876f508f49b71ee0971de66717b90 Mon Sep 17 00:00:00 2001 From: Ruben van Staveren Date: Fri, 11 Mar 2022 20:42:19 +0100 Subject: [PATCH] Simplify code by letting run() generate exception on non zero exit status --- jail2ban/pfctl.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/jail2ban/pfctl.py b/jail2ban/pfctl.py index 24f4448..0e062c5 100644 --- a/jail2ban/pfctl.py +++ b/jail2ban/pfctl.py @@ -9,12 +9,10 @@ def pfctl_cfg_read(anchor): cmd = [_SUDO, _PFCTL, '-a', anchor, '-sr'] logging.info('Running %s', cmd) - res = run(cmd, capture_output=True) + res = run(cmd, capture_output=True, check=True) - if res and res.stdout: - logging.info('Result: %s', res) - res.check_returncode() - return res.stdout.splitlines() + logging.info('Result: %s', res) + return res.stdout.splitlines() def pfctl_cfg_write(anchor, cfg): @@ -24,12 +22,11 @@ def pfctl_cfg_write(anchor, cfg): res = run(cmd, input=cfg, + check=True, capture_output=True) - if res: - logging.info('Result: %s', res) - res.check_returncode() - return res.stdout.splitlines() + logging.info('Result: %s', res) + return res.stdout.splitlines() def pfctl_table_op(anchor, **kwargs): @@ -40,9 +37,9 @@ def pfctl_table_op(anchor, **kwargs): logging.info('Running %s', cmd) - res = run([x for x in cmd if x is not None], capture_output=True) + res = run([x for x in cmd if x is not None], + capture_output=True, + check=True) - if res: - logging.debug(res) - res.check_returncode() - return res.stdout.splitlines() + logging.debug(res) + return res.stdout.splitlines()