Skip to content

Commit c53fb88

Browse files
authored
Merge pull request #186 from qmk/found-repo-prompt
2 parents fed1e39 + 27a97e4 commit c53fb88

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

qmk_cli/subcommands/setup.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import subprocess
66
import sys
77
from pathlib import Path
8+
from shutil import rmtree
89

910
from milc import cli
10-
from milc.questions import yesno
11+
from milc.questions import choice, question, yesno
1112
from qmk_cli.git import git_clone
1213
from qmk_cli.helpers import is_qmk_firmware
1314

@@ -43,6 +44,17 @@ def git_upstream(destination):
4344
return False
4445

4546

47+
def git_clone_fork(fork, branch, force=False):
48+
if force:
49+
rmtree(cli.args.home)
50+
51+
git_url = '/'.join((cli.args.baseurl, fork))
52+
if git_clone(git_url, cli.args.home, branch):
53+
git_upstream(cli.args.home)
54+
else:
55+
exit(1)
56+
57+
4658
@cli.argument('-n', '--no', arg_only=True, action='store_true', help='Answer no to all questions')
4759
@cli.argument('-y', '--yes', arg_only=True, action='store_true', help='Answer yes to all questions')
4860
@cli.argument('--baseurl', arg_only=True, default=default_base, help='The URL all git operations start from. Default: %s' % default_base)
@@ -61,9 +73,34 @@ def setup(cli):
6173
cli.log.error("Can't use both --yes and --no at the same time.")
6274
exit(1)
6375

64-
# Check on qmk_firmware, and if it doesn't exist offer to check it out.
76+
# Check on qmk_firmware
77+
# If it exists, ask the user what to do with it
78+
# If it doesn't exist, offer to check it out
6579
if is_qmk_firmware(cli.args.home):
6680
cli.log.info('Found qmk_firmware at %s.', str(cli.args.home))
81+
found_prompt = "What do you want to do?"
82+
found_options = [
83+
f"Delete and reclone {cli.args.fork}",
84+
"Delete and clone a different fork",
85+
"Keep it and continue"
86+
]
87+
delete_confirm = "WARNING: This will delete your current qmk_firmware directory. Proceed?"
88+
89+
found_action = choice(found_prompt, options=found_options, default=2)
90+
if found_action == f"Delete and reclone {cli.args.fork}":
91+
if not yesno(delete_confirm, default=False):
92+
exit(1)
93+
94+
git_clone_fork(cli.args.fork, cli.args.branch, force=True)
95+
96+
elif found_action == "Delete and clone a different fork":
97+
fork_name = question("Enter the name of the fork:", default=cli.args.fork)
98+
branch_name = question("Enter the branch name to clone:", default=cli.args.branch)
99+
100+
if not yesno(delete_confirm, default=False):
101+
exit(1)
102+
103+
git_clone_fork(fork_name, branch_name, force=True)
67104

68105
# Exists (but not an empty dir)
69106
elif cli.args.home.exists() and any(cli.args.home.iterdir()):
@@ -78,12 +115,7 @@ def setup(cli):
78115
else:
79116
cli.log.error('Could not find qmk_firmware!')
80117
if yesno(clone_prompt):
81-
git_url = '/'.join((cli.args.baseurl, cli.args.fork))
82-
83-
if git_clone(git_url, cli.args.home, cli.args.branch):
84-
git_upstream(cli.args.home)
85-
else:
86-
exit(1)
118+
git_clone_fork(cli.args.fork, cli.args.branch)
87119
else:
88120
cli.log.warning('Not cloning qmk_firmware due to user input or --no flag.')
89121

0 commit comments

Comments
 (0)