fundaia 0.53.0

Command line for the Fundaia deployment platform: projects, services, variables, deployments, logs and metrics
use anyhow::Result;
use owo_colors::OwoColorize;

use crate::api::client::ApiClient;
use crate::api::models::Variable;
use crate::output::Format;
use crate::render::sanitise;
use crate::render::table::{Cell, Table};
use crate::render::theme;

pub async fn list(client: &ApiClient, project_reference: &str, format: Format) -> Result<()> {
    let project = client.find_project(project_reference).await?;
    let answer = client.shared_variables(&project.id).await?;

    if format.answered_in_json(&answer.raw) {
        return Ok(());
    }

    let page = &answer.body;
    let mut table = Table::new(&["variable", "valor", "dónde"]);
    for variable in &page.environment.variables {
        table.push(row(variable, &format!("solo {}", page.environment.name)));
    }
    for variable in &page.project {
        table.push(row(variable, "todos los entornos"));
    }

    table.print_or(format_args!(
        "`{}` no comparte variables. fundaia shared set {} CLAVE valor",
        project.name, project.slug
    ));
    Ok(())
}

fn row(variable: &Variable, scope: &str) -> Vec<Cell> {
    vec![
        Cell::styled(sanitise::plain(&variable.key), theme::strong()),
        Cell::styled(
            variable
                .preview
                .as_deref()
                .map_or_else(|| "••••••••".to_owned(), sanitise::plain),
            theme::muted(),
        ),
        Cell::plain(scope.to_owned()),
    ]
}

pub struct SetRequest<'a> {
    pub project: &'a str,
    pub key: &'a str,
    pub value: &'a str,
    pub secret: bool,
    pub environment_only: bool,
}

pub async fn set(client: &ApiClient, request: SetRequest<'_>, format: Format) -> Result<()> {
    let project = client.find_project(request.project).await?;
    let answer = client
        .set_shared_variable(
            &project.id,
            request.key,
            request.value,
            request.secret,
            request.environment_only,
        )
        .await?;

    if format.answered_in_json(&answer.raw) {
        return Ok(());
    }

    println!(
        "  {} {} {}",
        theme::CHECK.style(theme::success()),
        request.key.style(theme::strong()),
        if request.environment_only {
            "compartida en este entorno"
        } else {
            "compartida en todos los entornos"
        }
        .style(theme::muted()),
    );
    println!(
        "  {}",
        "Cada servicio la recibe en su siguiente despliegue; una clave propia del servicio gana."
            .style(theme::muted()),
    );
    Ok(())
}

pub async fn unset(
    client: &ApiClient,
    project_reference: &str,
    key: &str,
    environment_only: bool,
    format: Format,
) -> Result<()> {
    let project = client.find_project(project_reference).await?;
    let answer = client
        .unset_shared_variable(&project.id, key, environment_only)
        .await?;

    if format.answered_in_json(&answer.raw) {
        return Ok(());
    }

    println!(
        "  {} {} ya no se comparte",
        theme::CHECK.style(theme::success()),
        key.style(theme::strong()),
    );
    Ok(())
}