pdns-cli 0.0.2

Rust client library and CLI for the PowerDNS Authoritative Server API
Documentation
//! the servers subcommand module

use clap::Subcommand;

/// Server level commands supported by the CLI
#[derive(Subcommand, Debug)]
pub(crate) enum ServerCommands {
    /// command to list all servers
    List {
        /// the server id to fetch information of - fetches all if unspecified
        #[arg(short, long)]
        server_id: Option<String>,
    },
}

impl ServerCommands {
    /// dispatcher for server subcommands
    pub(crate) fn dispatch(
        &self,
        client: &pdns_client::Client,
    ) -> Result<(), Box<dyn std::error::Error>> {
        match self {
            ServerCommands::List { server_id } => list_servers(client, server_id.as_ref())?,
        }

        Ok(())
    }
}

/// lists information of server corresponding to `server_id` if specified, else all servers
/// printed in tabled form
fn list_servers(
    client: &pdns_client::Client,
    server_id: Option<&String>,
) -> Result<(), Box<dyn std::error::Error>> {
    if let Some(server_id) = server_id {
        let server = client.list_server(server_id)?;
        println!("{}", tabled::Table::new([server])); // need to put into a slice for using tabled
    } else {
        let servers = client.list_servers()?;
        println!("{}", tabled::Table::new(&servers));
    }

    Ok(())
}