33from pathlib import Path
44
55import click
6- from click_aliases import ClickAliasedGroup
76
87from cratedb_toolkit .cluster .core import DatabaseCluster
98from cratedb_toolkit .model import InputOutputResource , TableAddress
109from cratedb_toolkit .option import option_cluster_id , option_cluster_name , option_cluster_url
11- from cratedb_toolkit .util .cli import boot_click , make_command
10+ from cratedb_toolkit .util .cli import boot_click
1211
1312logger = logging .getLogger (__name__ )
1413
1514
16- @click .group (cls = ClickAliasedGroup ) # type: ignore[arg-type]
15+ @click .command ()
16+ @click .argument ("source_url" )
17+ @click .argument ("target_url" , required = False )
1718@click .option ("--verbose" , is_flag = True , required = False , help = "Turn on logging" )
1819@click .option ("--debug" , is_flag = True , required = False , help = "Turn on logging with debug level" )
19- @click .version_option ()
20- @click .pass_context
21- def cli_load (ctx : click .Context , verbose : bool , debug : bool ):
22- """
23- Load data into CrateDB.
24- """
25- return boot_click (ctx , verbose , debug )
26-
27-
28- @make_command (cli_load , name = "table" )
29- @click .argument ("url" )
3020@option_cluster_id
3121@option_cluster_name
3222@option_cluster_url
@@ -36,9 +26,10 @@ def cli_load(ctx: click.Context, verbose: bool, debug: bool):
3626@click .option ("--compression" , type = str , required = False , help = "Compression format of the import resource" )
3727@click .option ("--transformation" , type = Path , required = False , help = "Path to Tikray transformation file" )
3828@click .pass_context
39- def load_table (
29+ def cli_load (
4030 ctx : click .Context ,
41- url : str ,
31+ source_url : str ,
32+ target_url : str ,
4233 cluster_id : str ,
4334 cluster_name : str ,
4435 cluster_url : str ,
@@ -47,20 +38,30 @@ def load_table(
4738 format_ : str ,
4839 compression : str ,
4940 transformation : t .Union [Path , None ],
41+ verbose : bool ,
42+ debug : bool ,
5043):
5144 """
52- Import data into CrateDB and CrateDB Cloud clusters .
45+ Load data into CrateDB.
5346 """
5447
48+ boot_click (ctx , verbose , debug )
49+
50+ # API evolution adjustments.
51+ if target_url and cluster_url and target_url != cluster_url :
52+ raise click .UsageError ("Specify cluster URL either as TARGET_URL or --cluster-url, not both." )
53+ if target_url :
54+ cluster_url = target_url
55+
5556 # When `--transformation` is given, but empty, fix it.
5657 if transformation is not None and transformation .name == "" :
5758 transformation = None
5859
5960 # Encapsulate source and target parameters.
60- source = InputOutputResource (url = url , format = format_ , compression = compression )
61+ source = InputOutputResource (url = source_url , format = format_ , compression = compression )
6162 target = TableAddress (schema = schema , table = table )
6263
63- # Dispatch "load table " operation.
64+ # Dispatch "load" operation.
6465 cluster = DatabaseCluster .create (
6566 cluster_id = cluster_id ,
6667 cluster_name = cluster_name ,
@@ -69,49 +70,58 @@ def load_table(
6970 cluster .load_table (source = source , target = target , transformation = transformation )
7071
7172
72- @click .group (cls = ClickAliasedGroup ) # type: ignore[arg-type]
73+ @click .command ()
74+ @click .argument ("source_url" , required = False )
75+ @click .argument ("target_url" , required = False )
7376@click .option ("--verbose" , is_flag = True , required = False , help = "Turn on logging" )
7477@click .option ("--debug" , is_flag = True , required = False , help = "Turn on logging with debug level" )
75- @click .version_option ()
76- @click .pass_context
77- def cli_save (ctx : click .Context , verbose : bool , debug : bool ):
78- """
79- Export data from CrateDB.
80- """
81- return boot_click (ctx , verbose , debug )
82-
83-
84- @make_command (cli_save , name = "table" )
85- @click .argument ("url" )
8678@option_cluster_id
8779@option_cluster_name
8880@option_cluster_url
8981@click .option ("--format" , "format_" , type = str , required = False , help = "File format of the export resource" )
9082@click .option ("--compression" , type = str , required = False , help = "Compression format of the export resource" )
9183@click .option ("--transformation" , type = Path , required = False , help = "Path to Tikray transformation file" )
9284@click .pass_context
93- def save_table (
85+ def cli_save (
9486 ctx : click .Context ,
95- url : str ,
87+ source_url : t .Optional [str ],
88+ target_url : t .Optional [str ],
9689 cluster_id : str ,
9790 cluster_name : str ,
9891 cluster_url : str ,
9992 format_ : str ,
10093 compression : str ,
10194 transformation : t .Union [Path , None ],
95+ verbose : bool ,
96+ debug : bool ,
10297):
10398 """
104- Export data from CrateDB and CrateDB Cloud clusters .
99+ Export data from CrateDB.
105100 """
106101
102+ boot_click (ctx , verbose , debug )
103+
104+ # API evolution adjustments.
105+ if source_url and not target_url :
106+ target_url = source_url
107+ source_url = None
108+ if source_url and cluster_url and source_url != cluster_url :
109+ raise click .UsageError ("Specify cluster URL either as SOURCE_URL or --cluster-url, not both." )
110+ if source_url :
111+ cluster_url = source_url
112+ if not target_url :
113+ raise click .UsageError (
114+ "Missing TARGET_URL. Use `ctk save <target-url>` or `ctk save <cluster-url> <target-url>`."
115+ )
116+
107117 # When `--transformation` is given, but empty, fix it.
108118 if transformation is not None and transformation .name == "" :
109119 transformation = None
110120
111121 # Encapsulate source and target parameters.
112- target = InputOutputResource (url = url , format = format_ , compression = compression )
122+ target = InputOutputResource (url = target_url , format = format_ , compression = compression )
113123
114- # Dispatch "save table " operation.
124+ # Dispatch "save" operation.
115125 cluster = DatabaseCluster .create (
116126 cluster_id = cluster_id ,
117127 cluster_name = cluster_name ,
0 commit comments