pub mod api;
use std::sync::Arc;
use anyhow::Result;
use colored::Colorize;
use serde_json::{json, Value};
use toon_format::encode_default as toon_encode;
use crate::config::OutputFormat;
use crate::execution::{fan_out, ExecutionTarget};
use crate::render;
use api::{Preset, PresetsApi};
fn preset_to_json(preset: &Preset, include_profile: bool, profile: &str) -> Value {
let mut v = json!({
"id": preset.id,
"name": preset.display_name(),
"connector_type": preset.display_connector_type(),
"is_default": preset.is_default,
"is_custom": preset.is_custom,
});
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert("profile".to_string(), Value::String(profile.to_string()));
}
}
v
}
fn read_from_file(path: &str) -> Result<Value> {
let raw = if path == "-" {
eprintln!("{}", "Reading preset definition from stdin...".dimmed());
use std::io::Read;
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf)?;
buf
} else {
eprintln!(
"{}",
format!("Reading preset definition from {path}...").dimmed()
);
std::fs::read_to_string(path)?
};
Ok(serde_json::from_str(&raw)?)
}
pub async fn run_list(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
eprintln!("{}", "Fetching presets...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = PresetsApi::new(&t.client);
Ok(api.list().await?)
})
.await;
let mut all_json: Vec<Value> = Vec::new();
let mut all_items: Vec<(String, Preset)> = Vec::new();
for (profile, result) in per_profile {
match result {
Ok(resp) => {
for preset in resp.presets {
all_json.push(preset_to_json(&preset, include_profile, &profile));
all_items.push((profile.clone(), preset));
}
}
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
match output {
OutputFormat::Json => render::render_json(&all_json)?,
OutputFormat::Agents => {
let toon =
toon_encode(&all_json).map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
if all_items.is_empty() {
render::print_no_results("No presets found.");
return Ok(());
}
let rows: Vec<Vec<String>> = all_items
.iter()
.map(|(profile, preset)| {
vec![
profile.clone(),
preset.id.clone().unwrap_or_default(),
preset.display_name().to_string(),
preset.display_connector_type(),
render::bool_display(preset.is_default),
render::bool_display(preset.is_custom),
]
})
.collect();
render::render_table(
&["ID", "Name", "Connector Type", "Default", "Custom"],
rows,
include_profile,
);
}
}
Ok(())
}
pub async fn run_get(
targets: &[Arc<ExecutionTarget>],
id: &str,
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", format!("Fetching preset {id}...").dimmed());
let include_profile = targets.len() > 1;
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = PresetsApi::new(&t.client);
Ok(api.get(&id).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, result) in per_profile {
match result {
Ok(mut val) => {
if include_profile {
render::tag_get_result(&mut val, &profile);
}
all_results.push(val);
}
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Agents => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
render::render_get_text(
&all_results,
include_profile,
"Preset not found.",
None::<&dyn Fn(&Value)>,
)?;
}
}
Ok(())
}
pub async fn run_create(
targets: &[Arc<ExecutionTarget>],
from_file: &str,
output: OutputFormat,
) -> Result<()> {
let body = read_from_file(from_file)?;
eprintln!("{}", "Creating preset...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| {
let body = body.clone();
async move {
let api = PresetsApi::new(&t.client);
Ok(api.create(&body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, result) in per_profile {
match result {
Ok(resp) => {
if let Some(preset) = resp.preset {
eprintln!(
"{}",
format!(
"Created preset '{}' in profile '{profile}'.",
preset.display_name()
)
.green()
);
all_results.push(preset_to_json(&preset, include_profile, &profile));
}
}
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Agents => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {}
}
Ok(())
}
pub async fn run_update(
targets: &[Arc<ExecutionTarget>],
from_file: &str,
output: OutputFormat,
) -> Result<()> {
let body = read_from_file(from_file)?;
eprintln!("{}", "Updating preset...".dimmed());
let per_profile = fan_out(targets, |t| {
let body = body.clone();
async move {
let api = PresetsApi::new(&t.client);
Ok(api.replace(&body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, result) in per_profile {
match result {
Ok(val) => {
eprintln!(
"{}",
format!("Updated preset in profile '{profile}'.").green()
);
all_results.push(val);
}
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Agents => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {}
}
Ok(())
}
pub async fn run_delete(targets: &[Arc<ExecutionTarget>], id: &str) -> Result<()> {
eprintln!("{}", format!("Deleting preset {id}...").dimmed());
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = PresetsApi::new(&t.client);
api.delete(&id).await?;
Ok(())
}
})
.await;
for (profile, result) in per_profile {
match result {
Ok(()) => eprintln!(
"{}",
format!("Preset {id} deleted in profile '{profile}'.").green()
),
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
Ok(())
}
pub async fn run_set_default(targets: &[Arc<ExecutionTarget>], id: &str) -> Result<()> {
eprintln!("{}", format!("Setting preset {id} as default...").dimmed());
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = PresetsApi::new(&t.client);
api.set_default(&id).await?;
Ok(())
}
})
.await;
for (profile, result) in per_profile {
match result {
Ok(()) => eprintln!(
"{}",
format!("Preset {id} set as default in profile '{profile}'.").green()
),
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
Ok(())
}