Skip to content

Commit c5c92cb

Browse files
authored
feat(datasets): Add upload id functionality to datasets create command
1 parent d5ac1e1 commit c5c92cb

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

src/command.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub enum DatasetsCommands {
179179
format: String,
180180
},
181181

182-
/// Create a new dataset from a file or piped stdin
182+
/// Create a new dataset from a file, piped stdin, or a pre-existing upload ID
183183
Create {
184184
/// Dataset label (derived from filename if omitted)
185185
#[arg(long)]
@@ -190,8 +190,16 @@ pub enum DatasetsCommands {
190190
table_name: Option<String>,
191191

192192
/// Path to a file to upload (omit to read from stdin)
193-
#[arg(long)]
193+
#[arg(long, conflicts_with = "upload_id")]
194194
file: Option<String>,
195+
196+
/// Skip upload and use a pre-existing upload ID directly
197+
#[arg(long, conflicts_with = "file")]
198+
upload_id: Option<String>,
199+
200+
/// Source format when using --upload-id (csv, json, parquet)
201+
#[arg(long, default_value = "csv", value_parser = ["csv", "json", "parquet"], requires = "upload_id")]
202+
format: String,
195203
},
196204

197205
}

src/datasets.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ pub fn create(
231231
label: Option<&str>,
232232
table_name: Option<&str>,
233233
file: Option<&str>,
234+
upload_id: Option<&str>,
235+
source_format: &str,
234236
) {
235237
let profile_config = match config::load("default") {
236238
Ok(c) => c,
@@ -260,31 +262,42 @@ pub fn create(
260262
.to_string();
261263
&label_derived
262264
}
263-
None => match stdin_redirect_filename() {
264-
Some(name) => {
265-
label_derived = name;
266-
&label_derived
267-
}
268-
None => {
265+
None => {
266+
if upload_id.is_some() {
269267
eprintln!("error: no label provided. Use --label to name the dataset.");
270268
std::process::exit(1);
271269
}
272-
},
270+
match stdin_redirect_filename() {
271+
Some(name) => {
272+
label_derived = name;
273+
&label_derived
274+
}
275+
None => {
276+
eprintln!("error: no label provided. Use --label to name the dataset.");
277+
std::process::exit(1);
278+
}
279+
}
280+
}
273281
},
274282
};
275283

276284
let client = reqwest::blocking::Client::new();
277285

278-
let (upload_id, format) = match file {
279-
Some(path) => upload_from_file(&client, &api_key, workspace_id, &profile_config.api_url, path),
280-
None => {
281-
use std::io::IsTerminal;
282-
if std::io::stdin().is_terminal() {
283-
eprintln!("error: no input data. Use --file <path> or pipe data via stdin.");
284-
std::process::exit(1);
286+
let (upload_id, format, upload_id_was_uploaded): (String, &str, bool) = if let Some(id) = upload_id {
287+
(id.to_string(), source_format, false)
288+
} else {
289+
let (id, fmt) = match file {
290+
Some(path) => upload_from_file(&client, &api_key, workspace_id, &profile_config.api_url, path),
291+
None => {
292+
use std::io::IsTerminal;
293+
if std::io::stdin().is_terminal() {
294+
eprintln!("error: no input data. Use --file <path>, --upload-id <id>, or pipe data via stdin.");
295+
std::process::exit(1);
296+
}
297+
upload_from_stdin(&client, &api_key, workspace_id, &profile_config.api_url)
285298
}
286-
upload_from_stdin(&client, &api_key, workspace_id, &profile_config.api_url)
287-
}
299+
};
300+
(id, fmt, true)
288301
};
289302

290303
let source = json!({ "upload_id": upload_id, "format": format });
@@ -312,6 +325,16 @@ pub fn create(
312325
if !resp.status().is_success() {
313326
use crossterm::style::Stylize;
314327
eprintln!("{}", api_error(resp.text().unwrap_or_default()).red());
328+
// Only show the resume hint when the upload_id came from a fresh upload
329+
if upload_id_was_uploaded {
330+
eprintln!(
331+
"{}",
332+
format!(
333+
"Resume dataset creation without re-uploading by passing --upload-id {upload_id}"
334+
)
335+
.yellow()
336+
);
337+
}
315338
std::process::exit(1);
316339
}
317340

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ fn main() {
7070
Some(DatasetsCommands::List { limit, offset, format }) => {
7171
datasets::list(&workspace_id, limit, offset, &format)
7272
}
73-
Some(DatasetsCommands::Create { label, table_name, file }) => {
74-
datasets::create(&workspace_id, label.as_deref(), table_name.as_deref(), file.as_deref())
73+
Some(DatasetsCommands::Create { label, table_name, file, upload_id, format }) => {
74+
datasets::create(&workspace_id, label.as_deref(), table_name.as_deref(), file.as_deref(), upload_id.as_deref(), &format)
7575
}
7676
None => {
7777
use clap::CommandFactory;

0 commit comments

Comments
 (0)