From 15c0eaa5dc655e4056bb037a1f7eb1181e32a3b6 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Fri, 23 May 2025 13:15:37 +0000 Subject: [PATCH] chore: Make `commands` module Signed-off-by: Awiteb --- src/cli/{ => commands}/issue/mod.rs | 0 src/cli/{ => commands}/issue/new.rs | 0 src/cli/commands/mod.rs | 111 ++++++++++++++++++++++++ src/cli/{ => commands}/reply.rs | 3 +- src/cli/{ => commands}/repo/announce.rs | 0 src/cli/{ => commands}/repo/mod.rs | 0 src/cli/{ => commands}/repo/view.rs | 0 src/cli/mod.rs | 95 ++------------------ 8 files changed, 119 insertions(+), 90 deletions(-) rename src/cli/{ => commands}/issue/mod.rs (100%) rename src/cli/{ => commands}/issue/new.rs (100%) create mode 100644 src/cli/commands/mod.rs rename src/cli/{ => commands}/reply.rs (99%) rename src/cli/{ => commands}/repo/announce.rs (100%) rename src/cli/{ => commands}/repo/mod.rs (100%) rename src/cli/{ => commands}/repo/view.rs (100%) diff --git a/src/cli/issue/mod.rs b/src/cli/commands/issue/mod.rs similarity index 100% rename from src/cli/issue/mod.rs rename to src/cli/commands/issue/mod.rs diff --git a/src/cli/issue/new.rs b/src/cli/commands/issue/new.rs similarity index 100% rename from src/cli/issue/new.rs rename to src/cli/commands/issue/new.rs diff --git a/src/cli/commands/mod.rs b/src/cli/commands/mod.rs new file mode 100644 index 0000000..0773551 --- /dev/null +++ b/src/cli/commands/mod.rs @@ -0,0 +1,111 @@ +// n34 - A CLI to interact with NIP-34 and other stuff related to codes in nostr +// Copyright (C) 2025 Awiteb +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + + +/// `issue` subcommands +mod issue; +/// 'reply` command +mod reply; +/// `repo` subcommands +mod repo; + + +use std::fmt; + +use clap::{ArgGroup, Args, Parser}; +use nostr::key::{Keys, PublicKey, SecretKey}; +use nostr::types::RelayUrl; + +use self::issue::IssueSubcommands; +use self::reply::ReplyArgs; +use self::repo::RepoSubcommands; +use super::traits::CommandRunner; +use crate::error::N34Result; + +/// The command-line interface options +#[derive(Args, Clone)] +#[clap( + group( + ArgGroup::new("auth") + .args(&["secret_key"]) + .required(true) + ) +)] +pub struct CliOptions { + /// Your Nostr secret key + #[arg(short, long)] + pub secret_key: Option, + /// Where your relays list. And repository relays if not included in naddr + #[arg(short, long, required = true)] + pub relays: Vec, + /// Proof of Work difficulty when creatring events + #[arg(long, default_value_t = 0)] + pub pow: u8, +} + +/// N34 commands +#[derive(Parser, Debug)] +pub enum Commands { + /// Manage repositories + Repo { + #[command(subcommand)] + subcommands: RepoSubcommands, + }, + /// Manage issues + Issue { + #[command(subcommand)] + subcommands: IssueSubcommands, + }, + // /// Manage patches + // Patch { + // #[command(subcommand)] + // subcommands: PatchSubcommands, + // }, + /// Reply to issues and patches. + Reply(ReplyArgs), +} + + +impl CommandRunner for Commands { + async fn run(self, options: CliOptions) -> N34Result<()> { + tracing::trace!("Options: {options:#?}"); + tracing::trace!("Handling: {self:#?}"); + match self { + Self::Repo { subcommands } => subcommands.run(options).await, + Self::Issue { subcommands } => subcommands.run(options).await, + Commands::Reply(args) => args.run(options).await, + } + } +} + +impl CliOptions { + /// Gets the public key of the user. + pub async fn pubkey(&self) -> N34Result { + if let Some(sk) = &self.secret_key { + return Ok(Keys::new(sk.clone()).public_key()); + } + unreachable!("There is no other method until now") + } +} + +impl fmt::Debug for CliOptions { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("CliOptions") + .field("secret_key", &self.secret_key.as_ref().map(|_| "*******")) + .field("relays", &self.relays) + .finish() + } +} diff --git a/src/cli/reply.rs b/src/cli/commands/reply.rs similarity index 99% rename from src/cli/reply.rs rename to src/cli/commands/reply.rs index 150d227..9eaeaaa 100644 --- a/src/cli/reply.rs +++ b/src/cli/commands/reply.rs @@ -24,8 +24,9 @@ use nostr::{ types::RelayUrl, }; -use super::{CliOptions, CommandRunner, parsers}; +use super::{CliOptions, CommandRunner}; use crate::{ + cli::parsers, error::{N34Error, N34Result}, nostr_utils::{NostrClient, utils}, }; diff --git a/src/cli/repo/announce.rs b/src/cli/commands/repo/announce.rs similarity index 100% rename from src/cli/repo/announce.rs rename to src/cli/commands/repo/announce.rs diff --git a/src/cli/repo/mod.rs b/src/cli/commands/repo/mod.rs similarity index 100% rename from src/cli/repo/mod.rs rename to src/cli/commands/repo/mod.rs diff --git a/src/cli/repo/view.rs b/src/cli/commands/repo/view.rs similarity index 100% rename from src/cli/repo/view.rs rename to src/cli/commands/repo/view.rs diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 0628b28..0b4ad35 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -14,27 +14,17 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -/// `issue` subcommands -mod issue; +/// Commands module +mod commands; /// CLI arguments parsers pub mod parsers; -/// 'reply` command -mod reply; -/// `repo` subcommands -mod repo; /// CLI traits mod traits; -use std::fmt; - -use clap::{ArgGroup, Args, Parser}; +use clap::Parser; use clap_verbosity_flag::Verbosity; -use nostr::key::Keys; -use nostr::{PublicKey, RelayUrl, SecretKey}; -use self::issue::IssueSubcommands; -use self::reply::ReplyArgs; -use self::repo::RepoSubcommands; +pub use self::commands::*; use self::traits::CommandRunner; use crate::error::N34Result; @@ -52,63 +42,21 @@ const FOOTER: &str = r#"Please report bugs to , - /// Where your relays list. And repository relays if not included in naddr - #[arg(short, long, required = true)] - pub relays: Vec, - /// Proof of Work difficulty when creatring events - #[arg(long, default_value_t = 0)] - pub pow: u8, -} - #[derive(Parser, Debug)] #[command(about, version, before_long_help = HEADER, after_long_help = FOOTER)] /// A command-line interface for interacting with NIP-34 and other Nostr /// code-related stuff. pub struct Cli { #[command(flatten)] - pub options: CliOptions, + pub options: commands::CliOptions, /// Controls the verbosity level of output #[command(flatten)] pub verbosity: Verbosity, /// The subcommand to execute #[command(subcommand)] - pub command: Commands, + pub command: commands::Commands, } -/// N34 commands -#[derive(Parser, Debug)] -pub enum Commands { - /// Manage repositories - Repo { - #[command(subcommand)] - subcommands: RepoSubcommands, - }, - /// Manage issues - Issue { - #[command(subcommand)] - subcommands: IssueSubcommands, - }, - // /// Manage patches - // Patch { - // #[command(subcommand)] - // subcommands: PatchSubcommands, - // }, - /// Reply to issues and patches. - Reply(ReplyArgs), -} impl Cli { /// Executes the command @@ -116,34 +64,3 @@ impl Cli { self.command.run(self.options).await } } - -impl CommandRunner for Commands { - async fn run(self, options: CliOptions) -> N34Result<()> { - tracing::trace!("Options: {options:#?}"); - tracing::trace!("Handling: {self:#?}"); - match self { - Self::Repo { subcommands } => subcommands.run(options).await, - Self::Issue { subcommands } => subcommands.run(options).await, - Commands::Reply(args) => args.run(options).await, - } - } -} - -impl CliOptions { - /// Gets the public key of the user. - pub async fn pubkey(&self) -> N34Result { - if let Some(sk) = &self.secret_key { - return Ok(Keys::new(sk.clone()).public_key()); - } - unreachable!("There is no other method until now") - } -} - -impl fmt::Debug for CliOptions { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("CliOptions") - .field("secret_key", &self.secret_key.as_ref().map(|_| "*******")) - .field("relays", &self.relays) - .finish() - } -}