quicknode-cli 0.1.3

Command-line interface for the Quicknode SDK
Documentation
//! `qn chain …` — list supported blockchains.

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};

#[derive(Debug, ClapArgs)]
pub struct Args {
    #[command(subcommand)]
    pub cmd: ChainCmd,
}

#[derive(Debug, Subcommand)]
pub enum ChainCmd {
    /// List supported chains and their networks.
    #[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 = 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)
    }
}