trello-to-deck/src/lib.rs

132 lines
3.1 KiB
Rust
Raw Normal View History

//
use std::path::PathBuf;
2024-12-01 07:13:23 +00:00
use clap::Parser;
use color_eyre::eyre::eyre;
use config::AppConfig;
2024-12-07 10:05:37 +00:00
use kxio::{fs::FileSystem, net::Net, print::Printer};
mod api_result;
2024-11-29 14:31:40 +00:00
mod check;
2024-11-29 19:19:36 +00:00
mod config;
mod conversion;
mod execute;
2024-12-19 09:04:23 +00:00
mod import;
mod init;
2024-11-29 17:43:49 +00:00
mod macros;
mod nextcloud;
mod template;
mod trello;
#[cfg(test)]
mod tests;
2024-11-29 17:43:49 +00:00
const NAME: &str = "trello-to-deck";
2024-11-29 14:31:40 +00:00
use crate::nextcloud::client::DeckClient;
use crate::trello::client::TrelloClient;
use execute::Execute;
use kxio::kxeprintln as e;
use kxio::kxprintln as p;
2024-12-07 10:05:37 +00:00
#[derive(Parser, Debug)]
#[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())]
pub struct Commands {
2024-12-08 10:51:19 +00:00
#[clap(long, action = clap::ArgAction::SetTrue)]
pub log: bool,
#[clap(subcommand)]
pub command: Command,
}
#[derive(Parser, Debug)]
pub enum Command {
/// Initialize the configuration
#[command(about = "Initialize configuration")]
Init,
/// Check the configuration and connection
#[command(about = "Check configuration and connection")]
Check,
/// Import boards from Trello to Nextcloud Deck
#[command(about = "Import boards from Trello to Nextcloud Deck")]
Import,
/// Trello-specific commands
#[command(about = "Trello-specific commands")]
#[clap(subcommand)]
Trello(trello::TrelloCommand),
/// Nextcloud-specific commands
#[command(about = "Nextcloud-specific commands")]
#[clap(subcommand)]
Nextcloud(nextcloud::NextcloudCommand),
}
#[derive(Clone)]
pub struct Ctx {
pub fs: FileSystem,
pub net: Net,
2024-12-07 10:05:37 +00:00
pub prt: Printer,
}
impl From<PathBuf> for Ctx {
fn from(base: PathBuf) -> Self {
Self {
fs: kxio::fs::new(base),
net: kxio::net::new(),
2024-12-07 10:05:37 +00:00
prt: kxio::print::standard(),
}
}
}
2024-11-29 14:31:40 +00:00
#[derive(Clone)]
pub(crate) struct FullCtx {
pub fs: FileSystem,
pub net: Net,
2024-12-07 10:05:37 +00:00
pub prt: Printer,
pub cfg: AppConfig,
}
impl FullCtx {
pub(crate) const fn deck_client(&self) -> DeckClient {
DeckClient::new(self)
}
pub(crate) const fn trello_client(&self) -> TrelloClient {
TrelloClient::new(self)
}
}
2024-11-29 14:31:40 +00:00
#[cfg_attr(test, mutants::skip)]
pub async fn run(ctx: &Ctx, commands: &Commands) -> color_eyre::Result<()> {
2024-12-08 10:51:19 +00:00
if commands.log {
tracing::subscriber::set_global_default(
tracing_subscriber::FmtSubscriber::builder()
.with_max_level(tracing::Level::TRACE)
.finish(),
)?;
tracing::info!("ready");
}
2024-12-15 20:07:34 +00:00
let cfg = AppConfig::load(ctx);
match cfg {
Err(err) => {
if matches!(commands.command, Command::Init) {
2024-12-15 20:07:34 +00:00
init::run(ctx)
} else {
Err(eyre!("Missing or invalid config: {err}"))
}
}
Ok(cfg) => {
commands
.command
2024-12-15 20:07:34 +00:00
.execute(&FullCtx {
fs: ctx.fs.clone(),
net: ctx.net.clone(),
prt: ctx.prt.clone(),
cfg,
})
.await
}
}
2024-11-29 14:31:40 +00:00
}