From 8b11a4fa6126c8a3fa70028efaabfc7bbc6c25f0 Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 13 Jul 2023 11:07:59 +0200 Subject: [PATCH] Add execute_handled --- .../infrastructure/infrastructure.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/python/ddadevops/infrastructure/infrastructure.py b/src/main/python/ddadevops/infrastructure/infrastructure.py index 2d375da..8b66ede 100644 --- a/src/main/python/ddadevops/infrastructure/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure/infrastructure.py @@ -1,4 +1,4 @@ -from subprocess import Popen, PIPE, run +from subprocess import Popen, PIPE, run, CalledProcessError from pathlib import Path from sys import stdout from os import chmod, environ @@ -101,7 +101,7 @@ class ExecutionApi: output = output.rstrip() return output - def execute_live(self, command, dry_run=False, shell=True): + def execute_live(self, command: str, dry_run=False, shell=True): if dry_run: print(command) else: @@ -111,6 +111,21 @@ class ExecutionApi: process.stdout.close() process.wait() + def execute_handled(self, command: str, dry_run=False, shell=True, check=True): + if dry_run: + print(command) + else: + try: + run( + command, + shell=shell, + check=check, + stderr=PIPE, + text=True) + except CalledProcessError as exc: + print("Command failed with code: ", exc.returncode, " and message:", exc.stderr) + raise exc + class EnvironmentApi: def get(self, key):