Skip to content

Commit a5d17fb

Browse files
authored
Merge pull request #41 from aclark4life/main
Refactor with typer
2 parents be2a468 + dcc2ca5 commit a5d17fb

File tree

5 files changed

+690
-757
lines changed

5 files changed

+690
-757
lines changed

django_mongodb_cli/repo.py

Lines changed: 155 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from .utils import Package, Repo, Test
55

66
repo = typer.Typer()
7+
repo_remote = typer.Typer()
8+
repo.add_typer(repo_remote, name="remote")
79

810

911
def repo_command(
@@ -33,52 +35,153 @@ def main(
3335
list_repos: bool = typer.Option(
3436
False, "--list-repos", "-l", help="List available repositories."
3537
),
38+
quiet: bool = typer.Option(
39+
False, "--quiet", "-q", help="Suppress output messages."
40+
),
3641
):
3742
if list_repos:
3843
Repo().list_repos()
3944
raise typer.Exit() # End, no further action
4045

46+
ctx.ensure_object(dict)
47+
ctx.obj["quiet"] = quiet
48+
4149
if ctx.invoked_subcommand is None:
4250
typer.echo(ctx.get_help())
4351
raise typer.Exit()
4452

4553

54+
@repo_remote.callback(invoke_without_command=True)
55+
def remote(
56+
ctx: typer.Context,
57+
repo_name: str = typer.Argument(None),
58+
all_repos: bool = typer.Option(
59+
False, "--all-repos", "-a", help="Show remotes of all repositories"
60+
),
61+
):
62+
"""
63+
Show the git remotes for the specified repository.
64+
If --all-repos is used, show remotes for all repositories.
65+
"""
66+
repo = Repo()
67+
repo.ctx = ctx
68+
repo.ctx.obj["repo_name"] = repo_name
69+
repo_command(
70+
all_repos,
71+
repo_name,
72+
all_msg="Showing remotes for all repositories...",
73+
missing_msg="Please specify a repository name or use -a,--all-repos to show remotes of all repositories.",
74+
single_func=lambda repo_name: repo.get_repo_remote(repo_name),
75+
all_func=lambda repo_name: repo.get_repo_remote(repo_name),
76+
)
77+
78+
79+
@repo_remote.command("add")
80+
def remote_add(
81+
ctx: typer.Context,
82+
remote_name: str = typer.Argument(..., help="Name of the remote to add"),
83+
remote_url: str = typer.Argument(..., help="URL of the remote to add"),
84+
):
85+
"""
86+
Add a git remote to the specified repository.
87+
"""
88+
repo = Repo()
89+
repo.ctx = ctx
90+
repo.remote_add(remote_name, remote_url)
91+
92+
93+
@repo_remote.command("remove")
94+
def remote_remove(
95+
ctx: typer.Context,
96+
remote_name: str = typer.Argument(..., help="Name of the remote to remove"),
97+
):
98+
"""
99+
Remove a git remote from the specified repository.
100+
"""
101+
repo = Repo()
102+
repo.ctx = ctx
103+
repo.remote_remove(remote_name)
104+
105+
46106
@repo.command()
47107
def branch(
108+
ctx: typer.Context,
48109
repo_name: str = typer.Argument(None),
49-
branch_name: str = typer.Argument(None),
110+
branch_name: str = typer.Argument(None, help="Branch name"),
50111
list_branches: bool = typer.Option(
51112
False, "--list-branches", "-l", help="List branches of the repository"
52113
),
53114
all_repos: bool = typer.Option(
54115
False, "--all-repos", "-a", help="Show branches of all repositories"
55116
),
117+
delete_branch: bool = typer.Option(
118+
False, "--delete-branch", "-d", help="Delete the specified branch"
119+
),
120+
cloned_only: bool = typer.Option(
121+
False, "--cloned-only", "-c", help="Show branches only for cloned repositories"
122+
),
56123
):
57124
"""
58125
Checkout or create a branch in a repository.
59-
If branch_name is provided, switch to that branch or create it if it doesn't exist.
60126
If --all-repos is used, show branches for all repositories.
61127
"""
62128
repo = Repo()
63-
if branch_name:
64-
repo.set_branch(branch_name)
65-
66-
# else:
67-
# typer.echo(
68-
# typer.style(
69-
# "Create or set branch cannot be used with --all-repos.",
70-
# fg=typer.colors.RED,
71-
# )
72-
# )
73-
# return
74-
129+
repo.ctx = ctx
130+
repo_list = repo.map
131+
if delete_branch and branch_name:
132+
repo.delete_branch(repo_name, branch_name)
133+
raise typer.Exit()
134+
if cloned_only:
135+
_, fs_repos = repo._list_repos()
136+
repo_list = sorted(fs_repos)
75137
repo_command(
76138
all_repos,
77139
repo_name,
78-
all_msg="Showing branches for all repositories...",
79-
missing_msg="Please specify a repository name or use --all-repos to show branches of all repositories.",
80-
single_func=lambda repo_name: repo.get_repo_branch(repo_name),
81-
all_func=lambda repo_name: repo.get_repo_branch(repo_name),
140+
all_msg=None,
141+
missing_msg="Please specify a repository name or use -a,--all-repos to show branches of all repositories.",
142+
single_func=lambda repo_name: repo.get_repo_branch(repo_name, branch_name),
143+
all_func=lambda repo_name: repo.get_repo_branch(repo_name, branch_name),
144+
repo_list=repo_list,
145+
)
146+
147+
148+
@repo.command()
149+
def cd(
150+
repo_name: str = typer.Argument(None),
151+
):
152+
"""
153+
Change directory to the specified repository.
154+
"""
155+
156+
repo_command(
157+
False,
158+
repo_name,
159+
all_msg=None,
160+
missing_msg="Please specify a repository name.",
161+
single_func=Repo().cd_repo,
162+
all_func=Repo().cd_repo,
163+
)
164+
165+
166+
@repo.command()
167+
def checkout(
168+
repo_name: str = typer.Argument(None),
169+
branch_name: str = typer.Argument(None, help="Branch name to checkout"),
170+
):
171+
"""
172+
Checkout a branch in a repository.
173+
"""
174+
175+
def checkout_branch(name):
176+
Repo().checkout_branch(repo_name, branch_name)
177+
178+
repo_command(
179+
False,
180+
repo_name,
181+
all_msg=None,
182+
missing_msg="Please specify a repository and branch name.",
183+
single_func=checkout_branch,
184+
all_func=checkout_branch,
82185
)
83186

84187

@@ -107,7 +210,7 @@ def clone_repo(name):
107210
all_repos,
108211
repo_name,
109212
all_msg="Cloning all repositories...",
110-
missing_msg="Please specify a repository name or use --all-repos to clone all repositories.",
213+
missing_msg="Please specify a repository name or use -a,--all-repos to clone all repositories.",
111214
single_func=clone_repo,
112215
all_func=clone_repo,
113216
)
@@ -144,7 +247,7 @@ def do_commit(name):
144247

