Skip to content
Open
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
40 changes: 38 additions & 2 deletions src/cortex-cli/src/export_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub struct ExportCommand {
#[arg(short, long, value_enum, default_value_t = ExportFormat::Json)]
pub format: ExportFormat,

/// Pretty-print the output (for json/yaml)
#[arg(long, default_value_t = true)]
/// Pretty-print JSON output
#[arg(long, action = clap::ArgAction::SetTrue)]
pub pretty: bool,
}

Expand Down Expand Up @@ -111,6 +111,8 @@ pub struct ExportToolCall {
impl ExportCommand {
/// Run the export command.
pub async fn run(self) -> Result<()> {
validate_format_options(self.format, self.pretty)?;

let cortex_home = dirs::home_dir()
.map(|h| h.join(".cortex"))
.ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
Expand Down Expand Up @@ -241,6 +243,14 @@ impl ExportCommand {
}
}

fn validate_format_options(format: ExportFormat, pretty: bool) -> Result<()> {
if pretty && format != ExportFormat::Json {
bail!("--pretty is only supported for JSON exports");
}

Ok(())
}

/// Escape a field for CSV output.
fn escape_csv_field(field: &str) -> String {
if field.contains(',') || field.contains('"') || field.contains('\n') {
Expand Down Expand Up @@ -466,4 +476,30 @@ mod tests {
assert_eq!(refs.len(), 1);
assert!(refs.contains(&"explore".to_string()));
}

#[test]
fn test_pretty_defaults_to_false() {
let cmd = ExportCommand::try_parse_from(["export", "session-id"]).unwrap();

assert!(!cmd.pretty);
}

#[test]
fn test_validate_format_options_allows_pretty_json() {
validate_format_options(ExportFormat::Json, true).unwrap();
}

#[test]
fn test_validate_format_options_rejects_pretty_yaml() {
let err = validate_format_options(ExportFormat::Yaml, true).unwrap_err();

assert!(err.to_string().contains("JSON exports"));
}

#[test]
fn test_validate_format_options_rejects_pretty_csv() {
let err = validate_format_options(ExportFormat::Csv, true).unwrap_err();

assert!(err.to_string().contains("JSON exports"));
}
}