partiri-cli 0.1.7

partiri CLI — Deploy and manage services on Partiri Cloud
use tabled::Table;

use crate::client::ApiClient;
use crate::config::PartiriConfig;
use crate::error::Result;
use crate::output::{colored_job_status, format_datetime, JobRow};

pub fn run_list(client: &ApiClient, config: &PartiriConfig) -> Result<()> {
    let id = config.id_or_err()?;
    let jobs = client.list_service_jobs(id)?;

    if jobs.is_empty() {
        println!("No jobs found for this service.");
        return Ok(());
    }

    let mut jobs = jobs;
    jobs.sort_by(|a, b| b.created_at.cmp(&a.created_at));

    let rows: Vec<JobRow> = jobs
        .into_iter()
        .take(5)
        .map(|j| JobRow {
            job_type: j.job_type,
            deploy_ref: j
                .deploy_ref
                .as_deref()
                .map(|r| r.get(..7).unwrap_or(r).to_string())
                .unwrap_or_else(|| "".to_string()),
            status: colored_job_status(&j.status),
            created_at: j
                .created_at
                .as_deref()
                .map(format_datetime)
                .unwrap_or_else(|| "".to_string()),
        })
        .collect();

    println!("{}", Table::new(rows));
    Ok(())
}