clan-cli: manage sub-commands via python argparse
This commit is contained in:
@@ -1,24 +1,15 @@
|
|||||||
# !/usr/bin/env python3
|
# !/usr/bin/env python3
|
||||||
import subprocess
|
import argparse
|
||||||
import sys
|
|
||||||
|
import clan_admin
|
||||||
|
|
||||||
|
|
||||||
def showhelp() -> None:
|
# this will be the entrypoint under /bin/clan (see pyproject.toml config)
|
||||||
print('''
|
def clan() -> None:
|
||||||
usage:
|
parser = argparse.ArgumentParser(description="cLAN tool")
|
||||||
clan admin ...
|
subparsers = parser.add_subparsers()
|
||||||
clan join ...
|
|
||||||
clan delete ...
|
|
||||||
''')
|
|
||||||
|
|
||||||
|
# init clan admin
|
||||||
|
parser_admin = subparsers.add_parser("admin")
|
||||||
|
clan_admin.make_parser(parser_admin)
|
||||||
|
|
||||||
try:
|
|
||||||
cmd = f'clan-{sys.argv[1]}'
|
|
||||||
except: # noqa
|
|
||||||
showhelp()
|
|
||||||
|
|
||||||
try:
|
|
||||||
subprocess.Popen([cmd] + sys.argv[2:])
|
|
||||||
except FileNotFoundError:
|
|
||||||
print(f'command {cmd} not found')
|
|
||||||
exit(2)
|
|
||||||
|
|||||||
@@ -69,54 +69,58 @@ def git(args: argparse.Namespace) -> None:
|
|||||||
] + args.git_args
|
] + args.git_args
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# takes a (sub)parser and configures it
|
||||||
parser = argparse.ArgumentParser(description="clan-admin")
|
def make_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-f",
|
"-f",
|
||||||
"--folder",
|
"--folder",
|
||||||
help="the folder where the clan is defined, default to the current folder",
|
help="the folder where the clan is defined, default to the current folder",
|
||||||
default=os.environ["PWD"],
|
default=os.environ["PWD"],
|
||||||
)
|
)
|
||||||
subparser = parser.add_subparsers(
|
subparser = parser.add_subparsers(
|
||||||
title="command",
|
title="command",
|
||||||
description="the command to run",
|
description="the command to run",
|
||||||
help="the command to run",
|
help="the command to run",
|
||||||
required=True,
|
required=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
parser_create = subparser.add_parser("create", help="create a new clan")
|
parser_create = subparser.add_parser("create", help="create a new clan")
|
||||||
parser_create.set_defaults(func=create)
|
parser_create.set_defaults(func=create)
|
||||||
|
|
||||||
parser_edit = subparser.add_parser("edit", help="edit a clan")
|
parser_edit = subparser.add_parser("edit", help="edit a clan")
|
||||||
parser_edit.set_defaults(func=edit)
|
parser_edit.set_defaults(func=edit)
|
||||||
|
|
||||||
parser_rebuild = subparser.add_parser(
|
parser_rebuild = subparser.add_parser(
|
||||||
"rebuild", help="build configuration of a clan and push it to the target"
|
"rebuild", help="build configuration of a clan and push it to the target"
|
||||||
)
|
)
|
||||||
parser_rebuild.add_argument(
|
parser_rebuild.add_argument(
|
||||||
"--host", help="specify single host to rebuild", default=None
|
"--host", help="specify single host to rebuild", default=None
|
||||||
)
|
)
|
||||||
parser_rebuild.set_defaults(func=rebuild)
|
parser_rebuild.set_defaults(func=rebuild)
|
||||||
|
|
||||||
parser_destroy = subparser.add_parser(
|
parser_destroy = subparser.add_parser(
|
||||||
"destroy", help="destroy a clan, including all the machines"
|
"destroy", help="destroy a clan, including all the machines"
|
||||||
)
|
)
|
||||||
parser_destroy.add_argument(
|
parser_destroy.add_argument(
|
||||||
"--yes", help="specify single host to rebuild", action="store_true"
|
"--yes", help="specify single host to rebuild", action="store_true"
|
||||||
)
|
)
|
||||||
parser_destroy.set_defaults(func=destroy)
|
parser_destroy.set_defaults(func=destroy)
|
||||||
|
|
||||||
parser_backup = subparser.add_parser(
|
parser_backup = subparser.add_parser(
|
||||||
"backup", help="backup all the state of all machines in a clan or just a single one"
|
"backup", help="backup all the state of all machines in a clan or just a single one"
|
||||||
)
|
)
|
||||||
parser_backup.add_argument(
|
parser_backup.add_argument(
|
||||||
"--host", help="specify single host to rebuild", default=None
|
"--host", help="specify single host to rebuild", default=None
|
||||||
)
|
)
|
||||||
parser_backup.set_defaults(func=backup)
|
parser_backup.set_defaults(func=backup)
|
||||||
|
|
||||||
parser_git = subparser.add_parser("git", help="control the clan repo via git")
|
parser_git = subparser.add_parser("git", help="control the clan repo via git")
|
||||||
parser_git.add_argument("git_args", nargs="*")
|
parser_git.add_argument("git_args", nargs="*")
|
||||||
parser_git.set_defaults(func=git)
|
parser_git.set_defaults(func=git)
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
args.func(args)
|
# entry point if this file is executed directly
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description="clan-admin")
|
||||||
|
args = parser.parse_args()
|
||||||
|
args.func(args)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ include = ["clan.py"]
|
|||||||
name = "clan"
|
name = "clan"
|
||||||
description = "cLAN CLI tool"
|
description = "cLAN CLI tool"
|
||||||
dynamic = ["version"]
|
dynamic = ["version"]
|
||||||
scripts = {clan = "clan:my_cli"}
|
scripts = {clan = "clan:clan"}
|
||||||
|
|
||||||
[tool.pytest.ini_options]
|
[tool.pytest.ini_options]
|
||||||
addopts = "--cov . --cov-report term --cov-fail-under=100 --no-cov-on-fail"
|
addopts = "--cov . --cov-report term --cov-fail-under=100 --no-cov-on-fail"
|
||||||
|
|||||||
Reference in New Issue
Block a user