From 2809295ec955f89a30d4ee079a4ca4181bd51bfa Mon Sep 17 00:00:00 2001 From: talyz Date: Thu, 21 Oct 2021 13:43:48 +0200 Subject: [PATCH] Make --network accept a file argument and add a --flake flag To make it possible to keep multiple network definitions in the same directory, make the `--network` flag accept a file in addition to a directory. Also, add a corresponding `--flake` flag and create short flags for both (`-n` and `-f` respectively). --- nixops/args.py | 5 ++-- nixops/script_defs.py | 61 ++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/nixops/args.py b/nixops/args.py index 97ade4469..fb12a7cf3 100644 --- a/nixops/args.py +++ b/nixops/args.py @@ -61,20 +61,19 @@ subparser = add_subparser(subparsers, "create", help="create a new deployment") subparser.set_defaults(op=op_create) subparser.add_argument( - "--name", "-n", dest="name", metavar="NAME", help=SUPPRESS + "--name", dest="name", metavar="NAME", help=SUPPRESS ) # obsolete, use -d instead subparser = add_subparser(subparsers, "modify", help="modify an existing deployment") subparser.set_defaults(op=op_modify) subparser.add_argument( - "--name", "-n", dest="name", metavar="NAME", help="new symbolic name of deployment" + "--name", dest="name", metavar="NAME", help="new symbolic name of deployment" ) subparser = add_subparser(subparsers, "clone", help="clone an existing deployment") subparser.set_defaults(op=op_clone) subparser.add_argument( "--name", - "-n", dest="name", metavar="NAME", help="symbolic name of the cloned deployment", diff --git a/nixops/script_defs.py b/nixops/script_defs.py index d396ee60d..887e50e09 100644 --- a/nixops/script_defs.py +++ b/nixops/script_defs.py @@ -37,27 +37,38 @@ def get_network_file(args: Namespace) -> NetworkFile: - network_dir: str = os.path.abspath(args.network_dir) - - if not os.path.exists(network_dir): - raise ValueError("f{network_dir} does not exist") - - classic_path = os.path.join(network_dir, "nixops.nix") - flake_path = os.path.join(network_dir, "flake.nix") - - classic_exists: bool = os.path.exists(classic_path) + if args.network_path is not None: + network_path: str = os.path.abspath(args.network_path) + if os.path.isdir(network_path): + network_path = os.path.join(network_path, "nixops.nix") + if not os.path.exists(network_path): + raise ValueError(f"{network_path} does not exist") + return NetworkFile(network=network_path, is_flake=False) + + if args.flake_path is not None: + flake_path: str = os.path.abspath(args.flake_path) + if os.path.isdir(flake_path): + flake_path = os.path.join(flake_path, "flake.nix") + if not os.path.exists(flake_path): + raise ValueError(f"{flake_path} does not exist") + return NetworkFile(network=flake_path, is_flake=True) + + network_path: str = os.path.join(os.getcwd(), "nixops.nix") + flake_path: str = os.path.join(os.getcwd(), "flake.nix") + + network_exists: bool = os.path.exists(network_path) flake_exists: bool = os.path.exists(flake_path) - if all((flake_exists, classic_exists)): - raise ValueError("Both flake.nix and nixops.nix cannot coexist") - - if classic_exists: - return NetworkFile(network=classic_path, is_flake=False) + if all((flake_exists, network_exists)): + raise ValueError("Both flake.nix and nixops.nix found in current directory") - if flake_exists: - return NetworkFile(network=network_dir, is_flake=True) + if not network_exists and not flake_exists: + raise ValueError("Neither flake.nix nor nixops.nix exists in current directory") - raise ValueError(f"Neither flake.nix nor nixops.nix exists in {network_dir}") + if network_exists: + return NetworkFile(network=network_path, is_flake=False) + else: + return NetworkFile(network=network_path, is_flake=True) def set_common_depl(depl: nixops.deployment.Deployment, args: Namespace) -> None: @@ -1127,12 +1138,20 @@ def add_subparser( subparsers: _SubParsersAction, name: str, help: str ) -> ArgumentParser: subparser = subparsers.add_parser(name, help=help) - subparser.add_argument( + network = subparser.add_mutually_exclusive_group() + network.add_argument( "--network", - dest="network_dir", + "-n", + dest="network_path", + metavar="FILE", + help="path to network file or a directory containing nixops.nix", + ) + network.add_argument( + "--flake", + "-f", + dest="flake_path", metavar="FILE", - default=os.getcwd(), - help="path to a directory containing either nixops.nix or flake.nix", + help="path to flake or a directory containing flake.nix", ) subparser.add_argument( "--deployment",