trello-to-deck/src/lib.rs

148 lines
3.4 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;
pub 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 19:19:36 +00:00
mod config;
mod init;
2024-11-29 17:43:49 +00:00
mod macros;
pub mod nextcloud;
mod template;
mod trello;
#[cfg(test)]
mod tests;
2024-11-29 17:43:49 +00:00
2024-11-29 14:31:40 +00:00
pub const NAME: &str = "trello-to-deck";
2024-12-07 10:05:37 +00:00
pub use kxio::kxeprintln as e;
pub use kxio::kxprintln as p;
use nextcloud::DeckClient;
2024-12-07 10:05:37 +00:00
#[derive(Parser, Debug)]
#[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())]
struct Commands {
#[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),
}
#[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 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,
2024-12-07 10:05:37 +00:00
pub prt: Printer,
}
impl Default for Ctx {
fn default() -> Self {
Self {
fs: kxio::fs::new(PathBuf::default()),
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 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) fn deck_client(&self) -> DeckClient {
DeckClient::new(self)
}
}
2024-11-29 14:31:40 +00:00
#[cfg_attr(test, mutants::skip)]
pub async fn run(ctx: Ctx) -> color_eyre::Result<()> {
2024-11-29 14:31:40 +00:00
color_eyre::install()?;
let commands = Commands::parse();
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,
2024-12-07 10:05:37 +00:00
prt: ctx.prt,
cfg,
};
match commands.command {
Command::Init => Err(eyre!("Config file already exists. Not overwriting it.")),
Command::Check => todo!("check"),
Command::Import => todo!("import"),
Command::Trello(TrelloCommand::Board(TrelloBoardCommand::List { dump })) => {
trello::boards::list(ctx, dump).await
}
Command::Nextcloud(NextcloudCommand::Board(NextcloudBoardCommand::List {
dump,
})) => nextcloud::board::list(ctx, dump).await,
Command::Nextcloud(NextcloudCommand::Stack(NextcloudStackCommand::List {
dump,
})) => nextcloud::stack::list(ctx, dump).await,
}
}
}
2024-11-29 14:31:40 +00:00
}