refactor: reshuffling and extracting Executor trait

This commit is contained in:
Paul Campbell 2024-12-08 17:23:54 +00:00
parent bbb2d7eedf
commit bff737a996
26 changed files with 619 additions and 485 deletions

View file

@ -3,7 +3,7 @@ use kxio::{net::Response, print::Printer};
use crate::{e, s};
pub struct APIResult<T> {
pub(crate) struct APIResult<T> {
pub text: String,
pub result: Result<T, kxio::net::Error>,
}

View file

@ -1,31 +1,9 @@
//
use color_eyre::Result;
use crate::{
f,
nextcloud::model::{NextcloudBoardId, NextcloudHostname, NextcloudPassword, NextcloudUsername},
s,
trello::types::{
auth::{TrelloApiKey, TrelloApiSecret},
TrelloBoardName,
},
Ctx, NAME,
};
#[derive(Clone, Debug, derive_more::From, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)]
pub struct TrelloConfig {
pub(crate) api_key: TrelloApiKey,
pub(crate) api_secret: TrelloApiSecret,
pub(crate) board_name: TrelloBoardName,
}
#[derive(Clone, Debug, derive_more::From, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)]
pub struct NextcloudConfig {
pub(crate) hostname: NextcloudHostname,
pub(crate) username: NextcloudUsername,
pub(crate) password: NextcloudPassword,
pub(crate) board_id: NextcloudBoardId,
}
use crate::nextcloud::NextcloudConfig;
use crate::trello::TrelloConfig;
use crate::{f, s, Ctx, NAME};
#[derive(
Clone,
@ -38,12 +16,12 @@ pub struct NextcloudConfig {
derive_more::AsRef,
serde::Deserialize,
)]
pub struct AppConfig {
pub(crate) struct AppConfig {
pub(crate) trello: TrelloConfig,
pub(crate) nextcloud: NextcloudConfig,
}
impl AppConfig {
pub fn load(ctx: &Ctx) -> Result<Self> {
pub(crate) fn load(ctx: &Ctx) -> Result<Self> {
let file = ctx.fs.base().join(f!("{NAME}.toml"));
let str = ctx.fs.file(&file).reader()?;
Ok(toml::from_str(s!(str).as_str())?)

21
src/execute.rs Normal file
View file

@ -0,0 +1,21 @@
//
use color_eyre::eyre::eyre;
use color_eyre::Result;
use crate::FullCtx;
pub(crate) trait Execute {
async fn execute(self, ctx: FullCtx) -> Result<()>;
}
impl Execute for crate::Command {
async fn execute(self, ctx: FullCtx) -> Result<()> {
match self {
Self::Init => Err(eyre!("Config file already exists. Not overwriting it.")),
Self::Check => crate::check::run(ctx).await,
Self::Import => todo!(), //crate::import::run(ctx).await,
Self::Trello(cmd) => cmd.execute(ctx).await,
Self::Nextcloud(cmd) => cmd.execute(ctx).await,
}
}
}

View file

@ -3,26 +3,30 @@ use std::path::PathBuf;
use clap::Parser;
use color_eyre::eyre::eyre;
pub use config::AppConfig;
use config::AppConfig;
use kxio::{fs::FileSystem, net::Net, print::Printer};
mod api_result;
mod check;
mod config;
mod execute;
mod init;
mod macros;
pub mod nextcloud;
mod nextcloud;
mod template;
mod trello;
#[cfg(test)]
mod tests;
pub const NAME: &str = "trello-to-deck";
const NAME: &str = "trello-to-deck";
pub use kxio::kxeprintln as e;
pub use kxio::kxprintln as p;
use nextcloud::DeckClient;
use crate::nextcloud::client::DeckClient;
use crate::trello::client::TrelloClient;
use execute::Execute;
use kxio::kxeprintln as e;
use kxio::kxprintln as p;
#[derive(Parser, Debug)]
#[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())]
@ -34,77 +38,27 @@ struct Commands {
}
#[derive(Parser, Debug)]
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,
#[clap(subcommand)]
Trello(TrelloCommand),
#[clap(subcommand)]
Nextcloud(NextcloudCommand),
}
#[derive(Parser, Debug)]
enum NextcloudCommand {
/// Trello-specific commands
#[command(about = "Trello-specific commands")]
#[clap(subcommand)]
Board(NextcloudBoardCommand),
Trello(trello::TrelloCommand),
/// Nextcloud-specific commands
#[command(about = "Nextcloud-specific commands")]
#[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,
},
Create {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
#[clap(long)]
title: String,
#[clap(long)]
description: Option<String>,
},
}
#[derive(Parser, Debug)]
enum TrelloCommand {
#[clap(subcommand)]
Board(TrelloBoardCommand),
}
#[derive(Parser, Debug)]
enum TrelloBoardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
Nextcloud(nextcloud::NextcloudCommand),
}
#[derive(Clone)]
@ -124,16 +78,20 @@ impl Default for Ctx {
}
#[derive(Clone)]
pub struct FullCtx {
pub fs: FileSystem,
pub(crate) struct FullCtx {
// pub fs: FileSystem,
pub net: Net,
pub prt: Printer,
pub cfg: AppConfig,
}
impl FullCtx {
pub fn deck_client(&self) -> DeckClient {
pub(crate) fn deck_client(&self) -> DeckClient {
DeckClient::new(self)
}
pub(crate) fn trello_client(&self) -> TrelloClient {
TrelloClient::new(self)
}
}
#[cfg_attr(test, mutants::skip)]
@ -160,63 +118,15 @@ pub async fn run(ctx: Ctx) -> color_eyre::Result<()> {
}
}
Ok(cfg) => {
let ctx = FullCtx {
fs: ctx.fs,
commands
.command
.execute(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,
NextcloudCardCommand::Create {
dump,
stack_id,
title,
description,
} => {
nextcloud::card::create(
ctx,
nextcloud::card::Create {
dump,
stack_id,
title,
description,
},
)
})
.await
}
},
},
}
}
}
}