145248

146249
@repo.command()
147-
def delete(
250+
def rm(
148251
repo_name: str = typer.Argument(None),
149252
all_repos: bool = typer.Option(
150253
False, "--all-repos", "-a", help="Delete all repositories"
@@ -168,13 +271,34 @@ def do_delete(name):
168271
all_repos,
169272
repo_name,
170273
all_msg="Deleting all repositories...",
171-
missing_msg="Please specify a repository name or use --all-repos to delete all repositories.",
274+
missing_msg="Please specify a repository name or use -a,--all-repos to delete all repositories.",
172275
single_func=do_delete,
173276
all_func=do_delete,
174277
fg=typer.colors.RED, # Red for delete
175278
)
176279

177280

281+
@repo.command()
282+
def diff(
283+
repo_name: str = typer.Argument(None),
284+
all_repos: bool = typer.Option(
285+
False, "--all-repos", "-a", help="Show diffs of all repositories"
286+
),
287+
):
288+
"""
289+
Show the git diff for the specified repository.
290+
If --all-repos is used, show diffs for all repositories.
291+
"""
292+
repo_command(
293+
all_repos,
294+
repo_name,
295+
all_msg="Showing diffs for all repositories...",
296+
missing_msg="Please specify a repository name or use -a,--all-repos to show diffs of all repositories.",
297+
single_func=lambda repo_name: Repo().get_repo_diff(repo_name),
298+
all_func=lambda repo_name: Repo().get_repo_diff(repo_name),
299+
)
300+
301+
178302
@repo.command()
179303
def install(
180304
repo_name: str = typer.Argument(None),
@@ -190,7 +314,7 @@ def install(
190314
all_repos,
191315
repo_name,
192316
all_msg="Installing all repositories...",
193-
missing_msg="Please specify a repository name or use --all-repos to install all repositories.",
317+
missing_msg="Please specify a repository name or use -a,--all-repos to install all repositories.",
194318
single_func=lambda repo_name: Package().install_package(repo_name),
195319
all_func=lambda repo_name: Package().install_package(repo_name),
196320
)
@@ -238,39 +362,6 @@ def open(
238362
)
239363

240364

241-
@repo.command()
242-
def origin(
243-
repo_name: str = typer.Argument(None),
244-
repo_user: str = typer.Argument(None),
245-
all_repos: bool = typer.Option(
246-
False, "--all-repos", "-a", help="Show origin of all repositories"
247-
),
248-
):
249-
"""
250-
Show or set the origin of a repository.
251-
"""
252-
repo = Repo()
253-
if repo_user and all_repos:
254-
typer.echo(
255-
typer.style(
256-
"Set origin cannot be used with --all-repos.",
257-
fg=typer.colors.RED,
258-
)
259-
)
260-
return
261-
if repo_user:
262-
repo.set_user(repo_user)
263-
264-
repo_command(
265-
all_repos,
266-
repo_name,
267-
all_msg="Showing origin for all repositories...",
268-
missing_msg="Please specify a repository name or use --all-repos to show origins of all repositories.",
269-
single_func=lambda name: repo.get_repo_origin(name),
270-
all_func=lambda repo_name: repo.get_repo_origin(repo_name),
271-
)
272-
273-
274365
@repo.command()
275366
def patch(
276367
repo_name: str = typer.Argument(None),
@@ -327,14 +418,15 @@ def reset_repo(name):
327418
all_repos,
328419
repo_name,
329420
all_msg="Resetting all repositories...",
330-
missing_msg="Please specify a repository name or use --all-repos to reset all repositories.",
421+
missing_msg="Please specify a repository name or use -a,--all-repos to reset all repositories.",
331422
single_func=reset_repo,
332423
all_func=reset_repo,
333424
)
334425

335426

336427
@repo.command()
337428
def status(
429+
ctx: typer.Context,
338430
repo_name: str = typer.Argument(None),
339431
all_repos: bool = typer.Option(
340432
False, "--all-repos", "-a", help="Show status of all repos"
@@ -345,18 +437,20 @@ def status(
345437
If --all-repos is used, show the status for all repositories.
346438
"""
347439
repo = Repo()
440+
repo.ctx = ctx
348441
repo_command(
349442
all_repos,
350443
repo_name,
351444
all_msg="Showing status for all repositories...",
352-
missing_msg="Please specify a repository name or use --all-repos to show all repositories.",
445+
missing_msg="Please specify a repository name or use -a,--all-repos to show all repositories.",
353446
single_func=lambda repo_name: repo.get_repo_status(repo_name),
354447
all_func=lambda repo_name: repo.get_repo_status(repo_name),
355448
)
356449

357450

358451
@repo.command()
359452
def sync(
453+
ctx: typer.Context,
360454
repo_name: str = typer.Argument(None),
361455
all_repos: bool = typer.Option(
362456
False, "--all-repos", "-a", help="Sync all repositories"
@@ -367,6 +461,7 @@ def sync(
367461
If --all-repos is used, sync all repositories.
368462
"""
369463
repo = Repo()
464+
repo.ctx = ctx
370465
if not repo.map:
371466
typer.echo(
372467
typer.style(
@@ -380,14 +475,15 @@ def sync(
380475
all_repos,
381476
repo_name,
382477
all_msg="Syncing all repositories...",
383-
missing_msg="Please specify a repository name or use --all-repos to sync all repositories.",
478+
missing_msg="Please specify a repository name or use -a,--all-repos to sync all repositories.",
384479
single_func=lambda repo_name: repo.sync_repo(repo_name),
385480
all_func=lambda repo_name: repo.sync_repo(repo_name),
386481
)
387482

388483

389484
@repo.command()
390485
def test(
486+
ctx: typer.Context,
391487
repo_name: str = typer.Argument(None),
392488
modules: list[str] = typer.Argument(None),
393489
keep_db: bool = typer.Option(
@@ -414,6 +510,7 @@ def test(
414510
If --setenv is used, set the DJANGO_SETTINGS_MODULE environment variable.
415511
"""
416512
test_runner = Test()
513+
test_runner.ctx = ctx
417514
if modules:
418515
test_runner.set_modules(modules)
419516
if keep_db:

0 commit comments

Comments
 (0)