Skip to content

Commit

Permalink
feat(api/commands)!: added project base command
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Sep 5, 2023
1 parent 257aeaa commit 1683fb2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
41 changes: 36 additions & 5 deletions api/outdated/commands.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
from asyncio import run
from django.core.management.base import BaseCommand, CommandParser

from django.core.management.base import BaseCommand
from outdated.outdated.models import Project


class AsyncCommand(BaseCommand):
"""Base command to run async code."""
class ProjectCommand(BaseCommand):
def add_arguments(self, parser: CommandParser):
projects = parser.add_mutually_exclusive_group(required=True)
projects.add_argument(
"--all",
action="store_true",
help="Affect all projects",
)
projects.add_argument("projects", nargs="*", type=str, default=[])

def handle(self, *args, **options):
run(self._handle(*args, **options))
projects = []
if not options["all"]:
nonexistant_projects = []
project_names = options["projects"]
for name in project_names:
try:
projects.append(Project.objects.get(name__iexact=name))
except Project.DoesNotExist:
nonexistant_projects.append(name)

if nonexistant_projects:
self.stderr.write(
f"Projects with names {nonexistant_projects} do not exist"
)
return
projects = (
Project.objects.filter(id__in=[project.pk for project in projects])
or Project.objects.all()
)

for project in projects:
self._handle(project)

def _handle(self, project: Project): # pragma: no cover
raise NotImplementedError()
24 changes: 7 additions & 17 deletions api/outdated/outdated/management/commands/syncproject.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
from django.core.management.base import BaseCommand
from outdated.commands import ProjectCommand
from outdated.outdated.synchroniser import Synchroniser

from ...models import Project
from ...synchroniser import Synchroniser


class Command(BaseCommand):
class Command(ProjectCommand):
help = "Syncs the given project with its remote counterpart."

def add_arguments(self, parser):
parser.add_argument("project_name", type=str)

def handle(self, *args, **options):
project_name = options["project_name"]
try:
project = Project.objects.get(name__iexact=project_name)
self.stdout.write(f"Syncing project {project}")
Synchroniser(project).sync()
self.stdout.write(f"Finished syncing {project}")
except Project.DoesNotExist:
self.stdout.write(f"Project {project_name} not found")
def _handle(self, project):
self.stdout.write(f"Syncing project {project}")
Synchroniser(project).sync()
self.stdout.write(f"Finished syncing {project}")
16 changes: 0 additions & 16 deletions api/outdated/outdated/management/commands/syncprojects.py

This file was deleted.

0 comments on commit 1683fb2

Please sign in to comment.