Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 14 additions & 15 deletions firm_cli/src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn list_items(
list_entities(workspace_path, target_type, output_format)
}

/// Lists entities of a given type in the workspace.
/// Lists entity IDs of a given type in the workspace.
fn list_entities(
workspace_path: &PathBuf,
entity_type: String,
Expand All @@ -30,35 +30,34 @@ fn list_entities(
let graph = load_current_graph(workspace_path)?;

let entities = graph.list_by_type(&entity_type.as_str().into());
let ids: Vec<&str> = entities.iter().map(|e| e.id.as_str()).collect();

ui::success(&format!(
"Found {} entities with type '{}'",
entities.len(),
ids.len(),
entity_type,
));

match output_format {
OutputFormat::Pretty => ui::pretty_output_entity_list(&entities),
OutputFormat::Json => ui::json_output(&entities),
}
ui::list_output(&ids, output_format);

Ok(())
}

/// Lists all schemas in the workspace.
/// Lists all schema names in the workspace.
fn list_schemas(workspace_path: &PathBuf, output_format: OutputFormat) -> Result<(), CliError> {
ui::header("Listing schemas");
let mut workspace = Workspace::new();
load_workspace_files(workspace_path, &mut workspace).map_err(|_| CliError::BuildError)?;
let build = build_workspace(workspace).map_err(|_| CliError::BuildError)?;

ui::success(&format!(
"Found {} schemas for this workspace",
build.schemas.len()
));
let names: Vec<&str> = build
.schemas
.iter()
.map(|s| s.entity_type.as_str())
.collect();

match output_format {
OutputFormat::Pretty => ui::pretty_output_schema_list(&build.schemas.iter().collect()),
OutputFormat::Json => ui::json_output(&build.schemas),
}
ui::success(&format!("Found {} schemas for this workspace", names.len()));

ui::list_output(&names, output_format);
Ok(())
}
28 changes: 13 additions & 15 deletions firm_cli/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ pub fn error_with_details(main_msg: &str, details: &str) {
}

/// Selects the output format used by the CLI.
#[derive(Clone, Debug, ValueEnum, PartialEq)]
#[derive(Default)]
#[derive(Clone, Debug, ValueEnum, PartialEq, Default)]
pub enum OutputFormat {
#[default]
Pretty,
Expand All @@ -95,7 +94,6 @@ impl fmt::Display for OutputFormat {
}
}


/// Outputs a single entity in pretty format.
pub fn pretty_output_entity_single(entity: &Entity) {
println!("\n{}", entity);
Expand All @@ -118,25 +116,25 @@ pub fn pretty_output_schema_single(schema: &EntitySchema) {
println!("\n{}", schema);
}

/// Outputs a list of entity schemas in pretty format.
pub fn pretty_output_schema_list(schemas: &Vec<&EntitySchema>) {
for (i, schema) in schemas.iter().enumerate() {
pretty_output_schema_single(schema);

// Add a separator after each entity, except for the last one.
if i < schemas.len() - 1 {
println!("---------------------------------------");
}
}
}

/// Outputs a serde-serializable object in json format.
pub fn json_output<T: serde::Serialize>(data: &T) {
if let Ok(json) = serde_json::to_string_pretty(data) {
println!("{}", json);
}
}

/// Outputs a list of strings (one per line for pretty, array for JSON).
pub fn list_output(items: &[&str], format: OutputFormat) {
match format {
OutputFormat::Pretty => {
for item in items {
println!("{}", item);
}
}
OutputFormat::Json => json_output(&items),
}
}

/// Outputs raw text (e.g., file paths, simple values).
pub fn raw_output(text: &str) {
println!("{}", text);
Expand Down