uncia/uncia/manage.py

85 lines
1.8 KiB
Python

import sys
from izzylib import logging
from . import __version__
from .database import db
exe = f'{sys.executable} -m uncia.manage'
forbidden_keys = ['pubkey', 'privkey', 'version']
class Command:
def __init__(self, arguments):
try:
cmd = arguments[0]
except IndexError:
self.result = self.cmd_help()
return
args = arguments[1:] if len(arguments) > 1 else []
self.result = self[cmd](*args)
def __getitem__(self, key):
try:
return getattr(self, f'cmd_{key}')
except AttributeError:
raise InvalidCommandError(f'Not a valid command: {key}')
def cmd_help(self):
return f'''Uncia Relay Management v{__version__}
python3 -m uncia.manage config [key] [value]:
Gets or sets the config. Specify a key and value to set a config option.
Only specify a key to get the value of a specific option. Leave out the
key and value to get all the options and their values
'''
def cmd_config(self, key=None, value=None):
with db.session as s:
if key and value:
if key in forbidden_keys:
return f'Refusing to set "{key}"'
s.put.config(key, value)
return self.cmd_config(key)
elif key and not value:
value = s.get.config(key)
return f'Value for "{key}": {value}'
output = 'Current config:\n'
for key, value in s.get.config_all().items():
if key not in forbidden_keys:
output += f' {key}: {value}\n'
return output
def cmd_set(self, key, *value):
return self.cmd_config(key, ' '.join(value))
def cmd_remove(self, data):
with db.session as s:
if s.delete.inbox(data):
return f'Instance removed: {data}'
else:
return f'Instance does not exist: {data}'
if __name__ == '__main__':
args = sys.argv[1:] if len(sys.argv) > 1 else []
cmd = Command(args)
if cmd.result:
print(cmd.result)