Skip to content

Commit

Permalink
add status command
Browse files Browse the repository at this point in the history
  • Loading branch information
rootiest committed Jun 13, 2023
1 parent 2a3e4d3 commit b86b90f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/printcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,39 @@ def remove_printcfg():
logger.info("Printcfg removed successfully.")
sys.exit(0)

def show_status():
'''Show the status of the service.'''
# Get the systemctl status
command = ["systemctl", "status", f"{REPO}.service"]
logger.debug("Executing command: %s", command)
try:
result = subprocess.run(command, capture_output=True, check=False)
except subprocess.CalledProcessError as err:
print("Error: The subprocess returned an error.")
print(err.stderr)
logger.error("Error: The subprocess returned an error: %s", err.stderr)
return
logger.debug("Command output: %s", result.stdout.decode("utf-8"))
if result.returncode != 0:
print(f"Error: The command '{command}' failed with code {result.returncode}.")
logger.error("Error: The command '%s' failed with code %s.", command, result.returncode)
print(f"Output from command: {result.stdout.decode('utf-8')}")
print(f"Error from command: {result.stderr.decode('utf-8')}")
return
# Print the status
print(result.stdout.decode("utf-8"))
# Exit successfully
logger.info("Status shown successfully.")
sys.exit(0)

def show_help():
'''Show the help message.'''
print(f"Usage: {sys.argv[0]} [install|change|remove|update|help]")
print(f" install: Install {REPO} service")
print(" change: Change the current profile")
print(f" remove: Remove {REPO} service")
print(f" update: Update {REPO}")
print(" status: Show the status of the service")
print(" help: Show this help message")
sys.exit(0)

Expand Down Expand Up @@ -276,6 +302,9 @@ def show_help():
elif sys.argv[1] == "update":
logger.info("Running update operations.")
update_printcfg()
elif sys.argv[1] == "status":
logger.info("Running status operations.")
show_status()
elif sys.argv[1] == "help":
logger.info("Running help operations.")
show_help()
Expand Down

0 comments on commit b86b90f

Please sign in to comment.