|
4 | 4 | from compiler_admin import __version__ as version
|
5 | 5 | from compiler_admin.commands.info import info
|
6 | 6 | from compiler_admin.commands.init import init
|
| 7 | +from compiler_admin.commands.time import time |
7 | 8 | from compiler_admin.commands.user import user
|
8 | 9 | from compiler_admin.commands.user.convert import ACCOUNT_TYPE_OU
|
9 | 10 |
|
10 | 11 |
|
| 12 | +def add_sub_cmd_parser(parser: ArgumentParser, dest="subcommand", help=None): |
| 13 | + """Helper adds a subparser for the given dest.""" |
| 14 | + return parser.add_subparsers(dest=dest, help=help) |
| 15 | + |
| 16 | + |
11 | 17 | def add_sub_cmd(cmd: _SubParsersAction, subcmd, help) -> ArgumentParser:
|
12 | 18 | """Helper creates a new subcommand parser."""
|
13 | 19 | return cmd.add_parser(subcmd, help=help)
|
14 | 20 |
|
15 | 21 |
|
16 |
| -def add_sub_cmd_username(cmd: _SubParsersAction, subcmd, help) -> ArgumentParser: |
| 22 | +def add_sub_cmd_with_username_arg(cmd: _SubParsersAction, subcmd, help) -> ArgumentParser: |
17 | 23 | """Helper creates a new subcommand parser with a required username arg."""
|
18 |
| - return add_username_arg(add_sub_cmd(cmd, subcmd, help=help)) |
19 |
| - |
20 |
| - |
21 |
| -def add_username_arg(cmd: ArgumentParser) -> ArgumentParser: |
22 |
| - cmd.add_argument("username", help="A Compiler user account name, sans domain.") |
23 |
| - return cmd |
24 |
| - |
| 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 |
25 | 27 |
|
26 |
| -def main(argv=None): |
27 |
| - argv = argv if argv is not None else sys.argv[1:] |
28 |
| - parser = ArgumentParser(prog="compiler-admin") |
29 |
| - |
30 |
| - # https://stackoverflow.com/a/8521644/812183 |
31 |
| - parser.add_argument( |
32 |
| - "-v", |
33 |
| - "--version", |
34 |
| - action="version", |
35 |
| - version=f"%(prog)s {version}", |
36 |
| - ) |
37 |
| - |
38 |
| - cmd_parsers = parser.add_subparsers(dest="command", help="The command to run") |
39 | 28 |
|
| 29 | +def setup_info_command(cmd_parsers: _SubParsersAction): |
40 | 30 | info_cmd = add_sub_cmd(cmd_parsers, "info", help="Print configuration and debugging information.")
|
41 | 31 | info_cmd.set_defaults(func=info)
|
42 | 32 |
|
43 |
| - init_cmd = add_sub_cmd_username( |
| 33 | + |
| 34 | +def setup_init_command(cmd_parsers: _SubParsersAction): |
| 35 | + init_cmd = add_sub_cmd_with_username_arg( |
44 | 36 | cmd_parsers, "init", help="Initialize a new admin project. This command should be run once before any others."
|
45 | 37 | )
|
46 | 38 | init_cmd.add_argument("--gam", action="store_true", help="If provided, initialize a new GAM project.")
|
47 | 39 | init_cmd.add_argument("--gyb", action="store_true", help="If provided, initialize a new GYB project.")
|
48 | 40 | init_cmd.set_defaults(func=init)
|
49 | 41 |
|
| 42 | + |
| 43 | +def setup_time_command(cmd_parsers: _SubParsersAction): |
| 44 | + time_cmd = add_sub_cmd(cmd_parsers, "time", help="Work with Compiler time entries.") |
| 45 | + time_cmd.set_defaults(func=time) |
| 46 | + time_subcmds = add_sub_cmd_parser(time_cmd, help="The time command to run.") |
| 47 | + |
| 48 | + time_convert = add_sub_cmd(time_subcmds, "convert", help="Convert a time report from one format into another.") |
| 49 | + time_convert.add_argument( |
| 50 | + "--input", default=sys.stdin, help="The path to the source data for conversion. Defaults to stdin." |
| 51 | + ) |
| 52 | + time_convert.add_argument( |
| 53 | + "--output", default=sys.stdout, help="The path to the file where converted data should be written. Defaults to stdout." |
| 54 | + ) |
| 55 | + time_convert.add_argument("--client", default=None, help="The name of the client to use in converted data.") |
| 56 | + |
| 57 | + |
| 58 | +def setup_user_command(cmd_parsers: _SubParsersAction): |
50 | 59 | user_cmd = add_sub_cmd(cmd_parsers, "user", help="Work with users in the Compiler org.")
|
51 | 60 | user_cmd.set_defaults(func=user)
|
52 |
| - user_subcmds = user_cmd.add_subparsers(dest="subcommand", help="The user command to run.") |
| 61 | + user_subcmds = add_sub_cmd_parser(user_cmd, help="The user command to run.") |
53 | 62 |
|
54 |
| - 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.") |
55 | 64 | user_create.add_argument("--notify", help="An email address to send the newly created account info.")
|
56 | 65 |
|
57 |
| - 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.") |
58 | 67 | user_convert.add_argument("account_type", choices=ACCOUNT_TYPE_OU.keys(), help="Target account type for this conversion.")
|
59 | 68 |
|
60 |
| - 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.") |
61 | 70 | user_delete.add_argument("--force", action="store_true", default=False, help="Don't ask for confirmation before deletion.")
|
62 | 71 |
|
63 |
| - 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.") |
64 | 73 | user_offboard.add_argument("--alias", help="Account to assign username as an alias.")
|
65 | 74 | user_offboard.add_argument(
|
66 | 75 | "--force", action="store_true", default=False, help="Don't ask for confirmation before offboarding."
|
67 | 76 | )
|
68 | 77 |
|
69 |
| - user_reset = add_sub_cmd_username( |
| 78 | + user_reset = add_sub_cmd_with_username_arg( |
70 | 79 | user_subcmds, "reset-password", help="Reset a user's password to a randomly generated string."
|
71 | 80 | )
|
72 | 81 | user_reset.add_argument("--notify", help="An email address to send the newly generated password.")
|
73 | 82 |
|
74 |
| - 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.") |
75 | 84 |
|
76 |
| - 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.") |
77 | 86 | user_signout.add_argument("--force", action="store_true", default=False, help="Don't ask for confirmation before signout.")
|
78 | 87 |
|
| 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 | + |
79 | 107 | if len(argv) == 0:
|
80 | 108 | argv = ["info"]
|
81 | 109 |
|
|
0 commit comments