Add cli.py entrypoint

This commit is contained in:
trueold89 2024-07-07 15:10:42 +03:00
parent 13c233b6a5
commit f249cd95f0
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
1 changed files with 59 additions and 0 deletions

59
mcaio/cli.py Normal file
View File

@ -0,0 +1,59 @@
from mcaio.client import AIOMCServer
from os import environ
from sys import argv
from asyncio import run as aiorun
def get_env(key: str) -> str:
val = environ.get(key)
if val is None:
raise ValueError
return val
def arg() -> str:
if len(argv) != 2:
raise RuntimeError
return argv[1]
async def get_name(server: AIOMCServer) -> str:
return await server.name
async def get_count(server: AIOMCServer) -> int:
return await server.players_count
async def get_max(server: AIOMCServer) -> int:
return await server.players_count
async def action() -> None:
try:
HOST, PORT = get_env("MC_HOST"), int(get_env("MC_PORT"))
server = AIOMCServer(HOST, PORT)
match arg():
case 'name':
out = get_name(server)
case 'pmax':
out = get_max(server)
case 'pcount':
out = get_count(server)
case _:
raise RuntimeError
print(await out)
except ValueError or TypeError:
print("ERROR: BAD ENV VARS")
except RuntimeError:
print("Bad args")
except ConnectionRefusedError:
print("Connection error")
def main():
aiorun(action())
if __name__ == "__main__":
main()