Skip to content

Commit 2ed007d

Browse files
committed
refactor(main): set function to call for command
avoids matching on string names, simplifies adding new commands
1 parent 9fdc65b commit 2ed007d

File tree

4 files changed

+71
-26
lines changed

4 files changed

+71
-26
lines changed

compiler_admin/commands/info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from compiler_admin.services.google import CallGAMCommand, CallGYBCommand
33

44

5-
def info() -> int:
5+
def info(*args, **kwargs) -> int:
66
"""Print information about this package and the GAM environment.
77
88
Returns:

compiler_admin/commands/init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def _clean_config_dir(config_dir: Path) -> None:
2222
rmtree(path)
2323

2424

25-
def init(args: Namespace) -> int:
25+
def init(args: Namespace, *extras) -> int:
2626
"""Initialize a new GAM project.
2727
2828
See https://github.com/taers232c/GAMADV-XTD3/wiki/How-to-Install-Advanced-GAM

compiler_admin/main.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ def main(argv=None):
3737

3838
cmd_parsers = parser.add_subparsers(dest="command", help="The command to run")
3939

40-
cmd_parsers.add_parser("info", help="Print configuration and debugging information.")
40+
info_cmd = add_sub_cmd(cmd_parsers, "info", help="Print configuration and debugging information.")
41+
info_cmd.set_defaults(func=info)
4142

4243
init_cmd = add_sub_cmd_username(
4344
cmd_parsers, "init", help="Initialize a new admin project. This command should be run once before any others."
4445
)
4546
init_cmd.add_argument("--gam", action="store_true", help="If provided, initialize a new GAM project.")
4647
init_cmd.add_argument("--gyb", action="store_true", help="If provided, initialize a new GYB project.")
48+
init_cmd.set_defaults(func=init)
4749

4850
user_cmd = add_sub_cmd(cmd_parsers, "user", help="Work with users in the Compiler org.")
51+
user_cmd.set_defaults(func=user)
4952
user_subcmds = user_cmd.add_subparsers(dest="subcommand", help="The user command to run.")
5053

5154
user_create = add_sub_cmd_username(user_subcmds, "create", help="Create a new user in the Compiler domain.")
@@ -78,12 +81,10 @@ def main(argv=None):
7881

7982
args, extra = parser.parse_known_args(argv)
8083

81-
if args.command == "info":
82-
return info()
83-
if args.command == "init":
84-
return init(args)
85-
elif args.command == "user":
86-
return user(args, *extra)
84+
if args.func:
85+
return args.func(args, *extra)
86+
else:
87+
raise ValueError("Unrecognized command")
8788

8889

8990
if __name__ == "__main__":

tests/test_main.py

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,30 @@ def test_main_user_create(mock_commands_user):
2828

2929
mock_commands_user.assert_called_once()
3030
call_args = mock_commands_user.call_args.args
31-
assert Namespace(command="user", subcommand="create", username="username", notify=None) in call_args
31+
assert (
32+
Namespace(func=mock_commands_user, command="user", subcommand="create", username="username", notify=None) in call_args
33+
)
3234

3335

3436
def test_main_user_create_notify(mock_commands_user):
3537
main(argv=["user", "create", "username", "--notify", "notification"])
3638

3739
mock_commands_user.assert_called_once()
3840
call_args = mock_commands_user.call_args.args
39-
assert Namespace(command="user", subcommand="create", username="username", notify="notification") in call_args
41+
assert (
42+
Namespace(func=mock_commands_user, command="user", subcommand="create", username="username", notify="notification")
43+
in call_args
44+
)
4045

4146

4247
def test_main_user_create_extras(mock_commands_user):
4348
main(argv=["user", "create", "username", "extra1", "extra2"])
4449

4550
mock_commands_user.assert_called_once()
4651
call_args = mock_commands_user.call_args.args
47-
assert Namespace(command="user", subcommand="create", username="username", notify=None) in call_args
52+
assert (
53+
Namespace(func=mock_commands_user, command="user", subcommand="create", username="username", notify=None) in call_args
54+
)
4855
assert "extra1" in call_args
4956
assert "extra2" in call_args
5057

@@ -60,7 +67,12 @@ def test_main_user_convert(mock_commands_user):
6067

6168
mock_commands_user.assert_called_once()
6269
call_args = mock_commands_user.call_args.args
63-
assert Namespace(command="user", subcommand="convert", username="username", account_type="contractor") in call_args
70+
assert (
71+
Namespace(
72+
func=mock_commands_user, command="user", subcommand="convert", username="username", account_type="contractor"
73+
)
74+
in call_args
75+
)
6476

6577

6678
def test_main_user_convert_no_username(mock_commands_user):
@@ -80,15 +92,19 @@ def test_main_user_delete(mock_commands_user):
8092

8193
mock_commands_user.assert_called_once()
8294
call_args = mock_commands_user.call_args.args
83-
assert Namespace(command="user", subcommand="delete", username="username", force=False) in call_args
95+
assert (
96+
Namespace(func=mock_commands_user, command="user", subcommand="delete", username="username", force=False) in call_args
97+
)
8498

8599

86100
def test_main_user_delete_force(mock_commands_user):
87101
main(argv=["user", "delete", "username", "--force"])
88102

89103
mock_commands_user.assert_called_once()
90104
call_args = mock_commands_user.call_args.args
91-
assert Namespace(command="user", subcommand="delete", username="username", force=True) in call_args
105+
assert (
106+
Namespace(func=mock_commands_user, command="user", subcommand="delete", username="username", force=True) in call_args
107+
)
92108

93109

