|
| 1 | +import json |
| 2 | +from typing import Annotated, Optional |
| 3 | + |
| 4 | +import questionary |
| 5 | +import typer |
| 6 | + |
| 7 | +from trakcli.config.models import Project |
| 8 | +from trakcli.projects.database import ( |
| 9 | + db_get_project_details, |
| 10 | + db_get_project_details_path, |
| 11 | + get_projects_from_config, |
| 12 | +) |
| 13 | +from trakcli.projects.messages.print_project_archived_toggle import ( |
| 14 | + print_project_archived_toggle, |
| 15 | +) |
| 16 | +from trakcli.projects.messages.print_project_broken_configuration import ( |
| 17 | + print_project_broken_configuration, |
| 18 | +) |
| 19 | +from trakcli.projects.utils.print_missing_project import print_missing_project |
| 20 | +from trakcli.projects.utils.print_no_projects import print_no_projects |
| 21 | +from trakcli.utils.styles_questionary import questionary_style_select |
| 22 | +from rich import print as rprint |
| 23 | + |
| 24 | + |
| 25 | +def command_project_archive(project: Annotated[Optional[str], typer.Argument()] = None): |
| 26 | + """Archive a project.""" |
| 27 | + |
| 28 | + projects_in_config = get_projects_from_config(True) |
| 29 | + |
| 30 | + # Check if there are configured projects |
| 31 | + if not len(projects_in_config): |
| 32 | + print_no_projects() |
| 33 | + return |
| 34 | + |
| 35 | + # Provide the list of prjects to the user |
| 36 | + if not project: |
| 37 | + project = questionary.select( |
| 38 | + "Select a project:", |
| 39 | + choices=projects_in_config, |
| 40 | + pointer="• ", |
| 41 | + show_selected=True, |
| 42 | + style=questionary_style_select, |
| 43 | + ).ask() |
| 44 | + |
| 45 | + if not project: |
| 46 | + return |
| 47 | + |
| 48 | + # Check if the project exists |
| 49 | + if not project or project not in projects_in_config: |
| 50 | + print_missing_project(projects_in_config) |
| 51 | + return |
| 52 | + |
| 53 | + details_path = db_get_project_details_path(project) |
| 54 | + details = db_get_project_details(project) |
| 55 | + |
| 56 | + if details_path and details: |
| 57 | + # Toggle the value of archived |
| 58 | + details = details._replace(archived=not details.archived) |
| 59 | + |
| 60 | + with open(details_path, "w") as details_file: |
| 61 | + json.dump( |
| 62 | + details._asdict(), |
| 63 | + details_file, |
| 64 | + indent=2, |
| 65 | + separators=(",", ": "), |
| 66 | + ) |
| 67 | + print_project_archived_toggle(project, details.archived) |
| 68 | + else: |
| 69 | + print_project_broken_configuration(project) |
| 70 | + return |
0 commit comments