improvement: use clap for command line args

This commit is contained in:
Rasmus Schlunsen
2023-01-14 09:22:11 -06:00
committed by Greg Heartsfield
parent d06d227ebe
commit 73f4f60cc7
6 changed files with 178 additions and 40 deletions
+9 -40
View File
@@ -1,58 +1,27 @@
//! Server process
use clap::Parser;
use nostr_rs_relay::cli::*;
use nostr_rs_relay::config;
use nostr_rs_relay::server::start_server;
use std::env;
use std::sync::mpsc as syncmpsc;
use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender};
use std::thread;
use tracing::info;
use console_subscriber::ConsoleLayer;
/// Return a requested DB name from command line arguments.
fn db_from_args(args: &[String]) -> Option<String> {
if args.len() == 3 && args.get(1) == Some(&"--db".to_owned()) {
return args.get(2).map(std::clone::Clone::clone);
}
None
}
fn print_version() {
println!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
}
fn print_help() {
println!("Usage: nostr-rs-relay [OPTION]...\n");
println!("Options:");
println!(" --help Show this help message and exit");
println!(" --version Show version information and exit");
println!(" --db <directory> Use the <directory> as the location of the database");
}
/// Start running a Nostr relay server.
fn main() {
// setup tracing
let _trace_sub = tracing_subscriber::fmt::try_init();
info!("Starting up from main");
let args = CLIArgs::parse();
// get database directory from args
let args: Vec<String> = env::args().collect();
let help_flag: bool = args.contains(&"--help".to_owned());
// if --help flag was passed, display help and exit
if help_flag {
print_help();
return;
}
let version_flag: bool = args.contains(&"--version".to_owned());
// if --version flag was passed, display version and exit
if version_flag {
print_version();
return;
}
let db_dir: Option<String> = db_from_args(&args);
let db_dir = args.db;
// configure settings from config.toml
// replace default settings with those read from config.toml
let mut settings = config::Settings::new();
@@ -62,8 +31,8 @@ fn main() {
ConsoleLayer::builder().with_default_env().init();
}
// update with database location
if let Some(db) = db_dir {
settings.database.data_directory = db;
if db_dir.len() > 0 {
settings.database.data_directory = db_dir;
}
let (_, ctrl_rx): (MpscSender<()>, MpscReceiver<()>) = syncmpsc::channel();