View file

@ -1,7 +1,26 @@
//
use clap::Parser;
use crate::execute::Execute;
use crate::{p, FullCtx};
pub async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
#[derive(Parser, Debug)]
pub(crate) enum NextcloudBoardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
impl Execute for NextcloudBoardCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::List { dump } => list(&ctx, dump).await,
}
}
}
pub(crate) async fn list(ctx: &FullCtx, dump: bool) -> color_eyre::Result<()> {
let api_result = ctx.deck_client().get_boards().await;
if dump {
p!(ctx.prt, "{}", api_result.text);

View file

@ -1,11 +1,78 @@
//
use clap::Parser;
use crate::execute::Execute;
use crate::{
nextcloud::model::{NextcloudCardId, NextcloudStackId},
p, FullCtx,
};
#[derive(Parser, Debug)]
pub(crate) 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,
},
Create {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
#[clap(long)]
title: String,
#[clap(long)]
description: Option<String>,
},
}
impl Execute for NextcloudCardCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::List { dump, stack_id } => {
list(&ctx, dump, NextcloudStackId::from(stack_id)).await
}
Self::Get {
dump,
stack_id,
card_id,
} => {
get(
&ctx,
dump,
NextcloudStackId::from(stack_id),
NextcloudCardId::from(card_id),
)
.await
}
Self::Create {
dump,
stack_id,
title,
description,
} => {
create(
ctx,
Create {
dump,
stack_id,
title,
description,
},
)
.await
}
}
}
}
pub(crate) async fn list(
ctx: FullCtx,
ctx: &FullCtx,
dump: bool,
stack_id: NextcloudStackId,
) -> color_eyre::Result<()> {
@ -26,7 +93,7 @@ pub(crate) async fn list(
}
pub(crate) async fn get(
ctx: FullCtx,
ctx: &FullCtx,
dump: bool,
stack_id: NextcloudStackId,
card_id: NextcloudCardId,
@ -45,10 +112,10 @@ pub(crate) async fn get(
}
pub(crate) struct Create {
pub dump: bool,
pub stack_id: i64,
pub title: String,
pub description: Option<String>,
pub(crate) dump: bool,
pub(crate) stack_id: i64,
pub(crate) title: String,
pub(crate) description: Option<String>,
}
pub(crate) async fn create(ctx: FullCtx, create: Create) -> color_eyre::Result<()> {
let dc = ctx.deck_client();

143
src/nextcloud/client.rs Normal file
View file

@ -0,0 +1,143 @@
use crate::api_result::APIResult;
use crate::nextcloud::card::Create;
use crate::nextcloud::model::{
Board, Card, NextcloudBoardId, NextcloudCardId, NextcloudHostname, NextcloudPassword,
NextcloudStackId, NextcloudUsername, Stack,
};
use crate::{f, FullCtx};
use bytes::Bytes;
use kxio::net::{Net, ReqBuilder};
pub(crate) struct DeckClient<'ctx> {
ctx: &'ctx FullCtx,
hostname: &'ctx NextcloudHostname,
username: &'ctx NextcloudUsername,
password: &'ctx NextcloudPassword,
}
// Uses the API described here: https://deck.readthedocs.io/en/stable/API/#cards
impl<'ctx> DeckClient<'ctx> {
pub fn new(ctx: &'ctx FullCtx) -> Self {
Self {
ctx,
hostname: &ctx.cfg.nextcloud.hostname,
username: &ctx.cfg.nextcloud.username,
password: &ctx.cfg.nextcloud.password,
}
}
fn url(&self, path: impl Into<String>) -> String {
f!(
"https://{}/index.php/apps/deck/api/v1.0/{}",
self.hostname,
path.into()
)
}
async fn request<T: for<'a> serde::Deserialize<'a>>(
&self,
url: impl Into<String>,
custom: fn(&Net, String) -> ReqBuilder,
) -> APIResult<T> {
APIResult::new(
custom(&self.ctx.net, self.url(url))
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json")
.header("content-type", "application/json")
.send()
.await,
&self.ctx.prt,
)
.await
}
async fn request_with_body<T: for<'a> serde::Deserialize<'a>>(
&self,
url: impl Into<String>,
body: impl Into<Bytes>,
custom: fn(&Net, String) -> ReqBuilder,
) -> APIResult<T> {
APIResult::new(
custom(&self.ctx.net, self.url(url))
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json")
.header("content-type", "application/json")
.body(body)
.send()
.await,
&self.ctx.prt,
)
.await
}
pub(crate) async fn get_boards(&self) -> APIResult<Vec<Board>> {
self.request("boards", |net, url| net.get(url)).await
}
pub(crate) async fn get_board(&self, board_id: NextcloudBoardId) -> APIResult<Board> {
self.request(f!("boards/{board_id}"), |net, url| net.get(url))
.await
}
// pub(crate) async fn create_board(&self, title: &str, color: &str) -> APIResult<Board> {
// self.request_with_body(
// "boards",
// serde_json::json!({
// "title": title,
// "color": color
// })
// .to_string(),
// |net, url| net.post(url),
// )
// .await
// }
pub(crate) async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult<Vec<Stack>> {
self.request(f!("boards/{board_id}/stacks"), |net, url| net.get(url))
.await
}
pub(crate) async fn get_stack(
&self,
board_id: NextcloudBoardId,
stack_id: NextcloudStackId,
) -> APIResult<Stack> {
self.request(f!("boards/{board_id}/stacks/{stack_id}"), |net, url| {
net.get(url)
})
.await
}
pub(crate) async fn create_card(&self, create: &Create) -> APIResult<Card> {
let mut body = serde_json::json!({
"title": create.title,
});
if let Some(desc) = &create.description {
body["description"] = serde_json::Value::String(desc.to_string());
}
self.request_with_body(
format!(
"boards/{}/stacks/{}/cards",
self.ctx.cfg.nextcloud.board_id, create.stack_id
),
body.to_string(),
|net, url| net.post(url),
)
.await
}
pub(crate) async fn get_card(
&self,
board_id: NextcloudBoardId,
stack_id: NextcloudStackId,
card_id: NextcloudCardId,
) -> APIResult<Card> {
self.request(
f!("boards/{board_id}/stacks/{stack_id}/cards/{card_id}"),
|net, url| net.get(url),
)
.await
}
}

View file

@ -1,153 +1,46 @@
//
use bytes::Bytes;
use kxio::net::{Net, ReqBuilder};
use crate::{api_result::APIResult, f, FullCtx};
use card::Create;
use model::{
Board, Card, NextcloudBoardId, NextcloudHostname, NextcloudPassword, NextcloudStackId,
NextcloudUsername, Stack,
use crate::execute::Execute;
use crate::nextcloud::board::NextcloudBoardCommand;
use crate::nextcloud::card::NextcloudCardCommand;
use crate::nextcloud::model::{
NextcloudBoardId, NextcloudHostname, NextcloudPassword, NextcloudUsername,
};
use crate::nextcloud::stack::NextcloudStackCommand;
use crate::FullCtx;
use clap::Parser;
pub mod board;
pub mod card;
pub mod model;
pub mod stack;
//
mod board;
mod card;
pub(crate) mod client;
pub(crate) mod model;
mod stack;
#[cfg(test)]
mod tests;
pub struct DeckClient<'ctx> {
ctx: &'ctx FullCtx,
hostname: &'ctx NextcloudHostname,
username: &'ctx NextcloudUsername,
password: &'ctx NextcloudPassword,
#[derive(Parser, Debug)]
pub(crate) enum NextcloudCommand {
#[clap(subcommand)]
Board(NextcloudBoardCommand),
#[clap(subcommand)]
Stack(NextcloudStackCommand),
#[clap(subcommand)]
Card(NextcloudCardCommand),
}
impl Execute for NextcloudCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
NextcloudCommand::Board(cmd) => cmd.execute(ctx).await,
NextcloudCommand::Stack(cmd) => cmd.execute(ctx).await,
NextcloudCommand::Card(cmd) => cmd.execute(ctx).await,
}
}
}
// Uses the API described here: https://deck.readthedocs.io/en/stable/API/#cards
impl<'ctx> DeckClient<'ctx> {
pub fn new(ctx: &'ctx FullCtx) -> Self {
Self {
ctx,
hostname: &ctx.cfg.nextcloud.hostname,
username: &ctx.cfg.nextcloud.username,
password: &ctx.cfg.nextcloud.password,
}
}
fn url(&self, path: impl Into<String>) -> String {
f!(
"https://{}/index.php/apps/deck/api/v1.0/{}",
self.hostname,
path.into()
)
}
async fn request<T: for<'a> serde::Deserialize<'a>>(
&self,
url: impl Into<String>,
custom: fn(&Net, String) -> ReqBuilder,
) -> APIResult<T> {
APIResult::new(
custom(&self.ctx.net, self.url(url))
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json")
.header("content-type", "application/json")
.send()
.await,
&self.ctx.prt,
)
.await
}
async fn request_with_body<T: for<'a> serde::Deserialize<'a>>(
&self,
url: impl Into<String>,
body: impl Into<Bytes>,
custom: fn(&Net, String) -> ReqBuilder,
) -> APIResult<T> {
APIResult::new(
custom(&self.ctx.net, self.url(url))
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json")
.header("content-type", "application/json")
.body(body)
.send()
.await,
&self.ctx.prt,
)
.await
}
pub async fn get_boards(&self) -> APIResult<Vec<Board>> {
self.request("boards", |net, url| net.get(url)).await
}
pub async fn get_board(&self, board_id: NextcloudBoardId) -> APIResult<Board> {
self.request(f!("boards/{board_id}"), |net, url| net.get(url))
.await
}
pub async fn create_board(&self, title: &str, color: &str) -> APIResult<Board> {
self.request_with_body(
"boards",
serde_json::json!({
"title": title,
"color": color
})
.to_string(),
|net, url| net.post(url),
)
.await
}
pub async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult<Vec<Stack>> {
self.request(f!("boards/{board_id}/stacks"), |net, url| net.get(url))
.await
}
pub async fn get_stack(
&self,
board_id: NextcloudBoardId,
stack_id: NextcloudStackId,
) -> APIResult<Stack> {
self.request(f!("boards/{board_id}/stacks/{stack_id}"), |net, url| {
net.get(url)
})
.await
}
pub(crate) async fn create_card(&self, create: &Create) -> APIResult<Card> {
let mut body = serde_json::json!({
"title": create.title,
});
if let Some(desc) = &create.description {
body["description"] = serde_json::Value::String(desc.to_string());
}
self.request_with_body(
format!(
"boards/{}/stacks/{}/cards",
self.ctx.cfg.nextcloud.board_id, create.stack_id
),
body.to_string(),
|net, url| net.post(url),
)
.await
}
async fn get_card(
&self,
board_id: NextcloudBoardId,
stack_id: NextcloudStackId,
card_id: model::NextcloudCardId,
) -> APIResult<Card> {
self.request(
f!("boards/{board_id}/stacks/{stack_id}/cards/{card_id}"),
|net, url| net.get(url),
)
.await
}
#[derive(Clone, Debug, derive_more::From, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)]
pub struct NextcloudConfig {
pub(crate) hostname: NextcloudHostname,
pub(crate) username: NextcloudUsername,
pub(crate) password: NextcloudPassword,
pub(crate) board_id: NextcloudBoardId,
}

View file

@ -113,64 +113,64 @@ newtype!(
);
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct NextcloudBoardOwner {
pub(crate) struct NextcloudBoardOwner {
#[serde(rename = "primaryKey")]
pub primary_key: String,
pub uid: String,
pub(crate) primary_key: String,
pub(crate) uid: String,
#[serde(rename = "displayname")]
pub display_name: String,
pub(crate) display_name: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Board {
pub id: NextcloudBoardId,
pub title: NextcloudBoardTitle,
pub owner: NextcloudBoardOwner,
pub color: NextcloudBoardColour,
pub archived: bool,
pub labels: Vec<Label>,
pub acl: Vec<Acl>,
pub(crate) struct Board {
pub(crate) id: NextcloudBoardId,
pub(crate) title: NextcloudBoardTitle,
pub(crate) owner: NextcloudBoardOwner,
pub(crate) color: NextcloudBoardColour,
pub(crate) archived: bool,
pub(crate) labels: Vec<Label>,
pub(crate) acl: Vec<Acl>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Stack {
pub id: NextcloudStackId,
pub title: NextcloudStackTitle,
pub order: NextcloudOrder,
pub(crate) struct Stack {
pub(crate) id: NextcloudStackId,
pub(crate) title: NextcloudStackTitle,
pub(crate) order: NextcloudOrder,
#[serde(rename = "boardId")]
pub board_id: NextcloudBoardId,
pub(crate) board_id: NextcloudBoardId,
#[serde(rename = "ETag")]
pub etag: NextcloudETag,
pub(crate) etag: NextcloudETag,
#[serde(default)]
pub cards: Vec<Card>,
pub(crate) cards: Vec<Card>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Card {
pub id: NextcloudCardId,
pub title: NextcloudCardTitle,
pub description: Option<String>,
pub(crate) struct Card {
pub(crate) id: NextcloudCardId,
pub(crate) title: NextcloudCardTitle,
pub(crate) description: Option<String>,
#[serde(rename = "stackId")]
pub stack_id: NextcloudStackId,
pub order: NextcloudOrder,
pub archived: bool,
pub(crate) stack_id: NextcloudStackId,
pub(crate) order: NextcloudOrder,
pub(crate) archived: bool,
#[serde(default)]
pub due_date: Option<String>,
pub(crate) due_date: Option<String>,
#[serde(default)]
pub labels: Option<Vec<Label>>,
pub(crate) labels: Option<Vec<Label>>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Label {
pub id: NextcloudLabelId,
pub title: String,
pub color: String,
pub(crate) struct Label {
pub(crate) id: NextcloudLabelId,
pub(crate) title: String,
pub(crate) color: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Acl {
pub participant: String,
pub permission_edit: bool,
pub permission_share: bool,
pub permission_manage: bool,
pub(crate) struct Acl {
pub(crate) participant: String,
pub(crate) permission_edit: bool,
pub(crate) permission_share: bool,
pub(crate) permission_manage: bool,
}

View file

@ -1,7 +1,26 @@
//
use clap::Parser;
use crate::execute::Execute;
use crate::{p, FullCtx};
pub async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
#[derive(Parser, Debug)]
pub(crate) enum NextcloudStackCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
impl Execute for NextcloudStackCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::List { dump } => list(ctx, dump).await,
}
}
}
pub(crate) async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
let api_result = ctx
.deck_client()
.get_stacks(ctx.cfg.nextcloud.board_id)

View file

@ -1,26 +1,19 @@
//
use kxio::{
fs::TempFileSystem,
net::{MockNet, StatusCode},
print::Printer,
};
use kxio::net::StatusCode;
use crate::{
config::{NextcloudConfig, TrelloConfig},
nextcloud::{
model::{
nextcloud::model::{
Board, Card, NextcloudBoardColour, NextcloudBoardId, NextcloudBoardOwner,
NextcloudBoardTitle, NextcloudCardId, NextcloudCardTitle, NextcloudETag,
NextcloudHostname, NextcloudOrder, NextcloudPassword, NextcloudStackId,
NextcloudStackTitle, NextcloudUsername, Stack,
NextcloudBoardTitle, NextcloudCardId, NextcloudCardTitle, NextcloudETag, NextcloudHostname,
NextcloudOrder, NextcloudPassword, NextcloudStackId, NextcloudStackTitle,
NextcloudUsername, Stack,
},
DeckClient,
},
s, AppConfig, FullCtx,
s,
};
mod config {
use super::*;
use crate::nextcloud::NextcloudConfig;
#[test]
fn config_hostname_returns_hostname() {
@ -126,12 +119,16 @@ mod commands {
.body(include_str!("../tests/responses/nextcloud-board-list.json"))
.expect("mock request");
let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net, fs);
let deck_client = DeckClient::new(&ctx);
// let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net);
//when
let result = deck_client.get_boards().await.result.expect("get boards");
let result = ctx
.deck_client()
.get_boards()
.await
.result
.expect("get boards");
assert_eq!(
result.first(),
@ -170,12 +167,12 @@ mod commands {
))
.expect("mock request");
let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net, fs);
let deck_client = DeckClient::new(&ctx);
// let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net);
//when
let result = deck_client
let result = ctx
.deck_client()
.get_stacks(ctx.cfg.nextcloud.board_id)
.await
.result;
@ -195,12 +192,12 @@ mod commands {
.body(include_str!("../tests/responses/nextcloud-stack-list.json"))
.expect("mock request");
let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net, fs);
let deck_client = DeckClient::new(&ctx);
// let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net);
//when
let result = deck_client
let result = ctx
.deck_client()
.get_stacks(ctx.cfg.nextcloud.board_id)
.await
.result
@ -313,12 +310,12 @@ mod commands {
.body(include_str!("../tests/responses/nextcloud-card-list.json"))
.expect("mock request");
let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net, fs);
let deck_client = DeckClient::new(&ctx);
// let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net);
//when
let result = deck_client
let result = ctx
.deck_client()
.get_stack(ctx.cfg.nextcloud.board_id, 1.into())
.await
.result
@ -359,12 +356,12 @@ mod commands {
.body(include_str!("../tests/responses/nextcloud-card-get.json"))
.expect("mock request");
let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net, fs);
let deck_client = DeckClient::new(&ctx);
// let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net);
//when
let result = deck_client
let result = ctx
.deck_client()
.get_card(ctx.cfg.nextcloud.board_id, 1.into(), 321.into())
.await
.result
@ -413,12 +410,12 @@ mod commands {
))
.expect("mock request");
let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net, fs);
let deck_client = DeckClient::new(&ctx);
// let fs = given::a_filesystem();
let ctx = given::a_full_context(mock_net);
//when
let result = deck_client
let result = ctx
.deck_client()
.create_card(&Create {
dump: false,
stack_id: 1,
@ -447,10 +444,16 @@ mod commands {
}
mod given {
use kxio::{net::MockNet, print::Printer};
use super::*;
use crate::nextcloud::model::{
NextcloudBoardId, NextcloudHostname, NextcloudPassword, NextcloudUsername,
};
use crate::nextcloud::NextcloudConfig;
use crate::trello::TrelloConfig;
use crate::{s, AppConfig, FullCtx};
pub fn a_nextcloud_config() -> NextcloudConfig {
pub(crate) fn a_nextcloud_config() -> NextcloudConfig {
let hostname = NextcloudHostname::new("host-name");
let username = NextcloudUsername::new("username");
let password = NextcloudPassword::new("password");
@ -463,13 +466,13 @@ mod given {
}
}
pub fn a_printer() -> Printer {
pub(crate) fn a_printer() -> Printer {
kxio::print::test()
}
pub(crate) fn a_filesystem() -> TempFileSystem {
kxio::fs::temp().expect("temp fs")
}
// pub(crate) fn a_filesystem() -> TempFileSystem {
// kxio::fs::temp().expect("temp fs")
// }
pub(crate) fn a_trello_config() -> TrelloConfig {
TrelloConfig {
@ -479,14 +482,17 @@ mod given {
}
}
pub(crate) fn a_full_context(mock_net: MockNet, fs: TempFileSystem) -> FullCtx {
pub(crate) fn a_full_context(
mock_net: MockNet,
// fs: TempFileSystem
) -> FullCtx {
FullCtx {
fs: fs.as_real(),
// fs: fs.as_real(),
net: mock_net.into(),
prt: given::a_printer(),
prt: a_printer(),
cfg: AppConfig {
trello: given::a_trello_config(),
nextcloud: given::a_nextcloud_config(),
trello: a_trello_config(),
nextcloud: a_nextcloud_config(),
},
}
}

View file

@ -50,8 +50,10 @@ mod config {
use super::*;
mod load {
use super::*;
use crate::config::{NextcloudConfig, TrelloConfig};
use crate::nextcloud::NextcloudConfig;
use crate::trello::TrelloConfig;
use crate::{s, AppConfig};
#[test]
fn test_load() {
//given
@ -114,18 +116,18 @@ mod given {
print::Printer,
};
pub fn a_context(fs: FileSystem, net: Net, prt: Printer) -> Ctx {
pub(crate) fn a_context(fs: FileSystem, net: Net, prt: Printer) -> Ctx {
Ctx { fs, net, prt }
}
pub fn a_filesystem() -> TempFileSystem {
pub(crate) fn a_filesystem() -> TempFileSystem {
kxio::fs::temp().expect("temp fs")
}
pub fn a_network() -> MockNet {
pub(crate) fn a_network() -> MockNet {
kxio::net::mock()
}
pub fn a_printer() -> Printer {
pub(crate) fn a_printer() -> Printer {
kxio::print::test()
}
}

View file

@ -1,11 +1,7 @@
//
// use color_eyre::Result;
// use kxio::net::Net;
use crate::trello::types::{board::TrelloBoard, TrelloBoardName};
// pub async fn get_board(
// pub(crate) async fn get_board(
// auth: &TrelloAuth,
// board_id: &TrelloBoardId,
// net: &Net,
@ -23,7 +19,7 @@ use crate::trello::types::{board::TrelloBoard, TrelloBoardName};
// Ok(board)
// }
pub trait TrelloBoards {
pub(crate) trait TrelloBoards {
fn find_by_name(&self, board_name: &TrelloBoardName) -> Option<&TrelloBoard>;
}
impl TrelloBoards for Vec<TrelloBoard> {

View file

@ -2,8 +2,3 @@ mod create;
mod delete;
mod get;
mod update;
pub use create::create_card;
pub use delete::delete_card;
pub use get::get_card;
pub use update::{update_card, TrelloCardUpdate};

View file

@ -30,7 +30,7 @@ use crate::{
/// 200 OK Success
///
/// application/json
pub async fn get_lists_cards(
pub(crate) async fn get_lists_cards(
auth: &TrelloAuth,
list: &TrelloList,
net: &Net,

View file

@ -1,11 +1,13 @@
//
use kxio::{net::Net, print::Printer};
use crate::api_result::APIResult;
use crate::config::TrelloConfig;
use crate::trello::{
use crate::trello::TrelloConfig;
use crate::{
api_result::APIResult,
trello::{
types::{auth::TrelloAuth, board::TrelloBoard},
url,
},
};
/// Get lists from named board that Member belongs to
@ -37,12 +39,15 @@ use crate::trello::{
/// curl --request GET \
/// --url "https://api.trello.com/1/members/$TRELLO_USERNAME/boards?key=$TRELLO_KEY&token=$TRELLO_SECRET&lists=open" \
/// --header 'Accept: application/json'
pub async fn get_boards_that_member_belongs_to(
pub(crate) async fn get_boards_that_member_belongs_to(
cfg: &TrelloConfig,
net: &Net,
prt: &Printer,
) -> APIResult<Vec<TrelloBoard>> {
let auth = TrelloAuth::new(&cfg.api_key, &cfg.api_secret);
let auth = TrelloAuth {
api_key: &cfg.api_key,
api_secret: &cfg.api_secret,
};
APIResult::new(
net.get(url("/members/me/boards?lists=open"))
.headers(auth.into())

View file

@ -1,9 +1,9 @@
//
pub mod boards;
// pub mod cards;
// pub mod lists;
pub mod members;
pub(crate) mod boards;
// pub(crate) mod cards;
// pub(crate) mod lists;
pub(crate) mod members;
#[cfg(test)]
mod tests;

View file

@ -5,7 +5,6 @@ use kxio::net::StatusCode;
use serde_json::json;
use crate::{
config::TrelloConfig,
s,
trello::{api::members::get_boards_that_member_belongs_to, types::board::TrelloBoard},
};
@ -15,8 +14,8 @@ mod given;
type TestResult = color_eyre::Result<()>;
mod members {
use super::*;
use crate::trello::TrelloConfig;
#[tokio::test]
async fn get_member_boards() -> TestResult {

View file

@ -1,18 +1,35 @@
//
// use crate::{p, FullCtx};
//
// pub(crate) async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
// let api_result =
// super::api::members::get_boards_that_member_belongs_to(&ctx.cfg.trello, &ctx.net, &ctx.prt)
// .await;
// if dump {
// p!(ctx.prt, "{}", api_result.text);
// } else {
// let mut boards = api_result.result?;
// boards.sort_by(|a, b| a.name.cmp(&b.name));
// boards.into_iter().for_each(|board| {
// p!(ctx.prt, "{}:{}", board.id, board.name);
// });
// }
// Ok(())
// }
use clap::Parser;
use crate::execute::Execute;
use crate::{p, FullCtx};
#[derive(Parser, Debug)]
pub(crate) enum TrelloBoardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
impl Execute for TrelloBoardCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::List { dump } => list(ctx, dump).await,
}
}
}
pub(crate) async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
let api_result = ctx.trello_client().boards(&ctx.cfg.trello).await;
if dump {
p!(ctx.prt, "{}", api_result.text);
} else {
let mut boards = api_result.result?;
boards.sort_by(|a, b| a.name.cmp(&b.name));
boards.into_iter().for_each(|board| {
p!(ctx.prt, "{}:{}", board.id, board.name);
});
}
Ok(())
}

22
src/trello/client.rs Normal file
View file

@ -0,0 +1,22 @@
//
use crate::api_result::APIResult;
use crate::trello::types::board::TrelloBoard;
use crate::trello::TrelloConfig;
use crate::FullCtx;
pub(crate) struct TrelloClient<'ctx> {
ctx: &'ctx FullCtx,
}
impl<'ctx> TrelloClient<'ctx> {
pub(crate) async fn boards(&self, cfg: &TrelloConfig) -> APIResult<Vec<TrelloBoard>> {
super::api::members::get_boards_that_member_belongs_to(cfg, &self.ctx.net, &self.ctx.prt)
.await
}
}
impl TrelloClient<'_> {
pub(crate) fn new(ctx: &FullCtx) -> TrelloClient {
TrelloClient { ctx }
}
}

View file

@ -1,16 +1,42 @@
//
// pub mod api;
pub mod api;
pub mod boards;
pub mod types;
pub(crate) mod api;
pub(crate) mod boards;
pub(crate) mod client;
pub(crate) mod types;
// #[cfg(test)]
// mod tests;
use crate::f;
use crate::execute::Execute;
use crate::trello::boards::TrelloBoardCommand;
use crate::trello::types::auth::{TrelloApiKey, TrelloApiSecret};
use crate::trello::types::TrelloBoardName;
use crate::{f, FullCtx};
use clap::Parser;
pub fn url(path: impl Into<String>) -> String {
pub(crate) fn url(path: impl Into<String>) -> String {
let path = path.into();
assert!(path.starts_with("/"));
f!("https://api.trello.com/1{path}")
}
#[derive(Parser, Debug)]
pub(crate) enum TrelloCommand {
#[clap(subcommand)]
Board(TrelloBoardCommand),
}
impl Execute for TrelloCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::Board(cmd) => cmd.execute(ctx).await,
}
}
}
#[derive(Clone, Debug, derive_more::From, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)]
pub struct TrelloConfig {
pub(crate) api_key: TrelloApiKey,
pub(crate) api_secret: TrelloApiSecret,
pub(crate) board_name: TrelloBoardName,
}

View file

@ -1,4 +1,3 @@
mod board {
// use crate::trello::{
// // api::boards::TrelloBoards as _,
@ -37,14 +36,6 @@ mod board {
// }
mod commands {
mod board {
use crate::trello::{
api::boards::TrelloBoards as _,
types::{
TrelloBoard, TrelloBoardId, TrelloBoardName, TrelloList, TrelloListId,
TrelloListName,
},
};
#[test]
fn name_returns_name() {
//given
@ -81,6 +72,47 @@ mod board {
assert_eq!(result, lists);
}
#[test]
fn list_of_boards_find_by_name_returns_board() {
//given
let board = TrelloBoard::new(
TrelloBoardId::new("2"),
TrelloBoardName::new("beta"),
vec![],
);
let boards = vec![TrelloBoard::new(
TrelloBoardId::new("1"),
TrelloBoardName::new("alpha"),
vec![],
)];
//when
let result = board.name();
//then
assert_eq!(result, &TrelloBoardName::new("board-name"));
}
#[test]
fn lists_should_return_lists() {
//given
let lists = vec![TrelloList::new(
TrelloListId::new("list-id"),
TrelloListName::new("list-name"),
)];
let board = TrelloBoard::new(
TrelloBoardId::new("board-id"),
TrelloBoardName::new("board-name"),
lists.clone(),
);
//when
let result = board.lists();
//then
assert_eq!(result, lists);
}
#[test]
fn list_of_boards_find_by_name_returns_board() {
//given

View file

@ -20,29 +20,13 @@ pub struct TrelloAuth<'cfg> {
pub(crate) api_key: &'cfg TrelloApiKey,
pub(crate) api_secret: &'cfg TrelloApiSecret,
}
impl<'cfg> TrelloAuth<'cfg> {
pub const fn new(api_key: &'cfg TrelloApiKey, api_secret: &'cfg TrelloApiSecret) -> Self {
Self {
api_key,
api_secret,
}
}
pub const fn api_key(&self) -> &TrelloApiKey {
self.api_key
}
pub const fn api_token(&self) -> &TrelloApiSecret {
self.api_secret
}
}
impl From<TrelloAuth<'_>> for HashMap<String, String> {
fn from(value: TrelloAuth) -> Self {
HashMap::from([(
"Authorization".into(),
format!(
r#"OAuth oauth_consumer_key="{}", oauth_token="{}""#,
value.api_key(),
value.api_token()
value.api_key, value.api_secret
),
)])
}

View file

@ -2,7 +2,7 @@
use crate::trello::{api::cards::TrelloCardUpdate, TrelloCardId, TrelloCardName, TrelloListId};
#[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct TrelloCard {
pub(crate) struct TrelloCard {
id: TrelloCardId,
name: TrelloCardName,
#[serde(rename = "idList")]
@ -10,11 +10,11 @@ pub struct TrelloCard {
}
impl TrelloCard {
#[cfg(test)]
pub const fn new(id: TrelloCardId, name: TrelloCardName, id_list: TrelloListId) -> Self {
pub(crate) const fn new(id: TrelloCardId, name: TrelloCardName, id_list: TrelloListId) -> Self {
Self { id, name, id_list }
}
pub const fn list_id(&self) -> &TrelloListId {
pub(crate) const fn list_id(&self) -> &TrelloListId {
&self.id_list
}
}

View file

@ -1,7 +1,7 @@
pub(crate) mod auth;
pub(crate) mod board;
// mod card;
mod list;
pub(crate) mod list;
// mod new_card;
use derive_more::derive::Display;

View file

@ -1,20 +1,20 @@
use super::{TrelloCardName, TrelloListId};
pub struct NewTrelloCard {
pub(crate) struct NewTrelloCard {
name: TrelloCardName,
list_id: TrelloListId,
}
impl NewTrelloCard {
pub fn new(name: impl Into<TrelloCardName>, list_id: impl Into<TrelloListId>) -> Self {
pub(crate) fn new(name: impl Into<TrelloCardName>, list_id: impl Into<TrelloListId>) -> Self {
Self {
name: name.into(),
list_id: list_id.into(),
}
}
pub const fn name(&self) -> &TrelloCardName {
pub(crate) const fn name(&self) -> &TrelloCardName {
&self.name
}
pub const fn list_id(&self) -> &TrelloListId {
pub(crate) const fn list_id(&self) -> &TrelloListId {
&self.list_id
}
}