Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions cortex-cli/src/import_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::export_cmd::{ExportMessage, SessionExport};
/// Import a session from JSON format.
#[derive(Debug, Parser)]
pub struct ImportCommand {
/// Path to the JSON file to import, or URL to fetch
/// Path to the JSON file to import, URL to fetch, or "-" to read from stdin
#[arg(value_name = "FILE_OR_URL")]
pub source: String,

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

// Read the export data
let json_content =
if self.source.starts_with("http://") || self.source.starts_with("https://") {
// Fetch from URL
fetch_url(&self.source).await?
} else {
// Read from local file (expand tilde to home directory)
let path = expand_home_path(Path::new(&self.source))
.map_err(|e| anyhow::anyhow!("Failed to expand path: {}", e))?;
std::fs::read_to_string(&path)
.with_context(|| format!("Failed to read file: {}", path.display()))?
};
let json_content = if self.source == "-" {
// Read from stdin (standard Unix convention)
use std::io::Read;
let mut buffer = String::new();
std::io::stdin()
.read_to_string(&mut buffer)
.with_context(|| "Failed to read from stdin")?;
buffer
} else if self.source.starts_with("http://") || self.source.starts_with("https://") {
// Fetch from URL
fetch_url(&self.source).await?
} else {
// Read from local file (expand tilde to home directory)
let path = expand_home_path(Path::new(&self.source))
.map_err(|e| anyhow::anyhow!("Failed to expand path: {}", e))?;
std::fs::read_to_string(&path)
.with_context(|| format!("Failed to read file: {}", path.display()))?
};

// Parse the export
let export: SessionExport = match serde_json::from_str(&json_content) {
Expand Down
Loading