Skip to content

Commit 2809295

Browse files
committed
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).
1 parent 35ac020 commit 2809295

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

nixops/args.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,19 @@
6161
subparser = add_subparser(subparsers, "create", help="create a new deployment")
6262
subparser.set_defaults(op=op_create)
6363
subparser.add_argument(
64-
"--name", "-n", dest="name", metavar="NAME", help=SUPPRESS
64+
"--name", dest="name", metavar="NAME", help=SUPPRESS
6565
) # obsolete, use -d instead
6666

6767
subparser = add_subparser(subparsers, "modify", help="modify an existing deployment")
6868
subparser.set_defaults(op=op_modify)
6969
subparser.add_argument(
70-
"--name", "-n", dest="name", metavar="NAME", help="new symbolic name of deployment"
70+
"--name", dest="name", metavar="NAME", help="new symbolic name of deployment"
7171
)
7272

7373
subparser = add_subparser(subparsers, "clone", help="clone an existing deployment")
7474
subparser.set_defaults(op=op_clone)
7575
subparser.add_argument(
7676
"--name",
77-
"-n",
7877
dest="name",
7978
metavar="NAME",
8079
help="symbolic name of the cloned deployment",

nixops/script_defs.py

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,38 @@
3737

3838

3939
def get_network_file(args: Namespace) -> NetworkFile:
40-
network_dir: str = os.path.abspath(args.network_dir)
41-
42-
if not os.path.exists(network_dir):
43-
raise ValueError("f{network_dir} does not exist")
44-
45-
classic_path = os.path.join(network_dir, "nixops.nix")
46-
flake_path = os.path.join(network_dir, "flake.nix")
47-
48-
classic_exists: bool = os.path.exists(classic_path)
40+
if args.network_path is not None:
41+
network_path: str = os.path.abspath(args.network_path)
42+
if os.path.isdir(network_path):
43+
network_path = os.path.join(network_path, "nixops.nix")
44+
if not os.path.exists(network_path):
45+
raise ValueError(f"{network_path} does not exist")
46+
return NetworkFile(network=network_path, is_flake=False)
47+
48+
if args.flake_path is not None:
49+
flake_path: str = os.path.abspath(args.flake_path)
50+
if os.path.isdir(flake_path):
51+
flake_path = os.path.join(flake_path, "flake.nix")
52+
if not os.path.exists(flake_path):
53+
raise ValueError(f"{flake_path} does not exist")
54+
return NetworkFile(network=flake_path, is_flake=True)
55+
56+
network_path: str = os.path.join(os.getcwd(), "nixops.nix")
57+
flake_path: str = os.path.join(os.getcwd(), "flake.nix")
58+
59+
network_exists: bool = os.path.exists(network_path)
4960
flake_exists: bool = os.path.exists(flake_path)
5061

51-
if all((flake_exists, classic_exists)):
52-
raise ValueError("Both flake.nix and nixops.nix cannot coexist")
53-
54-
if classic_exists:
55-
return NetworkFile(network=classic_path, is_flake=False)
62+
if all((flake_exists, network_exists)):
63+
raise ValueError("Both flake.nix and nixops.nix found in current directory")
5664

57-
if flake_exists:
58-
return NetworkFile(network=network_dir, is_flake=True)
65+
if not network_exists and not flake_exists:
66+
raise ValueError("Neither flake.nix nor nixops.nix exists in current directory")
5967

60-
raise ValueError(f"Neither flake.nix nor nixops.nix exists in {network_dir}")
68+
if network_exists:
69+
return NetworkFile(network=network_path, is_flake=False)
70+
else:
71+
return NetworkFile(network=network_path, is_flake=True)
6172

6273

6374
def set_common_depl(depl: nixops.deployment.Deployment, args: Namespace) -> None:
@@ -1127,12 +1138,20 @@ def add_subparser(
11271138
subparsers: _SubParsersAction, name: str, help: str
11281139
) -> ArgumentParser:
11291140
subparser = subparsers.add_parser(name, help=help)
1130-
subparser.add_argument(
1141+
network = subparser.add_mutually_exclusive_group()
1142+
network.add_argument(
11311143
"--network",
1132-
dest="network_dir",
1144+
"-n",
1145+
dest="network_path",
1146+
metavar="FILE",
1147+
help="path to network file or a directory containing nixops.nix",
1148+
)
1149+
network.add_argument(
1150+
"--flake",
1151+
"-f",
1152+
dest="flake_path",
11331153
metavar="FILE",
1134-
default=os.getcwd(),
1135-
help="path to a directory containing either nixops.nix or flake.nix",
1154+
help="path to flake or a directory containing flake.nix",
11361155
)
11371156
subparser.add_argument(
11381157
"--deployment",

0 commit comments

Comments
 (0)