Skip to content

Commit

Permalink
refactor(main): individual command setup funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman committed Apr 24, 2024
1 parent 5f1bf4b commit c97c9e4
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions compiler_admin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,28 @@ def add_sub_cmd(cmd: _SubParsersAction, subcmd, help) -> ArgumentParser:
return cmd.add_parser(subcmd, help=help)


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


def add_username_arg(cmd: ArgumentParser) -> ArgumentParser:
cmd.add_argument("username", help="A Compiler user account name, sans domain.")
return cmd


def main(argv=None):
argv = argv if argv is not None else sys.argv[1:]
parser = ArgumentParser(prog="compiler-admin")

# https://stackoverflow.com/a/8521644/812183
parser.add_argument(
"-v",
"--version",
action="version",
version=f"%(prog)s {version}",
)

cmd_parsers = add_sub_cmd_parser(parser, dest="command", help="The command to run")

def setup_info_command(cmd_parsers: _SubParsersAction):
info_cmd = add_sub_cmd(cmd_parsers, "info", help="Print configuration and debugging information.")
info_cmd.set_defaults(func=info)

init_cmd = add_sub_cmd_username(

def setup_init_command(cmd_parsers: _SubParsersAction):
init_cmd = add_sub_cmd_with_username_arg(
cmd_parsers, "init", help="Initialize a new admin project. This command should be run once before any others."
)
init_cmd.add_argument("--gam", action="store_true", help="If provided, initialize a new GAM project.")
init_cmd.add_argument("--gyb", action="store_true", help="If provided, initialize a new GYB project.")
init_cmd.set_defaults(func=init)


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


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

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

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

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

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

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

add_sub_cmd_username(user_subcmds, "restore", help="Restore an email backup from a prior offboarding.")
add_sub_cmd_with_username_arg(user_subcmds, "restore", help="Restore an email backup from a prior offboarding.")

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


def main(argv=None):
argv = argv if argv is not None else sys.argv[1:]
parser = ArgumentParser(prog="compiler-admin")

# https://stackoverflow.com/a/8521644/812183
parser.add_argument(
"-v",
"--version",
action="version",
version=f"%(prog)s {version}",
)

cmd_parsers = add_sub_cmd_parser(parser, dest="command", help="The command to run")
setup_info_command(cmd_parsers)
setup_init_command(cmd_parsers)
setup_time_command(cmd_parsers)
setup_user_command(cmd_parsers)

if len(argv) == 0:
argv = ["info"]

Expand Down

0 comments on commit c97c9e4

Please sign in to comment.