From faa855eda9d8149fcf5a76bf9e93baa2a700daad Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Thu, 30 Sep 2021 03:49:20 +0200 Subject: [PATCH] Improve `cache clean` --- cache.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/cache.py b/cache.py index 5050e9f..10f5487 100644 --- a/cache.py +++ b/cache.py @@ -5,6 +5,8 @@ from config import config from wrapper import enforce_wrap import logging +PATHS = ['chroots', 'pacman', 'jumpdrive'] + @click.group(name='cache') def cmd_cache(): @@ -12,14 +14,30 @@ def cmd_cache(): @cmd_cache.command(name='clean') -def cmd_clean(): +@click.option('--force', default=False) +@click.argument('paths', nargs=-1, required=False) +def cmd_clean(paths: list[str], force=False): + if unknown_paths := (set(paths) - set(PATHS + ['all'])): + raise Exception(f"Unknown paths: {' ,'.join(unknown_paths)}") + if 'all' in paths or (not paths and force): + paths = PATHS.copy() + enforce_wrap() - for path_name in ['chroots', 'pacman', 'jumpdrive']: - dir = config.file['paths'][path_name] - for file in os.listdir(dir): - path = os.path.join(dir, file) - logging.debug('Removing "{path}"') - if os.path.isdir(path): - shutil.rmtree(path) - else: - os.unlink(path) + + clear = {path: (path in paths) for path in PATHS} + query = not paths + if not query or force: + click.confirm(f'Really clear {", ".join(paths)}?', abort=True) + for path_name in PATHS: + if query: + clear[path_name] = click.confirm(f'Clear {path_name}?') + if clear[path_name]: + logging.info(f'Clearing {path_name}') + dir = config.get_path(path_name) + for file in os.listdir(dir): + path = os.path.join(dir, file) + logging.debug(f'Removing "{path_name}/{file}"') + if os.path.isdir(path): + shutil.rmtree(path) + else: + os.unlink(path)