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, report_errors_and_collect_successes, ExecutionTarget};
use crate::render;
use api::{View, ViewFolder, ViewsApi};
fn view_to_json(view: &View, include_profile: bool, profile: &str) -> Value {
let mut v = json!({
"id": view.id,
"name": view.name,
"folder_id": view.folder_id,
"created_at": view.created_at,
});
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert("profile".to_string(), Value::String(profile.to_string()));
}
}
v
}
fn folder_to_json(folder: &ViewFolder, include_profile: bool, profile: &str) -> Value {
let mut v = json!({
"id": folder.id,
"name": folder.name,
"parent_id": folder.parent_id,
});
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert("profile".to_string(), Value::String(profile.to_string()));
}
}
v
}
fn view_id_from_response(resp: &Value) -> Option<String> {
fn at(v: &Value, pointer: &str) -> Option<String> {
match v.pointer(pointer)? {
Value::String(s) => Some(s.clone()),
Value::Number(n) => Some(n.to_string()),
_ => None,
}
}
at(resp, "/view/id").or_else(|| at(resp, "/id"))
}
fn read_from_file(path: &str) -> Result<Value> {
let raw = if path == "-" {
eprintln!("{}", "Reading 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 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 views...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = ViewsApi::new(&t.client);
Ok(api.list().await?)
})
.await;
let mut all_json: Vec<Value> = Vec::new();
let mut all_items: Vec<(String, View)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
if !resp.views.is_empty() {
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::views_url(b)
})
.await;
}
for view in resp.views {
all_json.push(view_to_json(&view, include_profile, &profile));
all_items.push((profile.clone(), view));
}
}
match output {
OutputFormat::Json => render::render_json(&all_json)?,
OutputFormat::Toon => {
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 views found.");
return Ok(());
}
let rows: Vec<Vec<String>> = all_items
.iter()
.map(|(profile, view)| {
vec![
profile.clone(),
view.id.clone().unwrap_or_default(),
view.display_name().to_string(),
view.folder_id.clone().unwrap_or_default(),
view.created_at.clone().unwrap_or_default(),
]
})
.collect();
render::render_table(&["ID", "Name", "Folder", "Created"], rows, include_profile);
}
}
Ok(())
}
pub async fn run_get(
targets: &[Arc<ExecutionTarget>],
id: &str,
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", format!("Fetching view {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 = ViewsApi::new(&t.client);
Ok(api.get(&id).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, mut val) in report_errors_and_collect_successes(per_profile)? {
if include_profile {
render::tag_get_result(&mut val, &profile);
}
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::view_url(b, &id)
})
.await;
all_results.push(val);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
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,
"View 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)?;
let name = body
.get("name")
.and_then(|v| v.as_str())
.unwrap_or("<unnamed>")
.to_string();
eprintln!("{}", "Creating view...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| {
let body = body.clone();
async move {
let api = ViewsApi::new(&t.client);
Ok(api.create(&body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, mut resp) in report_errors_and_collect_successes(per_profile)? {
let created_id = view_id_from_response(&resp);
render::print_created(
"Created",
"view",
Some(&name),
created_id.as_deref(),
&profile,
);
if let Some(id) = &created_id {
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::view_url(b, id)
})
.await;
}
if include_profile {
render::tag_get_result(&mut resp, &profile);
}
all_results.push(resp);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
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>],
id: &str,
from_file: &str,
output: OutputFormat,
) -> Result<()> {
let body = read_from_file(from_file)?;
eprintln!("{}", format!("Updating view {id}...").dimmed());
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let body = body.clone();
let id = id.clone();
async move {
let api = ViewsApi::new(&t.client);
Ok(api.replace(&id, &body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, val) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Updated view in profile '{profile}'.").green()
);
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::view_url(b, &id)
})
.await;
all_results.push(val);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
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 view {id}...").dimmed());
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = ViewsApi::new(&t.client);
api.delete(&id).await?;
Ok(())
}
})
.await;
for (profile, ()) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("View {id} deleted in profile '{profile}'.").green()
);
}
Ok(())
}
pub async fn run_folders_list(
targets: &[Arc<ExecutionTarget>],
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", "Fetching view folders...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = ViewsApi::new(&t.client);
Ok(api.list_folders().await?)
})
.await;
let mut all_json: Vec<Value> = Vec::new();
let mut all_items: Vec<(String, ViewFolder)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
for folder in resp.folders {
all_json.push(folder_to_json(&folder, include_profile, &profile));
all_items.push((profile.clone(), folder));
}
}
match output {
OutputFormat::Json => render::render_json(&all_json)?,
OutputFormat::Toon => {
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 view folders found.");
return Ok(());
}
let rows: Vec<Vec<String>> = all_items
.iter()
.map(|(profile, folder)| {
vec![
profile.clone(),
folder.id.clone().unwrap_or_default(),
folder.display_name().to_string(),
folder.parent_id.clone().unwrap_or_default(),
]
})
.collect();
render::render_table(&["ID", "Name", "Parent"], rows, include_profile);
}
}
Ok(())
}
pub async fn run_folders_get(
targets: &[Arc<ExecutionTarget>],
id: &str,
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", format!("Fetching view folder {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 = ViewsApi::new(&t.client);
Ok(api.get_folder(&id).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, mut val) in report_errors_and_collect_successes(per_profile)? {
if include_profile {
render::tag_get_result(&mut val, &profile);
}
all_results.push(val);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
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,
"View folder not found.",
None::<&dyn Fn(&Value)>,
)?;
}
}
Ok(())
}
pub async fn run_folders_create(
targets: &[Arc<ExecutionTarget>],
from_file: &str,
output: OutputFormat,
) -> Result<()> {
let body = read_from_file(from_file)?;
eprintln!("{}", "Creating view folder...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| {
let body = body.clone();
async move {
let api = ViewsApi::new(&t.client);
Ok(api.create_folder(&body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
if let Some(folder) = resp.into_folder() {
eprintln!(
"{}",
format!(
"Created folder '{}' in profile '{profile}'.",
folder.display_name()
)
.green()
);
all_results.push(folder_to_json(&folder, include_profile, &profile));
}
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {}
}
Ok(())
}
pub async fn run_folders_update(
targets: &[Arc<ExecutionTarget>],
id: &str,
from_file: &str,
output: OutputFormat,
) -> Result<()> {
let body = read_from_file(from_file)?;
eprintln!("{}", format!("Updating view folder {id}...").dimmed());
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let body = body.clone();
let id = id.clone();
async move {
let api = ViewsApi::new(&t.client);
Ok(api.replace_folder(&id, &body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, val) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Updated folder in profile '{profile}'.").green()
);
all_results.push(val);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {}
}
Ok(())
}
pub async fn run_folders_delete(targets: &[Arc<ExecutionTarget>], id: &str) -> Result<()> {
eprintln!("{}", format!("Deleting view folder {id}...").dimmed());
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = ViewsApi::new(&t.client);
api.delete_folder(&id).await?;
Ok(())
}
})
.await;
for (profile, ()) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Folder {id} deleted in profile '{profile}'.").green()
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn view_id_from_response_reads_wrapped_shape() {
let resp = json!({ "view": { "id": "v-001", "name": "My View" } });
assert_eq!(view_id_from_response(&resp).as_deref(), Some("v-001"));
}
#[test]
fn view_id_from_response_reads_bare_shape() {
let resp = json!({ "id": "v-002", "name": "My View" });
assert_eq!(view_id_from_response(&resp).as_deref(), Some("v-002"));
}
#[test]
fn view_id_from_response_reads_numeric_id() {
let resp = json!({ "view": { "id": 42 } });
assert_eq!(view_id_from_response(&resp).as_deref(), Some("42"));
}
#[test]
fn view_id_from_response_none_when_id_absent() {
let resp = json!({});
assert_eq!(view_id_from_response(&resp), None);
}
}