94110
def test_main_user_delete_no_username(mock_commands_user):
@@ -114,23 +130,23 @@ def test_main_init_default(mock_commands_init):
114130

115131
mock_commands_init.assert_called_once()
116132
call_args = mock_commands_init.call_args.args
117-
assert Namespace(command="init", username="username", gam=False, gyb=False) in call_args
133+
assert Namespace(func=mock_commands_init, command="init", username="username", gam=False, gyb=False) in call_args
118134

119135

120136
def test_main_init_gam(mock_commands_init):
121137
main(argv=["init", "username", "--gam"])
122138

123139
mock_commands_init.assert_called_once()
124140
call_args = mock_commands_init.call_args.args
125-
assert Namespace(command="init", username="username", gam=True, gyb=False) in call_args
141+
assert Namespace(func=mock_commands_init, command="init", username="username", gam=True, gyb=False) in call_args
126142

127143

128144
def test_main_init_gyb(mock_commands_init):
129145
main(argv=["init", "username", "--gyb"])
130146

131147
mock_commands_init.assert_called_once()
132148
call_args = mock_commands_init.call_args.args
133-
assert Namespace(command="init", username="username", gam=False, gyb=True) in call_args
149+
assert Namespace(func=mock_commands_init, command="init", username="username", gam=False, gyb=True) in call_args
134150

135151

136152
def test_main_init_no_username(mock_commands_init):
@@ -144,23 +160,39 @@ def test_main_user_offboard(mock_commands_user):
144160

145161
mock_commands_user.assert_called_once()
146162
call_args = mock_commands_user.call_args.args
147-
assert Namespace(command="user", subcommand="offboard", username="username", alias=None, force=False) in call_args
163+
assert (
164+
Namespace(func=mock_commands_user, command="user", subcommand="offboard", username="username", alias=None, force=False)
165+
in call_args
166+
)
148167

149168

150169
def test_main_user_offboard_force(mock_commands_user):
151170
main(argv=["user", "offboard", "username", "--force"])
152171

153172
mock_commands_user.assert_called_once()
154173
call_args = mock_commands_user.call_args.args
155-
assert Namespace(command="user", subcommand="offboard", username="username", alias=None, force=True) in call_args
174+
assert (
175+
Namespace(func=mock_commands_user, command="user", subcommand="offboard", username="username", alias=None, force=True)
176+
in call_args
177+
)
156178

157179

158180
def test_main_user_offboard_with_alias(mock_commands_user):
159181
main(argv=["user", "offboard", "username", "--alias", "anotheruser"])
160182

161183
mock_commands_user.assert_called_once()
162184
call_args = mock_commands_user.call_args.args
163-
assert Namespace(command="user", subcommand="offboard", username="username", alias="anotheruser", force=False) in call_args
185+
assert (
186+
Namespace(
187+
func=mock_commands_user,
188+
command="user",
189+
subcommand="offboard",
190+
username="username",
191+
alias="anotheruser",
192+
force=False,
193+
)
194+
in call_args
195+
)
164196

165197

166198
def test_main_user_offboard_no_username(mock_commands_user):
@@ -174,15 +206,23 @@ def test_main_user_reset_password(mock_commands_user):
174206

175207
mock_commands_user.assert_called_once()
176208
call_args = mock_commands_user.call_args.args
177-
assert Namespace(command="user", subcommand="reset-password", username="username", notify=None) in call_args
209+
assert (
210+
Namespace(func=mock_commands_user, command="user", subcommand="reset-password", username="username", notify=None)
211+
in call_args
212+
)
178213

179214

180215
def test_main_user_reset_password_notify(mock_commands_user):
181216
main(argv=["user", "reset-password", "username", "--notify", "notification"])
182217

183218
mock_commands_user.assert_called_once()
184219
call_args = mock_commands_user.call_args.args
185-
assert Namespace(command="user", subcommand="reset-password", username="username", notify="notification") in call_args
220+
assert (
221+
Namespace(
222+
func=mock_commands_user, command="user", subcommand="reset-password", username="username", notify="notification"
223+
)
224+
in call_args
225+
)
186226

187227

188228
def test_main_user_reset_password_no_username(mock_commands_user):
@@ -196,7 +236,7 @@ def test_main_user_restore(mock_commands_user):
196236

197237
mock_commands_user.assert_called_once()
198238
call_args = mock_commands_user.call_args.args
199-
assert Namespace(command="user", subcommand="restore", username="username") in call_args
239+
assert Namespace(func=mock_commands_user, command="user", subcommand="restore", username="username") in call_args
200240

201241

202242
def test_main_user_restore_no_username(mock_commands_user):
@@ -210,15 +250,19 @@ def test_main_user_signout(mock_commands_user):
210250

211251
mock_commands_user.assert_called_once()
212252
call_args = mock_commands_user.call_args.args
213-
assert Namespace(command="user", subcommand="signout", username="username", force=False) in call_args
253+
assert (
254+
Namespace(func=mock_commands_user, command="user", subcommand="signout", username="username", force=False) in call_args
255+
)
214256

215257

216258
def test_main_user_signout_force(mock_commands_user):
217259
main(argv=["user", "signout", "username", "--force"])
218260

219261
mock_commands_user.assert_called_once()
220262
call_args = mock_commands_user.call_args.args
221-
assert Namespace(command="user", subcommand="signout", username="username", force=True) in call_args
263+
assert (
264+
Namespace(func=mock_commands_user, command="user", subcommand="signout", username="username", force=True) in call_args
265+
)
222266

223267

224268
def test_main_user_signout_no_username(mock_commands_user):

0 commit comments

Comments
 (0)