Skip to content

Commit 4b56443

Browse files
committed
refactor(init): describe as @click.command
1 parent 590e712 commit 4b56443

File tree

2 files changed

+30
-48
lines changed

2 files changed

+30
-48
lines changed

compiler_admin/commands/init.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from argparse import Namespace
21
import os
32
from pathlib import Path
43
from shutil import rmtree
54
import subprocess
65

7-
from compiler_admin import RESULT_FAILURE, RESULT_SUCCESS
6+
import click
7+
88
from compiler_admin.services.google import USER_ARCHIVE, CallGAMCommand
99

1010

@@ -22,48 +22,37 @@ def _clean_config_dir(config_dir: Path) -> None:
2222
rmtree(path)
2323

2424

25-
def init(args: Namespace, *extras) -> int:
26-
"""Initialize a new GAM project.
27-
28-
See https://github.com/taers232c/GAMADV-XTD3/wiki/How-to-Install-Advanced-GAM
29-
30-
Args:
31-
username (str): The Compiler admin with which to initialize a new project.
32-
33-
gam (bool): If True, initialize a new GAM project.
25+
@click.command()
26+
@click.option("--gam", "init_gam", is_flag=True)
27+
@click.option("--gyb", "init_gyb", is_flag=True)
28+
@click.argument("username")
29+
def init(username: str, init_gam: bool = False, init_gyb: bool = False):
30+
"""Initialize a new GAM and/or GYB project.
3431
35-
gyb (bool): If True, initialize a new GYB project.
32+
See:
3633
37-
Returns:
38-
A value indicating if the operation succeeded or failed.
34+
- https://github.com/taers232c/GAMADV-XTD3/wiki/How-to-Install-Advanced-GAM
35+
- https://github.com/GAM-team/got-your-back/wiki
3936
"""
40-
if not hasattr(args, "username"):
41-
raise ValueError("username is required")
42-
43-
admin_user = args.username
44-
res = RESULT_SUCCESS
45-
46-
if getattr(args, "gam", False):
37+
if init_gam:
4738
_clean_config_dir(GAM_CONFIG_PATH)
4839
# GAM is already installed via pyproject.toml
49-
res += CallGAMCommand(("config", "drive_dir", str(GAM_CONFIG_PATH), "verify"))
50-
res += CallGAMCommand(("create", "project"))
51-
res += CallGAMCommand(("oauth", "create"))
52-
res += CallGAMCommand(("user", admin_user, "check", "serviceaccount"))
40+
CallGAMCommand(("config", "drive_dir", str(GAM_CONFIG_PATH), "verify"))
41+
CallGAMCommand(("create", "project"))
42+
CallGAMCommand(("oauth", "create"))
43+
CallGAMCommand(("user", username, "check", "serviceaccount"))
5344

54-
if getattr(args, "gyb", False):
45+
if init_gyb:
5546
_clean_config_dir(GYB_CONFIG_PATH)
5647
# download GYB installer to config directory
5748
gyb = GYB_CONFIG_PATH / "gyb-install.sh"
5849
with gyb.open("w+") as dest:
59-
res += subprocess.call(("curl", "-s", "-S", "-L", "https://gyb-shortn.jaylee.us/gyb-install"), stdout=dest)
50+
subprocess.call(("curl", "-s", "-S", "-L", "https://gyb-shortn.jaylee.us/gyb-install"), stdout=dest)
6051

61-
res += subprocess.call(("chmod", "+x", str(gyb.absolute())))
52+
subprocess.call(("chmod", "+x", str(gyb.absolute())))
6253

6354
# install, giving values to some options
6455
# https://github.com/GAM-team/got-your-back/blob/main/install-gyb.sh
6556
#
6657
# use GYB_CONFIG_PATH.parent for the install directory option, otherwise we get a .config/gyb/gyb directory structure
67-
res += subprocess.call((gyb, "-u", admin_user, "-r", USER_ARCHIVE, "-d", str(GYB_CONFIG_PATH.parent)))
68-
69-
return RESULT_SUCCESS if res == RESULT_SUCCESS else RESULT_FAILURE
58+
subprocess.call((gyb, "-u", username, "-r", USER_ARCHIVE, "-d", str(GYB_CONFIG_PATH.parent)))

tests/commands/test_init.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from argparse import Namespace
21
import pytest
32

3+
from compiler_admin import RESULT_SUCCESS
44
from compiler_admin.commands.init import _clean_config_dir, init, __name__ as MODULE
55

66

@@ -52,35 +52,28 @@ def test_clean_config_dir(mocker, mock_GAM_CONFIG_PATH, mock_rmtree):
5252
assert mock_dir in mock_rmtree.call_args.args
5353

5454

55-
def test_init_admin_user_required():
56-
args = Namespace()
57-
58-
with pytest.raises(ValueError, match="username is required"):
59-
init(args)
60-
61-
62-
def test_init_default(mock_clean_config_dir, mock_google_CallGAMCommand, mock_subprocess_call):
63-
args = Namespace(username="username")
64-
init(args)
55+
def test_init_default(cli_runner, mock_clean_config_dir, mock_google_CallGAMCommand, mock_subprocess_call):
56+
result = cli_runner.invoke(init, ["username"])
6557

58+
assert result.exit_code == RESULT_SUCCESS
6659
assert mock_clean_config_dir.call_count == 0
6760
assert mock_google_CallGAMCommand.call_count == 0
6861
assert mock_subprocess_call.call_count == 0
6962

7063

71-
def test_init_gam(mock_GAM_CONFIG_PATH, mock_clean_config_dir, mock_google_CallGAMCommand):
72-
args = Namespace(username="username", gam=True, gyb=False)
73-
init(args)
64+
def test_init_gam(cli_runner, mock_GAM_CONFIG_PATH, mock_clean_config_dir, mock_google_CallGAMCommand):
65+
result = cli_runner.invoke(init, ["--gam", "username"])
7466

67+
assert result.exit_code == RESULT_SUCCESS
7568
mock_clean_config_dir.assert_called_once()
7669
assert mock_GAM_CONFIG_PATH in mock_clean_config_dir.call_args.args
7770
assert mock_google_CallGAMCommand.call_count > 0
7871

7972

80-
def test_init_gyb(mock_GYB_CONFIG_PATH, mock_clean_config_dir, mock_subprocess_call):
81-
args = Namespace(username="username", gam=False, gyb=True)
82-
init(args)
73+
def test_init_gyb(cli_runner, mock_GYB_CONFIG_PATH, mock_clean_config_dir, mock_subprocess_call):
74+
result = cli_runner.invoke(init, ["--gyb", "username"])
8375

76+
assert result.exit_code == RESULT_SUCCESS
8477
mock_clean_config_dir.assert_called_once()
8578
assert mock_GYB_CONFIG_PATH in mock_clean_config_dir.call_args.args
8679
assert mock_subprocess_call.call_count > 0

0 commit comments

Comments
 (0)