diff --git a/Cargo.lock b/Cargo.lock index 507668c..ef5095a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -939,6 +939,7 @@ name = "ldk-server" version = "0.1.0" dependencies = [ "bytes", + "clap", "hex-conservative", "http-body-util", "hyper 1.4.1", diff --git a/ldk-server/Cargo.toml b/ldk-server/Cargo.toml index ceebf1c..ac044d7 100644 --- a/ldk-server/Cargo.toml +++ b/ldk-server/Cargo.toml @@ -16,6 +16,7 @@ ldk-server-protos = { path = "../ldk-server-protos" } bytes = "1.4.0" hex = { package = "hex-conservative", version = "0.2.1", default-features = false } rusqlite = { version = "0.28.0", features = ["bundled"] } +clap = { version = "4.0.5", default-features = false, features = ["derive", "std", "error-context", "suggestions", "help"] } [dev-dependencies] rand = "0.8.5" diff --git a/ldk-server/src/args.rs b/ldk-server/src/args.rs new file mode 100644 index 0000000..5a1b2f3 --- /dev/null +++ b/ldk-server/src/args.rs @@ -0,0 +1,7 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +pub struct Cli { + #[arg(short, long)] + pub config: Option, +} diff --git a/ldk-server/src/main.rs b/ldk-server/src/main.rs index 6d31341..26b3cd9 100644 --- a/ldk-server/src/main.rs +++ b/ldk-server/src/main.rs @@ -1,10 +1,12 @@ mod api; +mod args; mod io; mod service; mod util; use crate::service::NodeService; +use clap::Parser; use ldk_node::{Builder, Event, LogLevel}; use tokio::net::TcpListener; @@ -18,16 +20,18 @@ use ldk_node::config::Config; use std::path::Path; use std::sync::Arc; -fn main() { - let args: Vec = std::env::args().collect(); +use args::Cli; - if args.len() < 2 { - eprintln!("Usage: {} config_path", args[0]); - std::process::exit(-1); - } +fn main() { + let args = Cli::parse(); + #[allow(deprecated)] + let config_file = args.config.unwrap_or_else(|| { + // FIXME: change this with a better default path + std::env::home_dir().unwrap().join("ldk-server.config").to_str().unwrap().to_string() + }); let mut ldk_node_config = Config::default(); - let config_file = load_config(Path::new(&args[1])).expect("Invalid configuration file."); + let config_file = load_config(Path::new(&config_file)).expect("Invalid configuration file."); ldk_node_config.log_level = LogLevel::Trace; ldk_node_config.storage_dir_path = config_file.storage_dir_path;