Merge pull request 'clan/improve/dynamic-completions' (#1557) from kenji/clan-core:clan/improve/dynamic-completions into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/1557
This commit is contained in:
kenji
2024-06-04 13:28:09 +00:00
3 changed files with 30 additions and 10 deletions

View File

@@ -4,6 +4,12 @@ import sys
from pathlib import Path from pathlib import Path
from ..cmd import run from ..cmd import run
from ..completions import (
add_dynamic_completer,
complete_groups,
complete_machines,
complete_users,
)
from ..errors import ClanError from ..errors import ClanError
from ..nix import nix_shell from ..nix import nix_shell
from .secrets import encrypt_secret, sops_secrets_folder from .secrets import encrypt_secret, sops_secrets_folder
@@ -57,27 +63,30 @@ def register_import_sops_parser(parser: argparse.ArgumentParser) -> None:
default=None, default=None,
help="the input type of the sops file (yaml, json, ...). If not specified, it will be guessed from the file extension", help="the input type of the sops file (yaml, json, ...). If not specified, it will be guessed from the file extension",
) )
parser.add_argument( group_action = parser.add_argument(
"--group", "--group",
type=str, type=str,
action="append", action="append",
default=[], default=[],
help="the group to import the secrets to", help="the group to import the secrets to",
) )
parser.add_argument( add_dynamic_completer(group_action, complete_groups)
machine_action = parser.add_argument(
"--machine", "--machine",
type=str, type=str,
action="append", action="append",
default=[], default=[],
help="the machine to import the secrets to", help="the machine to import the secrets to",
) )
parser.add_argument( add_dynamic_completer(machine_action, complete_machines)
user_action = parser.add_argument(
"--user", "--user",
type=str, type=str,
action="append", action="append",
default=[], default=[],
help="the user to import the secrets to", help="the user to import the secrets to",
) )
add_dynamic_completer(user_action, complete_users)
parser.add_argument( parser.add_argument(
"--prefix", "--prefix",
type=str, type=str,

View File

@@ -1,7 +1,7 @@
import argparse import argparse
from pathlib import Path from pathlib import Path
from ..completions import add_dynamic_completer, complete_machines from ..completions import add_dynamic_completer, complete_machines, complete_secrets
from ..errors import ClanError from ..errors import ClanError
from ..git import commit_files from ..git import commit_files
from ..machines.types import machine_name_type, validate_hostname from ..machines.types import machine_name_type, validate_hostname
@@ -136,9 +136,10 @@ def register_machines_parser(parser: argparse.ArgumentParser) -> None:
action="store_true", action="store_true",
default=False, default=False,
) )
add_parser.add_argument( add_machine_action = add_parser.add_argument(
"machine", help="the name of the machine", type=machine_name_type "machine", help="the name of the machine", type=machine_name_type
) )
add_dynamic_completer(add_machine_action, complete_machines)
add_parser.add_argument( add_parser.add_argument(
"key", "key",
help="public key or private key of the user", help="public key or private key of the user",
@@ -170,9 +171,10 @@ def register_machines_parser(parser: argparse.ArgumentParser) -> None:
"machine", help="the name of the machine", type=machine_name_type "machine", help="the name of the machine", type=machine_name_type
) )
add_dynamic_completer(machine_add_secret_parser, complete_machines) add_dynamic_completer(machine_add_secret_parser, complete_machines)
add_secret_parser.add_argument( add_secret_action = add_secret_parser.add_argument(
"secret", help="the name of the secret", type=secret_name_type "secret", help="the name of the secret", type=secret_name_type
) )
add_dynamic_completer(add_secret_action, complete_secrets)
add_secret_parser.set_defaults(func=add_secret_command) add_secret_parser.set_defaults(func=add_secret_command)
# Parser # Parser
@@ -183,7 +185,8 @@ def register_machines_parser(parser: argparse.ArgumentParser) -> None:
"machine", help="the name of the machine", type=machine_name_type "machine", help="the name of the machine", type=machine_name_type
) )
add_dynamic_completer(machine_remove_parser, complete_machines) add_dynamic_completer(machine_remove_parser, complete_machines)
remove_secret_parser.add_argument( remove_secret_action = remove_secret_parser.add_argument(
"secret", help="the name of the secret", type=secret_name_type "secret", help="the name of the secret", type=secret_name_type
) )
add_dynamic_completer(remove_secret_action, complete_secrets)
remove_secret_parser.set_defaults(func=remove_secret_command) remove_secret_parser.set_defaults(func=remove_secret_command)

View File

@@ -9,7 +9,13 @@ from pathlib import Path
from typing import IO from typing import IO
from .. import tty from .. import tty
from ..completions import add_dynamic_completer, complete_machines, complete_secrets from ..completions import (
add_dynamic_completer,
complete_groups,
complete_machines,
complete_secrets,
complete_users,
)
from ..errors import ClanError from ..errors import ClanError
from ..git import commit_files from ..git import commit_files
from .folders import ( from .folders import (
@@ -333,13 +339,14 @@ def register_secrets_parser(subparser: argparse._SubParsersAction) -> None:
parser_set = subparser.add_parser("set", help="set a secret") parser_set = subparser.add_parser("set", help="set a secret")
add_secret_argument(parser_set, False) add_secret_argument(parser_set, False)
parser_set.add_argument( set_group_action = parser_set.add_argument(
"--group", "--group",
type=str, type=str,
action="append", action="append",
default=[], default=[],
help="the group to import the secrets to (can be repeated)", help="the group to import the secrets to (can be repeated)",
) )
add_dynamic_completer(set_group_action, complete_groups)
machine_parser = parser_set.add_argument( machine_parser = parser_set.add_argument(
"--machine", "--machine",
type=str, type=str,
@@ -348,13 +355,14 @@ def register_secrets_parser(subparser: argparse._SubParsersAction) -> None:
help="the machine to import the secrets to (can be repeated)", help="the machine to import the secrets to (can be repeated)",
) )
add_dynamic_completer(machine_parser, complete_machines) add_dynamic_completer(machine_parser, complete_machines)
parser_set.add_argument( set_user_action = parser_set.add_argument(
"--user", "--user",
type=str, type=str,
action="append", action="append",
default=[], default=[],
help="the user to import the secrets to (can be repeated)", help="the user to import the secrets to (can be repeated)",
) )
add_dynamic_completer(set_user_action, complete_users)
parser_set.add_argument( parser_set.add_argument(
"-e", "-e",
"--edit", "--edit",