Skip to content

Commit 73e4dee

Browse files
factorydroidechobt
authored andcommitted
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 28efd51 commit 73e4dee

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

cortex-cli/src/import_cmd.rs

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

@@ -41,17 +41,24 @@ impl ImportCommand {
4141
.ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
4242

4343
// Read the export data
44-
let json_content =
45-
if self.source.starts_with("http://") || self.source.starts_with("https://") {
46-
// Fetch from URL
47-
fetch_url(&self.source).await?
48-
} else {
49-
// Read from local file (expand tilde to home directory)
50-
let path = expand_home_path(Path::new(&self.source))
51-
.map_err(|e| anyhow::anyhow!("Failed to expand path: {}", e))?;
52-
std::fs::read_to_string(&path)
53-
.with_context(|| format!("Failed to read file: {}", path.display()))?
54-
};
44+
let json_content = if self.source == "-" {
45+
// Read from stdin (standard Unix convention)
46+
use std::io::Read;
47+
let mut buffer = String::new();
48+
std::io::stdin()
49+
.read_to_string(&mut buffer)
50+
.with_context(|| "Failed to read from stdin")?;
51+
buffer
52+
} else if self.source.starts_with("http://") || self.source.starts_with("https://") {
53+
// Fetch from URL
54+
fetch_url(&self.source).await?
55+
} else {
56+
// Read from local file (expand tilde to home directory)
57+
let path = expand_home_path(Path::new(&self.source))
58+
.map_err(|e| anyhow::anyhow!("Failed to expand path: {}", e))?;
59+
std::fs::read_to_string(&path)
60+
.with_context(|| format!("Failed to read file: {}", path.display()))?
61+
};
5562

5663
// Parse the export
5764
let export: SessionExport = match serde_json::from_str(&json_content) {

0 commit comments

Comments
 (0)