|
37 | 37 |
|
38 | 38 |
|
39 | 39 | 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) |
49 | 63 | flake_exists: bool = os.path.exists(flake_path)
|
50 | 64 |
|
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") |
56 | 67 |
|
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") |
59 | 70 |
|
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) |
61 | 75 |
|
62 | 76 |
|
63 | 77 | def set_common_depl(depl: nixops.deployment.Deployment, args: Namespace) -> None:
|
@@ -1127,12 +1141,20 @@ def add_subparser(
|
1127 | 1141 | subparsers: _SubParsersAction, name: str, help: str
|
1128 | 1142 | ) -> ArgumentParser:
|
1129 | 1143 | subparser = subparsers.add_parser(name, help=help)
|
1130 |
| - subparser.add_argument( |
| 1144 | + network = subparser.add_mutually_exclusive_group() |
| 1145 | + network.add_argument( |
1131 | 1146 | "--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", |
1133 | 1156 | 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", |
1136 | 1158 | )
|
1137 | 1159 | subparser.add_argument(
|
1138 | 1160 | "--deployment",
|
|
0 commit comments