Skip to content

Commit

Permalink
server: use a command line parser
Browse files Browse the repository at this point in the history
This commit adds a command line parser to the server. The parser is
useful to avoid crashing when trying to run a simple --help command and
also in the future when we will add more configuration options.

It will be really cool if the config file is optional and the user can
configure the server using the command line by appending all the
options to `ldk-server --option1 value1 --option2 value2`.

     Running `target/debug/ldk-server --help`
thread 'main' panicked at ldk-server/src/main.rs:30:56:
Invalid configuration file.: Custom { kind: NotFound, error: "Failed to read config file '--help': No such file or directory (os error 2)" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[1]    750067 IOT instruction (core dumped)  cargo run --bin ldk-server -- --help

Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Dec 7, 2024
1 parent 31026d6 commit 9a240f8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ldk-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 7 additions & 0 deletions ldk-server/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use clap::Parser;

#[derive(Parser, Debug)]
pub struct Cli {
#[arg(short, long)]
pub config: Option<String>,
}
18 changes: 11 additions & 7 deletions ldk-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,16 +20,18 @@ use ldk_node::config::Config;
use std::path::Path;
use std::sync::Arc;

fn main() {
let args: Vec<String> = 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;
Expand Down

0 comments on commit 9a240f8

Please sign in to comment.