Fix error handling & add custom info printer

This commit is contained in:
askiiart 2022-12-15 17:39:32 -06:00
parent 1e05dd2cd6
commit 43af3125f1

View file

@ -1,13 +1,41 @@
from docker_wrapper import Docker from docker_wrapper import Docker, NoContainersError
from pprint import pprint from subprocess import getoutput
def container_to_str(container):
"""
Returns info about a Docker container as a string
:parameters:f
container: The name of the container (str)
:returns:
str: The container info as a string, formatted for printing
"""
# Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces)
info = Docker.container_info(container)
info_str = f'{container}\n'
info_str += f' CONTAINER ID: {info["CONTAINER ID"]}\n'
info_str += f' IMAGE: {info["IMAGE"]}\n'
info_str += f' COMMAND: {info["COMMAND"]}\n'
info_str += f' CREATED: {info["CREATED"]}\n'
info_str += f' STATUS: {info["STATUS"]}\n'
info_str += f' PORTS: {info["PORTS"]}\n'
info_str += f' NAMES: {info["NAMES"]}'
return info_str
try:
while True:
if not Docker.containers_exist():
raise(NoContainersError('No containers exist! Please create a container and try again.'))
while True:
# Main Menu # Main Menu
print('Select the container to manage:') print('Select the container to manage:')
containers = Docker.containers() containers = Docker.containers()
for i in range(len(containers)): for i in range(len(containers)):
print(f' {i} - {containers[i]}') print(f' {i} - {containers[i]}')
print(' q - quit') print(' q - Quit')
container_i = input() container_i = input()
print() print()
@ -16,6 +44,8 @@ while True:
exit(0) exit(0)
while True: while True:
if not Docker.containers_exist():
raise(NoContainersError('No containers exist! Please create a container and try again.'))
# Container Menu # Container Menu
container = containers[int(container_i)] container = containers[int(container_i)]
print() print()
@ -33,31 +63,35 @@ while True:
break break
elif selection == 'view': elif selection == 'view':
pprint(Docker.container_info(container)) # TODO: Make better, custom printer print(container_to_str(container))
elif selection == 'start': elif selection == 'start':
print('Starting...') print('Starting...')
status = Docker.start(container) Docker.start(container)
elif selection == 'stop': elif selection == 'stop':
print('Stopping...') print('Stopping...')
status = Docker.stop(container) Docker.stop(container)
print('Done.') print('Done')
elif selection == 'rm': elif selection == 'rm':
break_later = False break_later = False
while selection != 'y' and selection != 'n' and selection != 'Y' and selection != 'N': while selection != 'y' and selection != 'n' and selection != 'Y' and selection != 'N':
print(f'WARNING! This will DELETE {container}!') print(f'WARNING! This will DELETE {container}!')
print(f'Are you absolutely sure you want to delete {container}? (y/N)') print(
f'Are you absolutely sure you want to delete {container}? (y/N)')
selection = input() selection = input()
if selection == 'y' or selection == 'Y': if selection == 'y' or selection == 'Y':
print('Stopping...')
Docker.stop(container)
print('Deleting...') print('Deleting...')
status = Docker.rm(container) Docker.rm(container)
print('Done') print('Done')
break_later = True break_later = True
elif selection == 'n' or selection == 'N': elif selection == 'n' or selection == 'N':
print(f'Operation cancelled, returning to {container} menu...') print(f'Operation cancelled, returning to {container} menu...')
if break_later: if break_later:
print()
break break
elif selection == 'menu': elif selection == 'menu':
@ -67,3 +101,7 @@ while True:
else: else:
print('Selection invalid. Please try again.') print('Selection invalid. Please try again.')
print() print()
except NoContainersError as e:
print('Error:', e)
print('No containers found. Exiting...')
exit()