feat: New config pow command to set the default PoW difficulty

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb
2025-06-07 19:54:40 +00:00
parent 53a2eb5bda
commit 51bd239024
8 changed files with 100 additions and 7 deletions

View File

@@ -0,0 +1,39 @@
// n34 - A CLI to interact with NIP-34 and other stuff related to codes in nostr
// Copyright (C) 2025 Awiteb <a@4rs.nl>
//
// 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 <https://gnu.org/licenses/gpl-3.0.html>.
/// `config pow` subcommand
mod pow;
use clap::Subcommand;
use self::pow::PowArgs;
use super::CliOptions;
use crate::{cli::traits::CommandRunner, error::N34Result};
#[derive(Subcommand, Debug)]
pub enum ConfigSubcommands {
/// Sets the default PoW difficulty (0 if not specified)
Pow(PowArgs),
}
impl CommandRunner for ConfigSubcommands {
async fn run(self, options: CliOptions) -> N34Result<()> {
match self {
Self::Pow(args) => args.run(options).await,
}
}
}

View File

@@ -0,0 +1,37 @@
// n34 - A CLI to interact with NIP-34 and other stuff related to codes in nostr
// Copyright (C) 2025 Awiteb <a@4rs.nl>
//
// 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 <https://gnu.org/licenses/gpl-3.0.html>.
use clap::Args;
use crate::{
cli::{CliOptions, traits::CommandRunner},
error::N34Result,
};
#[derive(Args, Debug)]
pub struct PowArgs {
/// The new default PoW difficulty
difficulty: u8,
}
impl CommandRunner for PowArgs {
async fn run(self, mut options: CliOptions) -> N34Result<()> {
// FIXME: The signer is not required here
options.config.pow = Some(self.difficulty);
options.config.dump()
}
}

View File

@@ -126,7 +126,7 @@ impl CommandRunner for NewArgs {
self.label,
)?
.dedup_tags()
.pow(options.pow)
.pow(options.pow.unwrap_or_default())
.tags(content_details.clone().into_tags())
// p-tag the reset of the reposotoies owners
.tags(naddrs_iter.clone().map(|n| Tag::public_key(n.public_key)))

View File

@@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
/// `config` subcommands
pub mod config;
/// `issue` subcommands
pub mod issue;
/// `patch` subcommands
@@ -30,6 +32,7 @@ use std::fmt;
use clap::{ArgGroup, Args, Parser};
use nostr::key::{Keys, PublicKey, SecretKey};
use self::config::ConfigSubcommands;
use self::issue::IssueSubcommands;
use self::patch::PatchSubcommands;
use self::reply::ReplyArgs;
@@ -64,8 +67,8 @@ pub struct CliOptions {
#[arg(short, long)]
pub relays: Vec<RelayOrSet>,
/// Proof of Work difficulty when creatring events
#[arg(long, default_value_t = 0)]
pub pow: u8,
#[arg(long, value_name = "DIFFICULTY")]
pub pow: Option<u8>,
/// Config path [default: `$XDG_CONFIG_HOME` or `$HOME/.config`]
#[arg(long, value_name = "PATH", default_value = DEFAULT_FALLBACK_PATH,
hide_default_value = true, value_parser = parsers::parse_config_path
@@ -96,6 +99,11 @@ pub enum Commands {
#[command(subcommand)]
subcommands: PatchSubcommands,
},
/// Manage configuration
Config {
#[command(subcommand)]
subcommands: ConfigSubcommands,
},
/// Reply to issues and patches.
Reply(ReplyArgs),
}
@@ -111,6 +119,7 @@ impl CommandRunner for Commands {
Self::Reply(args) => args.run(options).await,
Self::Sets { subcommands } => subcommands.run(options).await,
Self::Patch { subcommands } => subcommands.run(options).await,
Self::Config { subcommands } => subcommands.run(options).await,
}
}
}
@@ -140,6 +149,8 @@ impl fmt::Debug for CliOptions {
f.debug_struct("CliOptions")
.field("secret_key", &self.secret_key.as_ref().map(|_| "*******"))
.field("relays", &self.relays)
.field("pow", &self.pow)
.field("config", &self.config)
.finish()
}
}

View File

@@ -135,7 +135,7 @@ impl CommandRunner for ReplyArgs {
repos.first().and_then(|r| r.relays.first()).cloned(),
)
.dedup_tags()
.pow(options.pow)
.pow(options.pow.unwrap_or_default())
.tags(content_details.clone().into_tags())
.build(user_pubk);

View File

@@ -106,7 +106,7 @@ impl CommandRunner for AnnounceArgs {
self.force_id,
)?
.dedup_tags()
.pow(options.pow)
.pow(options.pow.unwrap_or_default())
.build(user_pubk);

View File

@@ -57,6 +57,9 @@ pub struct CliConfig {
/// Groups of repositories and relays.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub sets: Vec<RepoRelaySet>,
/// The default PoW difficulty
#[serde(skip_serializing_if = "Option::is_none")]
pub pow: Option<u8>,
}
/// A named group of repositories and relays.

View File

@@ -73,7 +73,10 @@ impl Cli {
}
/// Processes the CLI configuration and returns it if successful.
pub fn post_cli(cli: Cli) -> N34Result<Cli> {
// TODO
pub fn post_cli(mut cli: Cli) -> N34Result<Cli> {
if cli.options.pow.is_none() && cli.options.config.pow.is_some() {
cli.options.pow = cli.options.config.pow;
}
Ok(cli)
}