bunny-api 0.0.5

Alpha API client for Bunny.net
Documentation
use std::path::PathBuf;

use clap::Parser;

use bunny_api::{Client, ClientError};
use bunny_api::edge::{Error as EdgeError, StorageZone, upload_file_by_path};

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub enum Error {
    Client(#[from] ClientError),
    Edge(#[from] bunny_api::Error<EdgeError>),
}

#[derive(Debug, Parser)]
pub struct EdgeCommon {
    /// The Storage API key found under "FTP & API Access".
    #[clap(long, env)]
    sz_access_key: String,
    #[clap(long, env)]
    storage_zone: StorageZone,
    #[clap(long, env)]
    zone_name: String,
}

impl EdgeCommon {
    pub fn client(&self) -> Result<Client, ClientError> {
        Client::new(&self.sz_access_key)
    }
}

#[derive(Debug, Parser)]
pub enum Cli {
    Upload {
        #[clap(flatten)]
        common: EdgeCommon,
        #[clap(short, long, env)]
        dest: String,
        #[clap(short, long, env)]
        source: PathBuf,
        #[clap(long, env)]
        sha256: Option<String>,
    }
}

impl Cli {
    pub async fn run(self) -> Result<(), Error> {
        match self {
            Self::Upload { common, dest, source, sha256 } => {
                let client = common.client()?;
                upload_file_by_path(&client, common.storage_zone, &common.zone_name, &dest, &source, sha256)
                    .await
                    .map_err(|error| error.map(EdgeError::from))
                    .map_err(Error::from)
            }
        }
    }
}