forked from clan/clan-core
Merge pull request 'cli' (#13) from cli into main
Reviewed-on: clan/clan-core#13
This commit is contained in:
commit
9c81da4c93
@ -2,25 +2,28 @@
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from . import admin
|
||||
|
||||
has_argcomplete = True
|
||||
try:
|
||||
import argcomplete
|
||||
except ImportError:
|
||||
has_argcomplete = False
|
||||
|
||||
import clan_admin
|
||||
|
||||
|
||||
# this will be the entrypoint under /bin/clan (see pyproject.toml config)
|
||||
def clan() -> None:
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="cLAN tool")
|
||||
subparsers = parser.add_subparsers()
|
||||
|
||||
# init clan admin
|
||||
parser_admin = subparsers.add_parser("admin")
|
||||
clan_admin.make_parser(parser_admin)
|
||||
admin.register_parser(parser_admin)
|
||||
if has_argcomplete:
|
||||
argcomplete.autocomplete(parser)
|
||||
parser.parse_args()
|
||||
if len(sys.argv) == 1:
|
||||
parser.print_help()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -34,7 +34,7 @@ def edit(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
)
|
||||
|
||||
|
||||
def rebuild(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
def rebuild(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
# TODO get clients from zerotier cli?
|
||||
if args.host:
|
||||
print(f"would redeploy {args.host} from clan {args.folder}")
|
||||
@ -42,7 +42,7 @@ def rebuild(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
print(f"would redeploy all hosts from clan {args.folder}")
|
||||
|
||||
|
||||
def destroy(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
def destroy(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
# TODO get clan folder & hosts from somwhere (maybe ~/.config/clan/$name /)
|
||||
# send some kind of kill signal, then remove the folder
|
||||
if args.yes:
|
||||
@ -53,24 +53,26 @@ def destroy(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
)
|
||||
|
||||
|
||||
def backup(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
def backup(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
if args.host:
|
||||
print(f"would backup {args.host} from clan {args.folder}")
|
||||
else:
|
||||
print(f"would backup all hosts from clan {args.folder}")
|
||||
|
||||
|
||||
def git(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
def git(args: argparse.Namespace) -> None: # pragma: no cover
|
||||
subprocess.Popen(
|
||||
[
|
||||
"git",
|
||||
"-C",
|
||||
args.folder,
|
||||
] + args.git_args
|
||||
]
|
||||
+ args.git_args
|
||||
)
|
||||
|
||||
|
||||
# takes a (sub)parser and configures it
|
||||
def make_parser(parser: argparse.ArgumentParser) -> None:
|
||||
def register_parser(parser: argparse.ArgumentParser) -> None:
|
||||
parser.add_argument(
|
||||
"-f",
|
||||
"--folder",
|
||||
@ -107,7 +109,8 @@ def make_parser(parser: argparse.ArgumentParser) -> None:
|
||||
parser_destroy.set_defaults(func=destroy)
|
||||
|
||||
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(
|
||||
"--host", help="specify single host to rebuild", default=None
|
||||
@ -117,13 +120,3 @@ def make_parser(parser: argparse.ArgumentParser) -> None:
|
||||
parser_git = subparser.add_parser("git", help="control the clan repo via git")
|
||||
parser_git.add_argument("git_args", nargs="*")
|
||||
parser_git.set_defaults(func=git)
|
||||
|
||||
|
||||
def clan_admin() -> None: # pragma: no cover
|
||||
parser = argparse.ArgumentParser(description="clan-admin")
|
||||
args = parser.parse_args()
|
||||
args.func(args)
|
||||
|
||||
# entry point if this file is executed directly
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
clan_admin()
|
@ -2,14 +2,11 @@
|
||||
requires = ["setuptools"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
include = ["clan.py"]
|
||||
|
||||
[project]
|
||||
name = "clan"
|
||||
description = "cLAN CLI tool"
|
||||
dynamic = ["version"]
|
||||
scripts = {clan = "clan:clan"}
|
||||
scripts = {clan = "clan_cli:main"}
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
addopts = "--cov . --cov-report term --cov-fail-under=100 --no-cov-on-fail"
|
||||
|
@ -7,6 +7,7 @@ def test_make_parser():
|
||||
parser = argparse.ArgumentParser()
|
||||
clan_admin.make_parser(parser)
|
||||
|
||||
|
||||
# using fp fixture from pytest-subprocess
|
||||
def test_create(fp):
|
||||
cmd = ["nix", "flake", "init", "-t", fp.any()]
|
||||
@ -14,4 +15,3 @@ def test_create(fp):
|
||||
args = argparse.Namespace(folder="./my-clan")
|
||||
clan_admin.create(args)
|
||||
assert fp.call_count(cmd) == 1
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
import clan
|
||||
import pytest
|
||||
|
||||
|
||||
def test_no_args(capsys):
|
||||
|
Loading…
Reference in New Issue
Block a user