75 lines
No EOL
2.6 KiB
Python
75 lines
No EOL
2.6 KiB
Python
from core.Constants import Constants
|
|
from core.Errors import UnknownConnectionTypeError
|
|
from cli.helpers import get_distribution
|
|
from cli.commands import all_commands
|
|
from cli.commands import get_set, sync_update
|
|
from pathlib import Path
|
|
import argparse
|
|
import sys
|
|
|
|
|
|
def _handle_exception(identifier, message, traceback):
|
|
if issubclass(identifier, UnknownConnectionTypeError):
|
|
print('Please specify the desired connection method and try again.\n')
|
|
else:
|
|
sys.__excepthook__(identifier, message, traceback)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
for path in [
|
|
Constants.HV_CONFIG_HOME,
|
|
Constants.HV_DATA_HOME,
|
|
Constants.HV_STATE_HOME,
|
|
Constants.HV_CACHE_HOME,
|
|
Constants.HV_PROFILE_CONFIG_HOME,
|
|
Constants.HV_PROFILE_DATA_HOME,
|
|
Constants.HV_TICKETING_CONFIG_HOME,
|
|
Constants.HV_TICKETING_DATA_HOME,
|
|
Constants.HV_APPLICATION_DATA_HOME,
|
|
Constants.HV_INCIDENT_DATA_HOME,
|
|
Constants.HV_RUNTIME_DATA_HOME,
|
|
Constants.HV_SESSION_STATE_HOME,
|
|
Constants.HV_TOR_STATE_HOME,
|
|
Constants.SINGBOX_CONFIG_DIR,
|
|
]:
|
|
Path(path).mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
|
|
# Bootstrap DB if needed
|
|
try:
|
|
from core.models.manage.session_management import create_ALL_tables, create_ONLY_db_version_table
|
|
from core.models.manage.version_check import insert_new_version, get_database_version
|
|
from core.models.manage.session_management import get_session
|
|
create_ONLY_db_version_table()
|
|
if get_database_version(get_session()) is None:
|
|
create_ALL_tables()
|
|
insert_new_version(get_session(), Constants.DB_VERSION_THIS_APP_WANTS)
|
|
except Exception:
|
|
pass
|
|
|
|
sys.excepthook = _handle_exception
|
|
|
|
distribution = get_distribution()
|
|
|
|
parser = argparse.ArgumentParser(prog=distribution.name)
|
|
parser.add_argument('--version', '-v', action='version', version=f'{distribution.name} v{distribution.version}')
|
|
subparsers = parser.add_subparsers(title='commands', dest='command')
|
|
|
|
for command in all_commands:
|
|
command.register(subparsers)
|
|
|
|
arguments = parser.parse_args()
|
|
|
|
if arguments.command is None:
|
|
parser.print_help()
|
|
|
|
elif arguments.command in (get_set.NAME, get_set.NAME_SET):
|
|
get_set.handle(arguments, parser)
|
|
|
|
elif arguments.command in (sync_update.NAME_SYNC, sync_update.NAME_UPDATE):
|
|
sync_update.handle(arguments, parser)
|
|
|
|
else:
|
|
command = next((c for c in all_commands if getattr(c, 'NAME', None) == arguments.command), None)
|
|
if command:
|
|
command.handle(arguments, parser) |