Fingerlink/src/args_parser.rs

127 lines
4 KiB
Rust
Raw Normal View History

2024-02-14 19:35:19 +01:00
use std::net::IpAddr;
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand, ValueEnum};
use tracing::Level;
#[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)]
pub run_mode: Command
}
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,
LoggingLevel::Error => Level::ERROR
}
}
}
#[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,
Error
}
#[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")]
pub max_allowed_rels_in_request: usize
}
#[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
Serve (ServerParameters),
/// Fetch one or multiple handles from Fediverse-compatible software
Fetch {
#[command(flatten)]
save: SaveSettings,
/// The handles to process
handles: Vec<String>,
/// Set this flag to treat HANDLES as a list of files that contain the handles to process
#[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,
},
/// Runs a single query against the database and returns the Resource associated with the value
Query {
/// The resource to query for
resource: String
},
/// Open the resource in your system editor
Editor {
#[command(flatten)]
save: SaveSettings,
/// Query for the resource to edit
resource: String,
#[command(flatten)]
server_reload: ServerReloadOptions,
}
}
#[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
OverwriteMultiple
//TODO: Handlers to remove only the offending aliases, not the whole record?
}
#[derive(Args, Debug)]
pub struct ServerReloadOptions {
/// Set this flag for the application to attempt to reload the running server process automaticallys
#[arg(long, short = 'r')]
pub reload_server: bool,
}