Skip to content

Commit

Permalink
Correctly capturing return codes from executed programs (#2)
Browse files Browse the repository at this point in the history
* Extending ability to capture errors

* Pass the env and set Shell to False

* Explicilty set env

* Correcting name of function

* Correctly capturing and reporting return code

* Removing unnecessary logging

* Bumping Version

* Noting return codes in the README.md

* Updating description.
  • Loading branch information
s7clarke10 authored Oct 7, 2023
1 parent f91ec4c commit 2ddc059
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Optional: Use existing env variables if they exist rather than incoming SSM Para
## exec switch
Calls a sub-process to execute the given program. Chamber expects that the given program call is available in the PATH or is a fully qualified location to the executable.

Note: Chamber will capture the return result from the program which is executed and will return the result of the execution back to the shell.

# Credits

The following people / projects are credited for pychamber project.
Expand Down
5 changes: 3 additions & 2 deletions pychamber/chamber.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import subprocess

from pychamber.utils.execution_utils import run_command
from pychamber.utils.ssm_parameter_store import SSMParameterStore
from pychamber.utils.manage_args import (
parse_exec,
Expand Down Expand Up @@ -47,7 +47,8 @@ def main():
my_env[param] = store[ssm_param]

my_command = args.exec
subprocess.run(my_command, env = my_env)
result = run_command(my_command, env = my_env)
return result

# run the main function only if this module is executed as the
# main script. (if you import this as a module then nothing is executed)
Expand Down
13 changes: 13 additions & 0 deletions pychamber/utils/execution_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Utilities for pychamber."""

import logging
import subprocess

def run_command(command, env):
""" Runs requested process with arguments.
Return: returncode of executed program.
"""
logging.debug("Command: {}".format(command))
result = subprocess.run(command, env=env, shell=False, capture_output=False)

return result.returncode
2 changes: 1 addition & 1 deletion pychamber/utils/manage_args.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# manage_args.py
"""Process cli arguments via argparse with extensions."""

import argparse
from enum import Enum
Expand Down
2 changes: 2 additions & 0 deletions pychamber/utils/ssm_parameter_store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Interact with AWS Parameter Store for secrets via boto3."""

# Copyright (c) 2018 Bao Nguyen <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pychamber"
version = "0.1.1"
version = "0.1.3"
description = ""
authors = ["Steve Clarke <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 2ddc059

Please sign in to comment.