Fingerlink/src/args_parser.rs

133 lines
4.3 KiB
Rust
Raw Normal View History

2024-02-14 19:48:08 +01:00
use clap::{Args, Parser, Subcommand, ValueEnum};
2024-02-14 19:35:19 +01:00
use std::net::IpAddr;
use std::path::PathBuf;
use tracing::Level;
2024-02-14 21:04:40 +01:00
/// A simple program to handle serving static WebFinger data.
2024-02-14 19:35:19 +01:00
#[derive(Debug, Parser)]
#[command(name = "Fingerlink", version, about, long_about = None)]
pub struct MainCommand {
#[arg(long, value_enum, default_value = "info")]
pub log_level: LoggingLevel,
#[command(flatten)]
pub data_paths: DataPaths,
#[command(subcommand)]
2024-02-14 19:48:08 +01:00
pub run_mode: Command,
2024-02-14 19:35:19 +01:00
}
impl MainCommand {
pub fn log_level(&self) -> Level {
match self.log_level {
LoggingLevel::Trace => Level::TRACE,
LoggingLevel::Debug => Level::DEBUG,
LoggingLevel::Info => Level::INFO,
LoggingLevel::Warning => Level::WARN,
2024-02-14 19:48:08 +01:00
LoggingLevel::Error => Level::ERROR,
2024-02-14 19:35:19 +01:00
}
}
}
#[derive(Args, Debug)]
pub struct DataPaths {
#[arg(long, short = 'd', default_value = "records.json")]
pub database_path: PathBuf,
#[arg(long, default_value = "server.pid")]
pub pid_file_path: PathBuf,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)]
pub enum LoggingLevel {
Trace,
Debug,
Info,
Warning,
2024-02-14 19:48:08 +01:00
Error,
2024-02-14 19:35:19 +01:00
}
#[derive(Debug, Clone, Args)]
pub struct ServerParameters {
/// The IP address to bind to
#[arg(long, short = 'b', default_value = "127.0.0.1")]
pub bind_ip: IpAddr,
/// The port to listen on
#[arg(long, short = 'p', default_value = "8080")]
pub bind_port: u16,
/// Whether to ignore the PID file already existing.
#[arg(long)]
pub force_pid: bool,
/// Limits how many rel parameters will be processed in a single query
#[arg(long, default_value = "10")]
2024-02-14 19:48:08 +01:00
pub max_allowed_rels_in_request: usize,
2024-02-14 19:35:19 +01:00
}
#[derive(Debug, Args)]
pub struct SaveSettings {
/// Specify this flag to write the fetched records into the database
#[arg(long, short = 's')]
pub save: bool,
/// Save behaviour when encountering a collision
#[arg(long, short = 'c', default_value = "overwrite-single-skip-multiple")]
pub collision_handling: CollisionHandling,
}
#[derive(Debug, Subcommand)]
pub enum Command {
/// Serve the database
2024-02-14 19:48:08 +01:00
Serve(ServerParameters),
2024-02-14 19:35:19 +01:00
/// Fetch one or multiple handles from Fediverse-compatible software
Fetch {
#[command(flatten)]
save: SaveSettings,
2024-02-14 21:04:40 +01:00
/// The handles to process in the format username@domainó
2024-02-14 19:35:19 +01:00
handles: Vec<String>,
2024-02-14 21:04:40 +01:00
/// Set this flag to treat HANDLES as a list of file paths that contain the handles to process
2024-02-14 19:35:19 +01:00
#[arg(long)]
handles_are_files: bool,
/// The domain to rewrite the acquired handles' subject domain to. If left unspecified, the domain is kept as-is
#[arg(long)]
new_domain: Option<String>,
#[command(flatten)]
server_reload: ServerReloadOptions,
},
2024-02-14 21:04:40 +01:00
/// Runs a single query against the database and returns the resource associated with the value
2024-02-14 19:35:19 +01:00
Query {
/// The resource to query for
2024-02-14 19:48:08 +01:00
resource: String,
2024-02-14 19:35:19 +01:00
},
/// Open the resource in your system editor
Editor {
#[command(flatten)]
save: SaveSettings,
2024-02-14 21:04:40 +01:00
/// The resource to query for and edit
2024-02-14 19:35:19 +01:00
resource: String,
#[command(flatten)]
server_reload: ServerReloadOptions,
2024-02-14 19:48:08 +01:00
},
2024-02-14 20:55:28 +01:00
/// Create a blank database
Init {
/// Force creating a new database even if one exists already. WILL DELETE DATA!
#[arg(long, short = 'f')]
force: bool,
},
2024-02-14 19:35:19 +01:00
}
#[derive(ValueEnum, Debug, Eq, PartialEq, Copy, Clone)]
pub enum CollisionHandling {
/// Terminate import process when encountering a collision.
/// Still inserts resources processed before detecting a collision
Terminate,
/// Skip adding new resource if it would collide with an existing resource
Skip,
/// Only overwrites if there's just a single resource the new item collides with
OverwriteSingleSkipMultiple,
/// Overwrites every already existing resource that the new item collides with
2024-02-14 19:48:08 +01:00
OverwriteMultiple, //TODO: Handlers to remove only the offending aliases, not the whole record?
2024-02-14 19:35:19 +01:00
}
#[derive(Args, Debug)]
pub struct ServerReloadOptions {
2024-02-14 21:11:36 +01:00
/// Set this flag for the application to attempt to reload the running server process automatically
2024-02-14 19:35:19 +01:00
#[arg(long, short = 'r')]
pub reload_server: bool,
2024-02-14 19:48:08 +01:00
}