Skip to content

Commit 3228a88

Browse files
committed
refactor(main): individual command setup funcs
1 parent 103ed7e commit 3228a88

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

compiler_admin/main.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,28 @@ def add_sub_cmd(cmd: _SubParsersAction, subcmd, help) -> ArgumentParser:
1919
return cmd.add_parser(subcmd, help=help)
2020

2121

22-
def add_sub_cmd_username(cmd: _SubParsersAction, subcmd, help) -> ArgumentParser:
22+
def add_sub_cmd_with_username_arg(cmd: _SubParsersAction, subcmd, help) -> ArgumentParser:
2323
"""Helper creates a new subcommand parser with a required username arg."""
24-
return add_username_arg(add_sub_cmd(cmd, subcmd, help=help))
24+
sub_cmd = add_sub_cmd(cmd, subcmd, help=help)
25+
sub_cmd.add_argument("username", help="A Compiler user account name, sans domain.")
26+
return sub_cmd
2527

2628

27-
def add_username_arg(cmd: ArgumentParser) -> ArgumentParser:
28-
cmd.add_argument("username", help="A Compiler user account name, sans domain.")
29-
return cmd
30-
31-
32-
def main(argv=None):
33-
argv = argv if argv is not None else sys.argv[1:]
34-
parser = ArgumentParser(prog="compiler-admin")
35-
36-
# https://stackoverflow.com/a/8521644/812183
37-
parser.add_argument(
38-
"-v",
39-
"--version",
40-
action="version",
41-
version=f"%(prog)s {version}",
42-
)
43-
44-
cmd_parsers = add_sub_cmd_parser(parser, dest="command", help="The command to run")
45-
29+
def setup_info_command(cmd_parsers: _SubParsersAction):
4630
info_cmd = add_sub_cmd(cmd_parsers, "info", help="Print configuration and debugging information.")
4731
info_cmd.set_defaults(func=info)
4832

49-
init_cmd = add_sub_cmd_username(
33+
34+
def setup_init_command(cmd_parsers: _SubParsersAction):
35+
init_cmd = add_sub_cmd_with_username_arg(
5036
cmd_parsers, "init", help="Initialize a new admin project. This command should be run once before any others."
5137
)
5238
init_cmd.add_argument("--gam", action="store_true", help="If provided, initialize a new GAM project.")
5339
init_cmd.add_argument("--gyb", action="store_true", help="If provided, initialize a new GYB project.")
5440
init_cmd.set_defaults(func=init)
5541

42+
43+
def setup_time_command(cmd_parsers: _SubParsersAction):
5644
time_cmd = add_sub_cmd(cmd_parsers, "time", help="Work with Compiler time entries")
5745
time_cmd.set_defaults(func=time)
5846
time_subcmds = add_sub_cmd_parser(time_cmd, help="The time command to run.")
@@ -66,35 +54,56 @@ def main(argv=None):
6654
)
6755
time_convert.add_argument("--client", default=None, help="The name of the client to use in converted data.")
6856

57+
58+
def setup_user_command(cmd_parsers: _SubParsersAction):
6959
user_cmd = add_sub_cmd(cmd_parsers, "user", help="Work with users in the Compiler org.")
7060
user_cmd.set_defaults(func=user)
7161
user_subcmds = add_sub_cmd_parser(user_cmd, help="The user command to run.")
7262

73-
user_create = add_sub_cmd_username(user_subcmds, "create", help="Create a new user in the Compiler domain.")
63+
user_create = add_sub_cmd_with_username_arg(user_subcmds, "create", help="Create a new user in the Compiler domain.")
7464
user_create.add_argument("--notify", help="An email address to send the newly created account info.")
7565

76-
user_convert = add_sub_cmd_username(user_subcmds, "convert", help="Convert a user account to a new type.")
66+
user_convert = add_sub_cmd_with_username_arg(user_subcmds, "convert", help="Convert a user account to a new type.")
7767
user_convert.add_argument("account_type", choices=ACCOUNT_TYPE_OU.keys(), help="Target account type for this conversion.")
7868

79-
user_delete = add_sub_cmd_username(user_subcmds, "delete", help="Delete a user account.")
69+
user_delete = add_sub_cmd_with_username_arg(user_subcmds, "delete", help="Delete a user account.")
8070
user_delete.add_argument("--force", action="store_true", default=False, help="Don't ask for confirmation before deletion.")
8171

82-
user_offboard = add_sub_cmd_username(user_subcmds, "offboard", help="Offboard a user account.")
72+
user_offboard = add_sub_cmd_with_username_arg(user_subcmds, "offboard", help="Offboard a user account.")
8373
user_offboard.add_argument("--alias", help="Account to assign username as an alias.")
8474
user_offboard.add_argument(
8575
"--force", action="store_true", default=False, help="Don't ask for confirmation before offboarding."
8676
)
8777

88-
user_reset = add_sub_cmd_username(
78+
user_reset = add_sub_cmd_with_username_arg(
8979
user_subcmds, "reset-password", help="Reset a user's password to a randomly generated string."
9080
)
9181
user_reset.add_argument("--notify", help="An email address to send the newly generated password.")
9282

93-
add_sub_cmd_username(user_subcmds, "restore", help="Restore an email backup from a prior offboarding.")
83+
add_sub_cmd_with_username_arg(user_subcmds, "restore", help="Restore an email backup from a prior offboarding.")
9484

95-
user_signout = add_sub_cmd_username(user_subcmds, "signout", help="Signs a user out from all active sessions.")
85+
user_signout = add_sub_cmd_with_username_arg(user_subcmds, "signout", help="Signs a user out from all active sessions.")
9686
user_signout.add_argument("--force", action="store_true", default=False, help="Don't ask for confirmation before signout.")
9787

88+
89+
def main(argv=None):
90+
argv = argv if argv is not None else sys.argv[1:]
91+
parser = ArgumentParser(prog="compiler-admin")
92+
93+
# https://stackoverflow.com/a/8521644/812183
94+
parser.add_argument(
95+
"-v",
96+
"--version",
97+
action="version",
98+
version=f"%(prog)s {version}",
99+
)
100+
101+
cmd_parsers = add_sub_cmd_parser(parser, dest="command", help="The command to run")
102+
setup_info_command(cmd_parsers)
103+
setup_init_command(cmd_parsers)
104+
setup_time_command(cmd_parsers)
105+
setup_user_command(cmd_parsers)
106+
98107
if len(argv) == 0:
99108
argv = ["info"]
100109

0 commit comments

Comments
 (0)