Added input handling and first utilities

This commit is contained in:
Neocky
2022-06-07 22:40:27 +02:00
parent 8e51b1976d
commit ac139ed048
3 changed files with 184 additions and 7 deletions

View File

@@ -0,0 +1,94 @@
""""
Handles the input through the pluGET command line
"""
from src.utils.utilities import rich_print_error
# check
# update
# get
# get-paper
# get-purpur
# get-airplane
# exit
# remove
# search ???
def handle_input() -> None:
"""
Manages the correct function calling from the given input
"""
while True:
try:
input_command, input_selected_object, input_parameter = get_input()
except TypeError:
# KeyboardInterrupt was triggered and None was returned so exit
return
match input_command:
case "get":
match input_selected_object.isdigit():
case True:
print("get specific package")
#getSpecificPackage(inputSelectedObject, pluginPath, inputParams)
case _:
print("get search specific package")
#searchPackage(inputSelectedObject)
case "update":
print("update package")
match input_selected_object:
case "serverjar":
print("update serverjar")
#updateServerjar(inputParams)
case _:
print("update package")
#updateInstalledPackage(inputSelectedObject)
case "check":
print("check package")
match input_selected_object:
case "serverjar":
print("check serverjar")
#checkInstalledServerjar()
case _:
print("check plugins")
#checkInstalledPackage(inputSelectedObject, inputParams)
case "search":
print("search package")
#searchPackage(inputSelectedObject)
case "remove":
print("remove package")
#removePlugin(inputSelectedObject)
case "get-paper":
# download papermc
print("download papermc")
#papermc_downloader(inputSelectedObject, inputParams)
case _:
rich_print_error("Error: Command not found. Please try again. :(")
rich_print_error("Use: 'help command' to get all available commands")
def get_input() -> None:
"""
Gets command line input and calls the handle input function
"""
input_command = None
while True:
try:
input_command, input_selected_object, *input_parameter = input("pluGET >> ").split()
break
except ValueError:
if input_command == None:
# request input again if no input was given or not enough
continue
else:
rich_print_error("Wrong input! Use: > 'command' 'selectedObject' [optionalParams]")
rich_print_error("Use: 'help command' to get all available commands")
except KeyboardInterrupt:
return
input_parameter = input_parameter[0] if input_parameter else None
return input_command, input_selected_object, input_parameter

80
src/utils/utilities.py Normal file
View File

@@ -0,0 +1,80 @@
"""
Holds all the utilitie code for pluGET and the webrequests function
"""
import os
import sys
import requests
import shutil
from pathlib import Path
from rich.console import Console
def api_do_request(url) -> list:
"""
Handles the webrequest and returns a json list
"""
webrequest_header = {'user-agent': 'pluGET/1.0'}
try:
response = requests.get(url, headers=webrequest_header)
except:
print("Couldn't create webrequest")
return
api_json_data = response.json()
return api_json_data
def api_test_spiget() -> None:
"""
Test if the Spiget api sends a 200 status code back
"""
try:
r = requests.get('https://api.spiget.org/v2/status')
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError):
rich_print_error("Error: Couldn't make a connection to the API. Check you connection to the internet!", style="bright_red")
rich_print_error("Press any key + enter to exit...")
sys.exit()
if r.status_code != 200:
rich_print_error("Error: Problems with the API detected. Plese try it again later!")
rich_print_error("Press any key + enter to exit...")
sys.exit()
return
def rich_print_error(error_message) -> None:
"""
Prints a formatted error message from rich
"""
console = Console()
console.print(error_message, style="bright_red")
return
def create_temp_plugin_folder() -> Path:
"""
Creates a temporary folder to store plugins inside
Returns full path of temporary folder
"""
path_temp_plugin_folder = Path("./TempSFTPFolder")
if os.path.isdir(path_temp_plugin_folder):
return path_temp_plugin_folder
try:
os.mkdir(path_temp_plugin_folder)
except OSError:
rich_print_error(f"Error: Creation of directory {path_temp_plugin_folder} failed")
rich_print_error("Please check for missing permissions in folder tree!")
input("Press any key + enter to exit...")
sys.exit()
return path_temp_plugin_folder
def remove_temp_plugin_folder() -> None:
"""
Removes the temporary plugin folder and all content inside it
"""
try:
shutil.rmtree(Path("./TempSFTPFolder"))
except OSError as e:
rich_print_error(f"Error: {e.filename} - {e.strerror}")
return