Skip to content

Commit 0ac23a7

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 0ac23a7

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

nixops/args.py

+2-3
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

+43-21
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,41 @@
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+
network_path: str = ""
41+
flake_path: str = ""
42+
43+
if args.network_path is not None:
44+
network_path = os.path.abspath(args.network_path)
45+
if os.path.isdir(network_path):
46+
network_path = os.path.join(network_path, "nixops.nix")
47+
if not os.path.exists(network_path):
48+
raise ValueError(f"{network_path} does not exist")
49+
return NetworkFile(network=network_path, is_flake=False)
50+
51+
if args.flake_path is not None:
52+
flake_path = os.path.abspath(args.flake_path)
53+
if os.path.isdir(flake_path):
54+
flake_path = os.path.join(flake_path, "flake.nix")
55+
if not os.path.exists(flake_path):
56+
raise ValueError(f"{flake_path} does not exist")
57+
return NetworkFile(network=flake_path, is_flake=True)
58+
59+
network_path = os.path.join(os.getcwd(), "nixops.nix")
60+
flake_path = os.path.join(os.getcwd(), "flake.nix")
61+
62+
network_exists: bool = os.path.exists(network_path)
4963
flake_exists: bool = os.path.exists(flake_path)
5064

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)
65+
if all((flake_exists, network_exists)):
66+
raise ValueError("Both flake.nix and nixops.nix found in current directory")
5667

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

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

6276

6377
def set_common_depl(depl: nixops.deployment.Deployment, args: Namespace) -> None:
@@ -1127,12 +1141,20 @@ def add_subparser(
11271141
subparsers: _SubParsersAction, name: str, help: str
11281142
) -> ArgumentParser:
11291143
subparser = subparsers.add_parser(name, help=help)
1130-
subparser.add_argument(
1144+
network = subparser.add_mutually_exclusive_group()
1145+
network.add_argument(
11311146
"--network",
1132-
dest="network_dir",
1147+
"-n",
1148+
dest="network_path",
1149+
metavar="FILE",
1150+
help="path to network file or a directory containing nixops.nix",
1151+
)
1152+
network.add_argument(
1153+
"--flake",
1154+
"-f",
1155+
dest="flake_path",
11331156
metavar="FILE",
1134-
default=os.getcwd(),
1135-
help="path to a directory containing either nixops.nix or flake.nix",
1157+
help="path to flake or a directory containing flake.nix",
11361158
)
11371159
subparser.add_argument(
11381160
"--deployment",

0 commit comments

Comments
 (0)