From 0f32f1d37c95818bc4ced69f09befe9320026611 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 30 Oct 2024 20:23:56 +0100 Subject: Add command module and move implementations to it. --- src/bin/zot/command.rs | 11 +++++++ src/bin/zot/command/channel.rs | 10 +++++++ src/bin/zot/command/channel/export.rs | 19 ++++++++++++ src/bin/zot/command/channel/list.rs | 21 +++++++++++++ src/bin/zot/command/channel/stream.rs | 47 +++++++++++++++++++++++++++++ src/bin/zot/command/contact.rs | 8 +++++ src/bin/zot/command/contact/list.rs | 21 +++++++++++++ src/bin/zot/command/verify.rs | 21 +++++++++++++ src/bin/zot/command/version.rs | 19 ++++++++++++ src/bin/zot/main.rs | 56 ++++++++++------------------------- 10 files changed, 193 insertions(+), 40 deletions(-) create mode 100644 src/bin/zot/command.rs create mode 100644 src/bin/zot/command/channel.rs create mode 100644 src/bin/zot/command/channel/export.rs create mode 100644 src/bin/zot/command/channel/list.rs create mode 100644 src/bin/zot/command/channel/stream.rs create mode 100644 src/bin/zot/command/contact.rs create mode 100644 src/bin/zot/command/contact/list.rs create mode 100644 src/bin/zot/command/verify.rs create mode 100644 src/bin/zot/command/version.rs diff --git a/src/bin/zot/command.rs b/src/bin/zot/command.rs new file mode 100644 index 0000000..6d85ed2 --- /dev/null +++ b/src/bin/zot/command.rs @@ -0,0 +1,11 @@ +/* + * SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri + * SPDX-FileCopyrightText: 2024 Harald Eilertsen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +pub mod channel; +pub mod contact; +pub mod verify; +pub mod version; diff --git a/src/bin/zot/command/channel.rs b/src/bin/zot/command/channel.rs new file mode 100644 index 0000000..883922f --- /dev/null +++ b/src/bin/zot/command/channel.rs @@ -0,0 +1,10 @@ +/* + * SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri + * SPDX-FileCopyrightText: 2024 Harald Eilertsen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +pub mod export; +pub mod list; +pub mod stream; diff --git a/src/bin/zot/command/channel/export.rs b/src/bin/zot/command/channel/export.rs new file mode 100644 index 0000000..ad006de --- /dev/null +++ b/src/bin/zot/command/channel/export.rs @@ -0,0 +1,19 @@ +/* + * SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri + * SPDX-FileCopyrightText: 2024 Harald Eilertsen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +use zotapi::ZotApi; +use std::error::Error; + +pub struct ChannelExportCmd { +} + +impl ChannelExportCmd { + pub async fn run(&self, client: ZotApi) -> Result<(), Box> { + println!("{}", client.channel_export().await?); + Ok(()) + } +} diff --git a/src/bin/zot/command/channel/list.rs b/src/bin/zot/command/channel/list.rs new file mode 100644 index 0000000..a308517 --- /dev/null +++ b/src/bin/zot/command/channel/list.rs @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri + * SPDX-FileCopyrightText: 2024 Harald Eilertsen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +use zotapi::ZotApi; +use std::error::Error; + +pub struct ChannelListCmd { +} + +impl ChannelListCmd { + pub async fn run(&self, client: ZotApi) -> Result<(), Box<(dyn Error + 'static)>> { + for ch in client.channel_list().await? { + println!("{}", ch); + } + Ok(()) + } +} diff --git a/src/bin/zot/command/channel/stream.rs b/src/bin/zot/command/channel/stream.rs new file mode 100644 index 0000000..365158b --- /dev/null +++ b/src/bin/zot/command/channel/stream.rs @@ -0,0 +1,47 @@ +/* + * SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri + * SPDX-FileCopyrightText: 2024 Harald Eilertsen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +use zotapi::{ + Stream, + ZotApi, +}; + +pub struct ChannelStreamCmd { +} + +impl ChannelStreamCmd { + pub async fn run(&self, client: ZotApi) -> Result<(), Box<(dyn std::error::Error + 'static)>> { + let json = client.channel_stream().await?; + + std::fs::write("channel_stream.json", &json) + .expect("Unable to write channel_stream.json file"); + + let s = Stream::from_json(&json)?; + for item in s.items { + if item.is_post() { + let mut summary = item.title; + + if summary.len() == 0 { + if item.summary.len() > 0 { + summary = item.summary; + } else { + summary = item.body; + } + } + + summary.truncate(64); + + println!("{} | {:24} | {}", + item.created.to_string(), + item.author.name, + summary); + } + } + //println!("{}", z.channel_stream().await?); + Ok(()) + } +} diff --git a/src/bin/zot/command/contact.rs b/src/bin/zot/command/contact.rs new file mode 100644 index 0000000..5db992d --- /dev/null +++ b/src/bin/zot/command/contact.rs @@ -0,0 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri + * SPDX-FileCopyrightText: 2024 Harald Eilertsen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +pub mod list; diff --git a/src/bin/zot/command/contact/list.rs b/src/bin/zot/command/contact/list.rs new file mode 100644 index 0000000..ee5f167 --- /dev/null +++ b/src/bin/zot/command/contact/list.rs @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri + * SPDX-FileCopyrightText: 2024 Harald Eilertsen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +use std::error::Error; +use zotapi::ZotApi; + +pub struct ContactListCmd { +} + +impl ContactListCmd { + pub async fn run(&self, client: ZotApi) -> Result<(), Box> { + let r = client.abook_list().await?; + println!("{}", r); + + Ok(()) + } +} diff --git a/src/bin/zot/command/verify.rs b/src/bin/zot/command/verify.rs new file mode 100644 index 0000000..5c6432f --- /dev/null +++ b/src/bin/zot/command/verify.rs @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri + * SPDX-FileCopyrightText: 2024 Harald Eilertsen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +use std::error::Error; +use zotapi::ZotApi; + +pub struct VerifyCmd { +} + +impl VerifyCmd { + pub async fn run(&self, client: ZotApi) -> Result<(), Box> { + let channel = client.verify().await; + println!("{:?}", channel); + + Ok(()) + } +} diff --git a/src/bin/zot/command/version.rs b/src/bin/zot/command/version.rs new file mode 100644 index 0000000..a184247 --- /dev/null +++ b/src/bin/zot/command/version.rs @@ -0,0 +1,19 @@ +/* + * SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri + * SPDX-FileCopyrightText: 2024 Harald Eilertsen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +use std::error::Error; +use zotapi::ZotApi; + +pub struct VersionCmd { +} + +impl VersionCmd { + pub async fn run(&self, client: ZotApi) -> Result<(), Box> { + println!("{}", client.version().await?); + Ok(()) + } +} diff --git a/src/bin/zot/main.rs b/src/bin/zot/main.rs index a919e35..8508c13 100644 --- a/src/bin/zot/main.rs +++ b/src/bin/zot/main.rs @@ -15,12 +15,12 @@ * along with this program. If not, see . */ -use zotapi::Stream; - use clap::{clap_app, crate_authors, crate_version}; use dotenv::dotenv; use std::env; +mod command; + #[tokio::main] async fn main() -> Result<(), Box<(dyn std::error::Error + 'static)>> { dotenv().ok(); @@ -104,52 +104,28 @@ async fn main() -> Result<(), Box<(dyn std::error::Error + 'static)>> { Ok(match matches.subcommand() { ("verify", Some(_)) => { - let channel = z.verify().await; - println!("{:?}", channel); + let cmd = command::verify::VerifyCmd{}; + cmd.run(z).await?; } ("version", Some(_)) => { - println!("{}", z.version().await?); + let cmd = command::version::VersionCmd{}; + cmd.run(z).await?; } ("channel", Some(m)) => { match m.subcommand() { ("list", Some(_)) => { - for ch in z.channel_list().await? { - println!("{}", ch); - } + let cmd = command::channel::list::ChannelListCmd{}; + cmd.run(z).await?; } ("stream", Some(_)) => { - let json = z.channel_stream().await?; - - std::fs::write("channel_stream.json", &json) - .expect("Unable to write channel_stream.json file"); - - let s = Stream::from_json(&json)?; - for item in s.items { - if item.is_post() { - let mut summary = item.title; - - if summary.len() == 0 { - if item.summary.len() > 0 { - summary = item.summary; - } else { - summary = item.body; - } - } - - summary.truncate(64); - - println!("{} | {:24} | {}", - item.created.to_string(), - item.author.name, - summary); - } - } - //println!("{}", z.channel_stream().await?); + let cmd = command::channel::stream::ChannelStreamCmd{}; + cmd.run(z).await?; } ("export", Some(_)) => { - println!("{}", z.channel_export().await?); + let cmd = command::channel::export::ChannelExportCmd{}; + cmd.run(z).await?; } _ => { println!("Not a known subcommand for `channel`, or it's not implemented yet."); @@ -157,14 +133,14 @@ async fn main() -> Result<(), Box<(dyn std::error::Error + 'static)>> { } } } - ("abook", Some(m)) => { + ("contact", Some(m)) => { match m.subcommand() { ("list", Some(_)) => { - let r = z.abook_list().await?; - println!("{}", r); + let cmd = command::contact::list::ContactListCmd{}; + cmd.run(z).await?; } _ => { - println!("Not a known subcommand for `abook`, or it's not implemented yet."); + println!("Not a known subcommand for `contact`, or it's not implemented yet."); println!("{}", m.usage()); } } -- cgit v1.2.3