Skip to content

Commit ad58983

Browse files
committed
feat(api/commands)!: added project base command
1 parent 77b88f1 commit ad58983

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

api/outdated/commands.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
1-
from asyncio import run
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
24

35
from django.core.management.base import BaseCommand
46

7+
from outdated.outdated.models import Project
8+
9+
if TYPE_CHECKING: # pragma: no cover
10+
from django.core.management.base import CommandParser
11+
512

6-
class AsyncCommand(BaseCommand):
7-
"""Base command to run async code."""
13+
class ProjectCommand(BaseCommand):
14+
def add_arguments(self, parser: CommandParser):
15+
projects = parser.add_mutually_exclusive_group(required=True)
16+
projects.add_argument(
17+
"--all",
18+
action="store_true",
19+
help="Affect all projects",
20+
)
21+
projects.add_argument("projects", nargs="*", type=str, default=[])
822

923
def handle(self, *args, **options):
10-
run(self._handle(*args, **options))
24+
projects = []
25+
if not options["all"]:
26+
nonexistant_projects = []
27+
project_names = options["projects"]
28+
for name in project_names:
29+
try:
30+
projects.append(Project.objects.get(name__iexact=name))
31+
except Project.DoesNotExist:
32+
nonexistant_projects.append(name)
33+
34+
if nonexistant_projects:
35+
self.stderr.write(
36+
f"Projects with names {nonexistant_projects} do not exist"
37+
)
38+
return
39+
projects = (
40+
Project.objects.filter(id__in=[project.pk for project in projects])
41+
or Project.objects.all()
42+
)
43+
44+
for project in projects:
45+
self._handle(project)
46+
47+
def _handle(self, project: Project): # pragma: no cover
48+
raise NotImplementedError()
Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
from django.core.management.base import BaseCommand
1+
from outdated.commands import ProjectCommand
2+
from outdated.outdated.synchroniser import Synchroniser
23

3-
from ...models import Project
4-
from ...synchroniser import Synchroniser
54

6-
7-
class Command(BaseCommand):
5+
class Command(ProjectCommand):
86
help = "Syncs the given project with its remote counterpart."
97

10-
def add_arguments(self, parser):
11-
parser.add_argument("project_name", type=str)
12-
13-
def handle(self, *args, **options):
14-
project_name = options["project_name"]
15-
try:
16-
project = Project.objects.get(name__iexact=project_name)
17-
self.stdout.write(f"Syncing project {project}")
18-
Synchroniser(project).sync()
19-
self.stdout.write(f"Finished syncing {project}")
20-
except Project.DoesNotExist:
21-
self.stdout.write(f"Project {project_name} not found")
8+
def _handle(self, project):
9+
self.stdout.write(f"Syncing project {project}")
10+
Synchroniser(project).sync()
11+
self.stdout.write(f"Finished syncing {project}")

api/outdated/outdated/management/commands/syncprojects.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)