-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api/commands)!: added project base command
- Loading branch information
Showing
3 changed files
with
43 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file was deleted.
Oops, something went wrong.