use clap::{Args as ClapArgs, Subcommand};
use comfy_table::Cell;
use serde::Serialize;
use crate::context::Ctx;
use crate::errors::CliError;
use crate::output::{new_table, set_header_bold, write_table, Render};
use crate::retry::retrying;
#[derive(Debug, ClapArgs)]
pub struct Args {
#[command(subcommand)]
pub cmd: ChainCmd,
}
#[derive(Debug, Subcommand)]
pub enum ChainCmd {
#[command(visible_alias = "ls")]
List,
}
pub async fn run(args: Args, ctx: Ctx) -> Result<(), CliError> {
match args.cmd {
ChainCmd::List => list(ctx).await,
}
}
async fn list(ctx: Ctx) -> Result<(), CliError> {
let resp = retrying(ctx.global.retries, || ctx.sdk.admin.list_chains()).await?;
crate::output::emit(&ctx.out, &ChainsView(resp))
}
#[derive(Serialize)]
struct ChainsView(quicknode_sdk::admin::ListChainsResponse);
impl Render for ChainsView {
fn render_table(
&self,
w: &mut dyn std::io::Write,
ctx: &crate::output::OutputCtx,
) -> std::io::Result<()> {
let mut t = new_table(ctx);
set_header_bold(&mut t, ctx, vec!["CHAIN", "NETWORKS"]);
for c in &self.0.data {
let nets = c
.networks
.iter()
.map(|n| n.slug.clone())
.collect::<Vec<_>>()
.join(", ");
t.add_row(vec![Cell::new(&c.slug), Cell::new(nets)]);
}
write_table(w, &t)
}
}