Skip to content

Commit fbd4dbc

Browse files
committed
fix(cli): support stdin via dash convention in import command
Fixes bounty issue #1574 The import command now recognizes the standard Unix convention of using "-" (dash) as a filename to read from stdin. Previously, using "cortex import -" would attempt to read from a file literally named "-" which would fail. Now it properly reads JSON content from stdin.
1 parent 8f839ec commit fbd4dbc

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

cortex-cli/src/import_cmd.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::export_cmd::{ExportMessage, SessionExport};
1818
/// Import a session from JSON format.
1919
#[derive(Debug, Parser)]
2020
pub struct ImportCommand {
21-
/// Path to the JSON file to import, or URL to fetch
21+
/// Path to the JSON file to import, URL to fetch, or "-" to read from stdin
2222
#[arg(value_name = "FILE_OR_URL")]
2323
pub source: String,
2424

@@ -39,16 +39,23 @@ impl ImportCommand {
3939
.ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
4040

4141
// Read the export data
42-
let json_content =
43-
if self.source.starts_with("http://") || self.source.starts_with("https://") {
44-
// Fetch from URL
45-
fetch_url(&self.source).await?
46-
} else {
47-
// Read from local file
48-
let path = PathBuf::from(&self.source);
49-
std::fs::read_to_string(&path)
50-
.with_context(|| format!("Failed to read file: {}", path.display()))?
51-
};
42+
let json_content = if self.source == "-" {
43+
// Read from stdin (standard Unix convention)
44+
use std::io::Read;
45+
let mut buffer = String::new();
46+
std::io::stdin()
47+
.read_to_string(&mut buffer)
48+
.with_context(|| "Failed to read from stdin")?;
49+
buffer
50+
} else if self.source.starts_with("http://") || self.source.starts_with("https://") {
51+
// Fetch from URL
52+
fetch_url(&self.source).await?
53+
} else {
54+
// Read from local file
55+
let path = PathBuf::from(&self.source);
56+
std::fs::read_to_string(&path)
57+
.with_context(|| format!("Failed to read file: {}", path.display()))?
58+
};
5259

5360
// Parse the export
5461
let export: SessionExport = serde_json::from_str(&json_content)

0 commit comments

Comments
 (0)