[util/cli] Add option to ignore return codes
Add an option that ignores the return code of an execute, instead of throwing an error (which sometimes causes ugly try/except blocks in client code)
This commit is contained in:
parent
7a1cd4b613
commit
30c539f1f2
2 changed files with 8 additions and 2 deletions
|
@ -26,6 +26,12 @@ class cli(unittest.TestCase):
|
|||
with self.assertRaises(RuntimeError):
|
||||
util.cli.execute('cat {}'.format(self.nonExistentCommand))
|
||||
|
||||
def test_command_exit_code_no_error(self):
|
||||
try:
|
||||
util.cli.execute('cat {}'.format(self.nonExistentCommand), ignore_errors=True)
|
||||
except Exception:
|
||||
self.fail('exception was thrown')
|
||||
|
||||
def test_async(self):
|
||||
rv = util.cli.execute(self.validCommand, wait=False)
|
||||
self.assertEqual('', rv)
|
||||
|
|
|
@ -2,7 +2,7 @@ import shlex
|
|||
import subprocess
|
||||
import logging
|
||||
|
||||
def execute(cmd, wait=True):
|
||||
def execute(cmd, wait=True, ignore_errors=False):
|
||||
args = shlex.split(cmd)
|
||||
logging.debug(cmd)
|
||||
try:
|
||||
|
@ -12,7 +12,7 @@ def execute(cmd, wait=True):
|
|||
|
||||
if wait:
|
||||
out, _ = proc.communicate()
|
||||
if proc.returncode != 0:
|
||||
if proc.returncode != 0 and not ignore_errors:
|
||||
raise RuntimeError('{} exited with {}'.format(cmd, proc.returncode))
|
||||
return out.decode('utf-8')
|
||||
return ''
|
||||
|
|
Loading…
Reference in a new issue