Skip to content

Commit

Permalink
Make --network accept a file argument and add a --flake flag
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
talyz committed Oct 21, 2021
1 parent 35ac020 commit 2809295
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
5 changes: 2 additions & 3 deletions nixops/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 40 additions & 21 deletions nixops/script_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 2809295

Please sign in to comment.