2021-09-30 17:19:55 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-10-01 18:25:42 +02:00
|
|
|
import click
|
2021-10-04 20:16:59 +02:00
|
|
|
import subprocess
|
|
|
|
|
2022-09-17 20:21:41 +02:00
|
|
|
from traceback import format_exc, format_exception_only, format_tb
|
2022-08-27 04:59:18 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2021-10-01 18:25:42 +02:00
|
|
|
from logger import logging, setup_logging, verbose_option
|
2022-08-27 04:59:18 +02:00
|
|
|
from wrapper import nowrapper_option, enforce_wrap
|
2022-10-16 23:33:08 +02:00
|
|
|
|
2022-10-08 04:04:27 +02:00
|
|
|
from config.cli import config, config_option, cmd_config
|
2022-10-08 02:17:04 +02:00
|
|
|
from packages.cli import cmd_packages
|
2022-10-08 03:25:50 +02:00
|
|
|
from flavours.cli import cmd_flavours
|
2022-10-08 02:17:04 +02:00
|
|
|
from devices.cli import cmd_devices
|
2022-10-16 04:43:45 +02:00
|
|
|
from net.cli import cmd_net
|
2022-10-08 02:45:43 +02:00
|
|
|
from chroot.cli import cmd_chroot
|
2022-10-16 23:33:08 +02:00
|
|
|
from cache.cli import cmd_cache
|
2022-10-16 23:25:47 +02:00
|
|
|
from image.cli import cmd_image
|
2021-08-04 18:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
2022-01-26 17:53:53 +01:00
|
|
|
@click.option('--error-shell', '-E', 'error_shell', is_flag=True, default=False, help='Spawn shell after error occurs')
|
2021-09-09 20:23:23 +02:00
|
|
|
@verbose_option
|
|
|
|
@config_option
|
2021-09-12 06:10:15 +02:00
|
|
|
@nowrapper_option
|
2022-11-09 15:22:06 +01:00
|
|
|
def cli(verbose: bool = False, config_file: Optional[str] = None, wrapper_override: Optional[bool] = None, error_shell: bool = False):
|
2021-09-09 20:23:23 +02:00
|
|
|
setup_logging(verbose)
|
2022-08-27 16:48:50 +02:00
|
|
|
config.runtime.verbose = verbose
|
|
|
|
config.runtime.no_wrap = wrapper_override is False
|
|
|
|
config.runtime.error_shell = error_shell
|
2021-09-09 20:23:23 +02:00
|
|
|
config.try_load_file(config_file)
|
2022-11-09 16:28:09 +01:00
|
|
|
if config.file_state.exception:
|
|
|
|
logging.warning(f"Config file couldn't be loaded: {config.file_state.exception}")
|
2022-08-27 04:59:18 +02:00
|
|
|
if wrapper_override:
|
|
|
|
enforce_wrap()
|
2021-09-12 06:10:15 +02:00
|
|
|
|
2021-08-04 18:36:37 +02:00
|
|
|
|
2021-09-09 20:23:23 +02:00
|
|
|
def main():
|
2021-09-29 02:00:59 +02:00
|
|
|
try:
|
|
|
|
return cli(prog_name='kupferbootstrap')
|
2021-10-05 17:34:43 +02:00
|
|
|
except Exception as ex:
|
2022-08-27 16:48:50 +02:00
|
|
|
if config.runtime.verbose:
|
2022-09-17 20:21:41 +02:00
|
|
|
msg = format_exc()
|
2021-10-05 17:34:43 +02:00
|
|
|
else:
|
2022-09-17 20:21:41 +02:00
|
|
|
tb_start = ''.join(format_tb(ex.__traceback__, limit=1)).strip('\n')
|
|
|
|
tb_end = ''.join(format_tb(ex.__traceback__, limit=-1)).strip('\n')
|
|
|
|
short_tb = [
|
|
|
|
'Traceback (most recent call last):',
|
|
|
|
tb_start,
|
|
|
|
'[...]',
|
|
|
|
tb_end,
|
|
|
|
format_exception_only(ex)[-1], # type: ignore[arg-type]
|
|
|
|
]
|
|
|
|
msg = '\n'.join(short_tb)
|
|
|
|
logging.fatal('\n' + msg)
|
2022-08-27 16:48:50 +02:00
|
|
|
if config.runtime.error_shell:
|
2021-10-05 17:34:43 +02:00
|
|
|
logging.info('Starting error shell. Type exit to quit.')
|
2021-10-04 20:16:59 +02:00
|
|
|
subprocess.call('/bin/bash')
|
2021-09-29 02:00:59 +02:00
|
|
|
exit(1)
|
2021-08-04 18:36:37 +02:00
|
|
|
|
2021-09-29 23:18:12 +02:00
|
|
|
|
2022-10-08 02:17:04 +02:00
|
|
|
cli.add_command(cmd_cache)
|
|
|
|
cli.add_command(cmd_chroot)
|
|
|
|
cli.add_command(cmd_config)
|
|
|
|
cli.add_command(cmd_devices)
|
2022-10-08 02:20:16 +02:00
|
|
|
cli.add_command(cmd_flavours)
|
2022-10-08 02:17:04 +02:00
|
|
|
cli.add_command(cmd_image)
|
2022-10-16 04:43:45 +02:00
|
|
|
cli.add_command(cmd_net)
|
2022-10-08 02:17:04 +02:00
|
|
|
cli.add_command(cmd_packages)
|
2021-09-12 06:10:15 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|