Skip to content

Commit 091a062

Browse files
authored
Merge pull request #6 from git-mastery/woojiahao/refactor
Cleaning up codebase
2 parents 79d2b74 + b1174a8 commit 091a062

File tree

22 files changed

+534
-458
lines changed

22 files changed

+534
-458
lines changed

app/cli.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66

77
from app.commands import check, download, progress, setup, verify
88
from app.commands.version import version
9-
from app.utils.click import warn
9+
from app.utils.click import ClickColor, CliContextKey, warn
10+
from app.utils.gitmastery import (
11+
find_exercise_root,
12+
find_gitmastery_root,
13+
read_gitmastery_config,
14+
read_exercise_config,
15+
)
1016
from app.utils.version import Version
1117
from app.version import __version__
1218

@@ -24,20 +30,36 @@ def invoke(self, ctx: click.Context) -> None:
2430
def cli(ctx, verbose) -> None:
2531
"""Git-Mastery app"""
2632
ctx.ensure_object(dict)
27-
ctx.obj["VERBOSE"] = verbose
33+
34+
# Attempt to load both Git-Mastery root config and exercise config where possible
35+
gitmastery_root = find_gitmastery_root()
36+
if gitmastery_root is not None:
37+
root_path, cds = gitmastery_root
38+
gitmastery_root_config = read_gitmastery_config(root_path, cds)
39+
ctx.obj[CliContextKey.GITMASTERY_ROOT_CONFIG] = gitmastery_root_config
40+
41+
exercise_root = find_exercise_root()
42+
if exercise_root is not None:
43+
root_path, cds = exercise_root
44+
exercise_root_config = read_exercise_config(root_path, cds)
45+
ctx.obj[CliContextKey.GITMASTERY_EXERCISE_CONFIG] = exercise_root_config
46+
47+
ctx.obj[CliContextKey.VERBOSE] = verbose
48+
2849
current_version = Version.parse_version_string(__version__)
50+
ctx.obj[CliContextKey.VERSION] = current_version
2951
latest_version = (
3052
requests.get(
3153
"https://github.com/git-mastery/app/releases/latest", allow_redirects=False
3254
)
3355
.headers["Location"]
3456
.rsplit("/", 1)[-1]
3557
)
36-
if current_version.is_behind(latest_version):
58+
if current_version.is_behind(Version.parse_version_string(latest_version)):
3759
warn(
3860
click.style(
3961
f"Your version of Git-Mastery app {current_version} is behind the latest version {latest_version}.",
40-
fg="bright_red",
62+
fg=ClickColor.BRIGHT_RED,
4163
)
4264
)
4365
warn("We strongly recommend upgrading your app.")
@@ -47,10 +69,7 @@ def cli(ctx, verbose) -> None:
4769

4870

4971
def start() -> None:
50-
cli.add_command(check)
51-
cli.add_command(download)
52-
cli.add_command(progress)
53-
cli.add_command(setup)
54-
cli.add_command(verify)
55-
cli.add_command(version)
72+
commands = [check, download, progress, setup, verify, version]
73+
for command in commands:
74+
cli.add_command(command)
5675
cli(obj={})

app/commands/check/git.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import click
22

33
from app.utils.click import error, info, success
4-
from app.utils.git_cli import get_git_config, is_git_installed
4+
from app.utils.git import get_git_config, is_git_installed
55

66

77
@click.command()
8-
@click.pass_context
9-
def git(ctx: click.Context) -> None:
8+
def git() -> None:
109
"""
1110
Verifies if Git is installed and setup for Git-Mastery.
1211
"""
13-
verbose = ctx.obj["VERBOSE"]
14-
1512
info("Checking that you have Git installed and configured")
16-
if is_git_installed(verbose):
13+
if is_git_installed():
1714
info("Git is installed")
1815
else:
1916
error("Git is not installed")
2017

21-
config_user_name = get_git_config("user.name", verbose)
18+
config_user_name = get_git_config("user.name")
2219
if not config_user_name:
2320
error(
2421
f"You do not have {click.style('user.name', bold=True)} yet. Run {click.style('git config --global user.name <name>', bold=True, italic=True)}."
@@ -28,7 +25,7 @@ def git(ctx: click.Context) -> None:
2825
f"You have set {click.style('user.name', bold=True)} as {click.style(config_user_name, bold=True, italic=True)}"
2926
)
3027

31-
config_user_email = get_git_config("user.email", verbose)
28+
config_user_email = get_git_config("user.email")
3229
if not config_user_email:
3330
error(
3431
f"You do not have {click.style('user.email', bold=True)} yet. Run {click.style('git config --global user.email <email>', bold=True, italic=True)}."

app/commands/check/github.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
import click
22

33
from app.utils.click import error, info, success
4-
from app.utils.gh_cli import (
4+
from app.utils.github_cli import (
55
has_delete_repo_scope,
66
is_authenticated,
77
is_github_cli_installed,
88
)
99

1010

1111
@click.command()
12-
@click.pass_context
13-
def github(ctx: click.Context) -> None:
12+
def github() -> None:
1413
"""
1514
Verifies if Github and Github CLI is installed and setup for Git-Mastery.
1615
"""
17-
verbose = ctx.obj["VERBOSE"]
18-
1916
info("Checking that you have Github CLI is installed and configured")
2017

21-
if is_github_cli_installed(verbose):
18+
if is_github_cli_installed():
2219
info("Github CLI is installed")
2320
else:
2421
error("Github CLI is not installed yet")
2522

26-
if is_authenticated(verbose):
23+
if is_authenticated():
2724
info("You have authenticated Github CLI")
2825
else:
2926
error("You have not authenticated Github CLI")
3027

31-
if has_delete_repo_scope(verbose):
28+
if has_delete_repo_scope():
3229
info("You have authenticated Github CLI with the 'delete_repo' scope")
3330
else:
3431
error(

0 commit comments

Comments
 (0)