1- import argparse
1+ from argparse import ArgumentParser , _SubParsersAction
22import sys
33
44from compiler_admin import __version__ as version
5- from compiler_admin .commands .create import create
6- from compiler_admin .commands .convert import ACCOUNT_TYPE_OU , convert
7- from compiler_admin .commands .delete import delete
85from compiler_admin .commands .info import info
96from compiler_admin .commands .init import init
10- from compiler_admin .commands .offboard import offboard
11- from compiler_admin .commands .reset_password import reset_password
12- from compiler_admin .commands .restore import restore
13- from compiler_admin .commands .signout import signout
7+ from compiler_admin .commands .user import user
8+ from compiler_admin .commands .user .convert import ACCOUNT_TYPE_OU
9+
10+
11+ def add_sub_cmd (cmd : _SubParsersAction , subcmd , help ) -> ArgumentParser :
12+ """Helper creates a new subcommand parser."""
13+ return cmd .add_parser (subcmd , help = help )
14+
15+
16+ def add_sub_cmd_username (cmd : _SubParsersAction , subcmd , help ) -> ArgumentParser :
17+ """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
1424
1525
1626def main (argv = None ):
1727 argv = argv if argv is not None else sys .argv [1 :]
18- parser = argparse . ArgumentParser (prog = "compiler-admin" )
28+ parser = ArgumentParser (prog = "compiler-admin" )
1929
2030 # https://stackoverflow.com/a/8521644/812183
2131 parser .add_argument (
@@ -25,52 +35,43 @@ def main(argv=None):
2535 version = f"%(prog)s { version } " ,
2636 )
2737
28- subparsers = parser .add_subparsers (dest = "command" )
38+ cmd_parsers = parser .add_subparsers (dest = "command" , help = "The command to run " )
2939
30- def _subcmd (name , help , add_username_arg = True ) -> argparse .ArgumentParser :
31- """Helper creates a new subcommand parser."""
32- parser = subparsers .add_parser (name , help = help )
33- if add_username_arg is True :
34- parser .add_argument ("username" , help = "A Compiler user account name, sans domain." )
35- return parser
40+ cmd_parsers .add_parser ("info" , help = "Print configuration and debugging information." )
3641
37- _subcmd ("info" , help = "Print configuration and debugging information." , add_username_arg = False )
38-
39- init_parser = _subcmd (
40- "init" ,
41- help = "Initialize a new admin project. This command should be run once before any others." ,
42+ init_cmd = add_sub_cmd_username (
43+ cmd_parsers , "init" , help = "Initialize a new admin project. This command should be run once before any others."
4244 )
43- init_parser .add_argument ("--gam" , action = "store_true" , help = "If provided, initialize a new GAM project." )
44- init_parser .add_argument ("--gyb" , action = "store_true" , help = "If provided, initialize a new GYB project." )
45+ init_cmd .add_argument ("--gam" , action = "store_true" , help = "If provided, initialize a new GAM project." )
46+ init_cmd .add_argument ("--gyb" , action = "store_true" , help = "If provided, initialize a new GYB project." )
4547
46- create_parser = _subcmd ( "create " , help = "Create a new user in the Compiler domain ." )
47- create_parser . add_argument ( "--notify " , help = "An email address to send the newly created account info ." )
48+ user_cmd = add_sub_cmd ( cmd_parsers , "user " , help = "Work with users in the Compiler org ." )
49+ user_subcmds = user_cmd . add_subparsers ( dest = "subcommand " , help = "The user command to run ." )
4850
49- convert_parser = _subcmd ("convert" , help = "Convert a user account to a new type." )
50- convert_parser .add_argument (
51- "account_type" , choices = ACCOUNT_TYPE_OU .keys (), help = "Target account type for this conversion."
52- )
51+ user_create = add_sub_cmd_username (user_subcmds , "create" , help = "Create a new user in the Compiler domain." )
52+ user_create .add_argument ("--notify" , help = "An email address to send the newly created account info." )
5353
54- delete_parser = _subcmd ("delete" , help = "Delete a user account." )
55- delete_parser .add_argument (
56- "--force" , action = "store_true" , default = False , help = "Don't ask for confirmation before deletion."
57- )
54+ user_convert = add_sub_cmd_username (user_subcmds , "convert" , help = "Convert a user account to a new type." )
55+ user_convert .add_argument ("account_type" , choices = ACCOUNT_TYPE_OU .keys (), help = "Target account type for this conversion." )
5856
59- offboard_parser = _subcmd ("offboard" , help = "Offboard a user account." )
60- offboard_parser .add_argument ("--alias" , help = "Account to assign username as an alias." )
61- offboard_parser .add_argument (
57+ user_delete = add_sub_cmd_username (user_subcmds , "delete" , help = "Delete a user account." )
58+ user_delete .add_argument ("--force" , action = "store_true" , default = False , help = "Don't ask for confirmation before deletion." )
59+
60+ user_offboard = add_sub_cmd_username (user_subcmds , "offboard" , help = "Offboard a user account." )
61+ user_offboard .add_argument ("--alias" , help = "Account to assign username as an alias." )
62+ user_offboard .add_argument (
6263 "--force" , action = "store_true" , default = False , help = "Don't ask for confirmation before offboarding."
6364 )
6465
65- reset_parser = _subcmd ("reset-password" , help = "Reset a user's password to a randomly generated string." )
66- reset_parser .add_argument ("--notify" , help = "An email address to send the newly generated password." )
66+ user_reset = add_sub_cmd_username (
67+ user_subcmds , "reset-password" , help = "Reset a user's password to a randomly generated string."
68+ )
69+ user_reset .add_argument ("--notify" , help = "An email address to send the newly generated password." )
6770
68- _subcmd ( "restore" , help = "Restore an email backup from a prior offboarding." )
71+ add_sub_cmd_username ( user_subcmds , "restore" , help = "Restore an email backup from a prior offboarding." )
6972
70- signout_parser = _subcmd ("signout" , help = "Signs a user out from all active sessions." )
71- signout_parser .add_argument (
72- "--force" , action = "store_true" , default = False , help = "Don't ask for confirmation before signout."
73- )
73+ user_signout = add_sub_cmd_username (user_subcmds , "signout" , help = "Signs a user out from all active sessions." )
74+ user_signout .add_argument ("--force" , action = "store_true" , default = False , help = "Don't ask for confirmation before signout." )
7475
7576 if len (argv ) == 0 :
7677 argv = ["info" ]
@@ -79,22 +80,10 @@ def _subcmd(name, help, add_username_arg=True) -> argparse.ArgumentParser:
7980
8081 if args .command == "info" :
8182 return info ()
82- elif args .command == "create" :
83- return create (args , * extra )
84- elif args .command == "convert" :
85- return convert (args )
86- elif args .command == "delete" :
87- return delete (args )
88- elif args .command == "init" :
83+ if args .command == "init" :
8984 return init (args )
90- elif args .command == "offboard" :
91- return offboard (args )
92- elif args .command == "restore" :
93- return restore (args )
94- elif args .command == "reset-password" :
95- return reset_password (args )
96- elif args .command == "signout" :
97- return signout (args )
85+ elif args .command == "user" :
86+ return user (args , * extra )
9887
9988
10089if __name__ == "__main__" :
0 commit comments