pub mod claim;
pub mod config;
pub mod create;
pub mod info;
pub mod new;
pub mod program;
pub mod reply;
pub mod send;
pub mod transfer;
pub mod update;
pub mod upload;
pub mod wallet;
use std::time::Duration;
pub use self::{
claim::Claim,
config::{Config, ConfigSettings},
create::Create,
info::Info,
new::New,
program::Program,
reply::Reply,
send::Send,
transfer::Transfer,
update::Update,
upload::Upload,
wallet::Wallet,
};
use crate::App;
use clap::Parser;
#[derive(Clone, Debug, Parser)]
pub enum Command {
Claim(Claim),
Create(Create),
Info(Info),
New(New),
Config(Config),
Program(Program),
Reply(Reply),
Send(Send),
Upload(Upload),
Transfer(Transfer),
Update(Update),
#[clap(subcommand)]
Wallet(Wallet),
}
impl Command {
pub async fn exec(&self, app: &impl App) -> anyhow::Result<()> {
match self {
Command::Config(config) => config.exec()?,
Command::New(new) => new.exec().await?,
Command::Program(program) => program.exec(app).await?,
Command::Update(update) => update.exec().await?,
Command::Claim(claim) => claim.exec(app).await?,
Command::Create(create) => create.exec(app).await?,
Command::Info(info) => info.exec(app).await?,
Command::Send(send) => send.exec(app).await?,
Command::Upload(upload) => upload.exec(app).await?,
Command::Transfer(transfer) => transfer.exec(app).await?,
Command::Reply(reply) => reply.exec(app).await?,
Command::Wallet(wallet) => wallet.run()?,
}
Ok(())
}
}
#[derive(Debug, Parser)]
#[clap(author, version)]
#[command(name = "gcli")]
pub struct Opt {
#[command(subcommand)]
pub command: Command,
#[arg(short, long, default_value = "60000")]
pub timeout: u64,
#[clap(short, long, action = clap::ArgAction::Count)]
pub verbose: u8,
#[arg(short, long)]
pub endpoint: Option<String>,
#[arg(short, long)]
pub passwd: Option<String>,
}
#[async_trait::async_trait]
impl App for Opt {
fn timeout(&self) -> Duration {
Duration::from_millis(self.timeout)
}
fn verbose(&self) -> u8 {
self.verbose
}
fn endpoint(&self) -> Option<String> {
if self.endpoint.is_some() {
return self.endpoint.clone();
}
ConfigSettings::read(None).ok().map(|c| c.url.to_string())
}
fn passwd(&self) -> Option<String> {
self.passwd.clone()
}
async fn exec(&self) -> anyhow::Result<()> {
self.command.exec(self).await
}
}
impl Opt {
pub fn exec_sync(&self) -> color_eyre::Result<()> {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(self.run())
}
}