trello-to-deck/src/lib.rs
Paul Campbell ce33f4ab20
Some checks failed
Test / build (map[name:stable]) (push) Successful in 1m47s
Test / build (map[name:nightly]) (push) Successful in 2m11s
Release Please / Release-plz (push) Failing after 16s
feat: enable trace logging support
2024-12-13 20:48:23 +00:00

196 lines
5 KiB
Rust

//
use std::path::PathBuf;
use clap::Parser;
use color_eyre::eyre::eyre;
pub use config::AppConfig;
use kxio::{fs::FileSystem, net::Net, print::Printer};
mod api_result;
mod check;
mod config;
mod init;
mod macros;
pub mod nextcloud;
mod template;
mod trello;
#[cfg(test)]
mod tests;
pub const NAME: &str = "trello-to-deck";
pub use kxio::kxeprintln as e;
pub use kxio::kxprintln as p;
use nextcloud::DeckClient;
#[derive(Parser, Debug)]
#[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())]
struct Commands {
#[clap(long, action = clap::ArgAction::SetTrue)]
log: bool,
#[clap(subcommand)]
command: Command,
}
#[derive(Parser, Debug)]
enum Command {
Init,
Check,
Import,
#[clap(subcommand)]
Trello(TrelloCommand),
#[clap(subcommand)]
Nextcloud(NextcloudCommand),
}
#[derive(Parser, Debug)]
enum NextcloudCommand {
#[clap(subcommand)]
Board(NextcloudBoardCommand),
#[clap(subcommand)]
Stack(NextcloudStackCommand),
#[clap(subcommand)]
Card(NextcloudCardCommand),
}
#[derive(Parser, Debug)]
enum NextcloudBoardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
#[derive(Parser, Debug)]
enum NextcloudStackCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
#[derive(Parser, Debug)]
enum NextcloudCardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
},
Get {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
card_id: i64,
},
}
#[derive(Parser, Debug)]
enum TrelloCommand {
#[clap(subcommand)]
Board(TrelloBoardCommand),
}
#[derive(Parser, Debug)]
enum TrelloBoardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
#[derive(Clone)]
pub struct Ctx {
pub fs: FileSystem,
pub net: Net,
pub prt: Printer,
}
impl Default for Ctx {
fn default() -> Self {
Self {
fs: kxio::fs::new(PathBuf::default()),
net: kxio::net::new(),
prt: kxio::print::standard(),
}
}
}
#[derive(Clone)]
pub struct FullCtx {
pub fs: FileSystem,
pub net: Net,
pub prt: Printer,
pub cfg: AppConfig,
}
impl FullCtx {
pub(crate) fn deck_client(&self) -> DeckClient {
DeckClient::new(self)
}
}
#[cfg_attr(test, mutants::skip)]
pub async fn run(ctx: Ctx) -> color_eyre::Result<()> {
color_eyre::install()?;
let commands = Commands::parse();
if commands.log {
tracing::subscriber::set_global_default(
tracing_subscriber::FmtSubscriber::builder()
.with_max_level(tracing::Level::TRACE)
.finish(),
)?;
tracing::info!("ready");
}
let cfg = AppConfig::load(&ctx);
match cfg {
Err(err) => {
if matches!(commands.command, Command::Init) {
init::run(&ctx)
} else {
Err(eyre!("Missing or invalid config: {err}"))
}
}
Ok(cfg) => {
let ctx = FullCtx {
fs: ctx.fs,
net: ctx.net,
prt: ctx.prt,
cfg,
};
match commands.command {
Command::Init => Err(eyre!("Config file already exists. Not overwriting it.")),
Command::Check => check::run(ctx).await,
Command::Import => todo!("import"),
Command::Trello(trello) => match trello {
TrelloCommand::Board(board) => match board {
TrelloBoardCommand::List { dump } => {
nextcloud::board::list(ctx, dump).await
}
},
},
Command::Nextcloud(nextcloud) => match nextcloud {
NextcloudCommand::Board(board) => match board {
NextcloudBoardCommand::List { dump } => {
nextcloud::board::list(ctx, dump).await
}
},
NextcloudCommand::Stack(stack) => match stack {
NextcloudStackCommand::List { dump } => {
nextcloud::stack::list(ctx, dump).await
}
},
NextcloudCommand::Card(card) => match card {
NextcloudCardCommand::List { dump, stack_id } => {
nextcloud::card::list(ctx, dump, stack_id.into()).await
}
NextcloudCardCommand::Get {
dump,
stack_id,
card_id,
} => nextcloud::card::get(ctx, dump, stack_id.into(), card_id.into()).await,
},
},
}
}
}
}