Remove if-"hell" from run function

This commit is contained in:
Marcel Schwarz 2020-01-20 22:42:01 +01:00
parent e573118b38
commit 4177fe2a5f

View File

@ -11,35 +11,30 @@ class SwapModule(AbstractModule):
def run(self):
actions = ["Create/Resize temp. swap", "Create/Resize persistent swap", "Delete swap",
"Make temp. swap persistent", "Get swap location", "Get swap size",
"Show swapiness", "Adjust temp. swapiness"]
actions = {
"Create/Resize temp. swap": self.create_resize_swap,
"Create/Resize persistent swap": self.create_resize_peristent_swap,
"Delete swap": self.delete_swap,
"Make temp. swap persistent": self.make_swap_persistent,
"Get swap location": self.swap_location_check,
"Get swap size": self.get_swap_size,
"Show swapiness": self.show_swapiness,
"Adjust temp. swapiness": self.adjust_swapiness_temp
}
menu = [
{
'type': 'list',
'message': 'Select action',
'name': 'action',
'choices': list(map(lambda x: {"name": x}, actions))
'choices': list(map(lambda x: {"name": x}, actions.keys()))
}
]
selected_action = prompt(menu)['action']
if selected_action == "Create/Resize temp. swap":
self.create_resize_swap()
elif selected_action == "Create/Resize persistent swap":
self.create_resize_swap()
self.make_swap_persistent()
elif selected_action == "Make temp. swap persistent":
self.make_swap_persistent()
elif selected_action == "Show swapiness":
self.show_swapiness()
elif selected_action == "Delete":
self.delete_swap()
elif selected_action == "Get swap location":
self.swap_location_check()
elif selected_action == "Get swap size":
self.get_swap_size()
elif selected_action == "Adjust temp. swapiness":
self.adjust_swapiness_temp()
actions[selected_action]()
def create_resize_peristent_swap(self):
self.create_resize_swap()
self.make_swap_persistent()
def swap_location_check(self) -> None:
output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8")