99from copy import copy
1010from glob import glob
1111from pathlib import Path
12- from typing import TYPE_CHECKING , Any , Dict , List , Optional , OrderedDict , Tuple , cast
12+ from typing import TYPE_CHECKING , Any , Dict , List , Optional , Tuple , cast
1313from urllib .parse import quote , urlparse
1414
1515import click
3131 wait_for_job ,
3232)
3333from splitgraph .commandline .engine import inject_config_into_engines
34- from splitgraph .config import CONFIG
35- from splitgraph .config .config import get_from_subsection , get_singleton
34+ from splitgraph .config .config import get_from_subsection
3635from splitgraph .config .management import patch_and_save_config
3736from splitgraph .core .output import Color , pluralise
3837
@@ -1219,22 +1218,9 @@ def seed_c(remote, seed, github_repository, directory):
12191218 click .echo (f"Splitgraph project generated in { os .path .abspath (directory )} ." )
12201219
12211220
1222- @click .command ("tunnel" )
1223- @click .option ("--remote" , default = "data.splitgraph.com" , help = "Name of the remote registry to use." )
1224- @click .option (
1225- "--repositories-file" , "-f" , default = ["splitgraph.yml" ], type = click .Path (), multiple = True
1226- )
1227- @click .argument ("repository" , type = RepositoryType (exists = False ))
1228- def tunnel_c (remote : str , repositories_file : List [Path ], repository : "CoreRepository" ):
1229- """
1230- Start the tunnel client to make tunneled external repo available.
1231-
1232- This will load a splitgraph.yml file and tunnel the host:port address of the
1233- external repository specified in the argument.
1234- """
1235-
1236- external = _get_external_from_yaml (repositories_file , repository )[0 ]
1237-
1221+ def start_repository_tunnel (
1222+ remote : str , repository : "CoreRepository" , external : "External"
1223+ ) -> None :
12381224 if not external .tunnel :
12391225 raise click .UsageError (
12401226 f"Repository { repository .namespace } /{ repository .repository } is not tunneled"
@@ -1266,15 +1252,7 @@ def tunnel_c(remote: str, repositories_file: List[Path], repository: "CoreReposi
12661252 launch_rathole_client (get_rathole_client_binary_path (), rathole_client_config_path )
12671253
12681254
1269- @click .command ("ephemeral-tunnel" )
1270- @click .option ("--remote" , default = "data.splitgraph.com" , help = "Name of the remote registry to use." )
1271- @click .argument ("host" , type = str )
1272- @click .argument ("port" , type = int )
1273- def ephemeral_tunnel_c (remote : str , host : str , port : int ):
1274- """
1275- Start ephemeral tunnel.
1276- """
1277-
1255+ def start_ephemeral_tunnel (remote : str , local_address : str ) -> None :
12781256 from splitgraph .cloud import GQLAPIClient
12791257
12801258 client = GQLAPIClient (remote )
@@ -1291,16 +1269,43 @@ def ephemeral_tunnel_c(remote: str, host: str, port: int):
12911269 secret_token ,
12921270 tunnel_connect_host ,
12931271 tunnel_connect_port ,
1294- f" { host } : { port } " ,
1272+ local_address ,
12951273 tunnel_connect_host ,
12961274 )
12971275 print (
1298- f"To connect to { host } : { port } from Splitgraph, use the following connection parameters:\n Host: { private_address_host } \n Port: { private_address_port } "
1276+ f"To connect to { local_address } from Splitgraph, use the following connection parameters:\n Host: { private_address_host } \n Port: { private_address_port } "
12991277 )
13001278 print ("launching rathole client" )
13011279 launch_rathole_client (get_rathole_client_binary_path (), rathole_client_config_path )
13021280
13031281
1282+ @click .command ("tunnel" )
1283+ @click .option ("--remote" , default = "data.splitgraph.com" , help = "Name of the remote registry to use." )
1284+ @click .option (
1285+ "--repositories-file" , "-f" , default = ["splitgraph.yml" ], type = click .Path (), multiple = True
1286+ )
1287+ @click .argument ("repository_or_local_address" , type = str )
1288+ def tunnel_c (remote : str , repositories_file : List [Path ], repository_or_local_address : str ):
1289+ """
1290+ Start the tunnel client to make tunneled external repo available.
1291+
1292+ This will load a splitgraph.yml file and tunnel the host:port address of the
1293+ external repository specified in the argument.
1294+ """
1295+
1296+ if repository_or_local_address .find ("/" ) > - 1 :
1297+ repository : "CoreRepository" = RepositoryType (exists = False ).convert (
1298+ repository_or_local_address , None , None
1299+ )
1300+ external = _get_external_from_yaml (repositories_file , repository )[0 ]
1301+ start_repository_tunnel (remote , repository , external )
1302+
1303+ elif repository_or_local_address .find (":" ) > - 1 :
1304+ start_ephemeral_tunnel (remote , repository_or_local_address )
1305+ else :
1306+ raise click .UsageError ("argument should be of the form namespace/repository or host:port" )
1307+
1308+
13041309@click .group ("cloud" )
13051310def cloud_c ():
13061311 """Run actions on Splitgraph Cloud."""
@@ -1329,4 +1334,3 @@ def cloud_c():
13291334cloud_c .add_command (validate_c )
13301335cloud_c .add_command (seed_c )
13311336cloud_c .add_command (tunnel_c )
1332- cloud_c .add_command (ephemeral_tunnel_c )
0 commit comments