Skip to main content

jira_cli/commands/
fields.rs

1use crate::api::{ApiError, JiraClient};
2use crate::output::OutputConfig;
3
4/// List all available fields, optionally showing only custom fields.
5pub async fn list(
6    client: &JiraClient,
7    out: &OutputConfig,
8    custom_only: bool,
9) -> Result<(), ApiError> {
10    let mut fields = client.list_fields().await?;
11
12    if custom_only {
13        fields.retain(|f| f.custom);
14    }
15
16    // Sort: system fields first (alphabetically), then custom fields (alphabetically)
17    fields.sort_by(|a, b| {
18        b.custom
19            .cmp(&a.custom)
20            .reverse()
21            .then(a.name.to_lowercase().cmp(&b.name.to_lowercase()))
22    });
23
24    if out.json {
25        out.print_data(
26            &serde_json::to_string_pretty(&serde_json::json!({
27                "total": fields.len(),
28                "fields": fields.iter().map(|f| serde_json::json!({
29                    "id": f.id,
30                    "name": f.name,
31                    "custom": f.custom,
32                    "type": f.schema.as_ref().map(|s| &s.field_type),
33                })).collect::<Vec<_>>(),
34            }))
35            .expect("failed to serialize JSON"),
36        );
37        return Ok(());
38    }
39
40    for f in &fields {
41        let kind = if f.custom { "custom" } else { "system" };
42        let type_str = f
43            .schema
44            .as_ref()
45            .map(|s| s.field_type.as_str())
46            .unwrap_or("-");
47        println!("{:<30}  {:<25}  {:<8}  {}", f.name, f.id, kind, type_str);
48    }
49    Ok(())
50}