This commit is contained in:
parent
3ca3f31d8d
commit
feb2f9c1ac
10 changed files with 338 additions and 124 deletions
16
src/main.rs
16
src/main.rs
|
|
@ -5,13 +5,8 @@ use en::{ONSET, graph::Graph, log, prelude::*, syntax};
|
|||
#[tokio::main]
|
||||
#[expect(clippy::print_stderr, clippy::print_stdout, clippy::use_debug)]
|
||||
async fn main() -> io::Result<()> {
|
||||
log::print_state();
|
||||
let mut instant = now();
|
||||
|
||||
let args = syntax::command::Arguments::default().parse();
|
||||
let address = args.make_address();
|
||||
instant = tlog!(&instant, "Parsed CLI arguments");
|
||||
|
||||
panic::set_hook(Box::new(|info| {
|
||||
let payload = info
|
||||
.payload_as_str()
|
||||
|
|
@ -37,12 +32,23 @@ async fn main() -> io::Result<()> {
|
|||
}));
|
||||
instant = tlog!(&instant, "Set up panic hook");
|
||||
|
||||
let args = syntax::command::Arguments::default().parse();
|
||||
instant = tlog!(&instant, "Parsed CLI arguments");
|
||||
|
||||
if args.flags.version {
|
||||
println!(env!("CARGO_PKG_VERSION"));
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
log::print_state();
|
||||
|
||||
let graph = Graph::load();
|
||||
instant = tlog!(&instant, "Loaded graph");
|
||||
|
||||
let router = en::router::new(graph);
|
||||
tlog!(&instant, "Initialized router");
|
||||
|
||||
let address = args.make_address();
|
||||
let listener =
|
||||
tokio::net::TcpListener::bind(&address).await.map_err(|e| {
|
||||
log!(ERROR, "Failed to create listener at {address}: {e:#?}");
|
||||
|
|
|
|||
|
|
@ -12,6 +12,12 @@ pub struct Arguments {
|
|||
pub hostname: String,
|
||||
pub port: u16,
|
||||
pub graph_path: PathBuf,
|
||||
pub flags: Flags,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Flags {
|
||||
pub version: bool,
|
||||
}
|
||||
|
||||
impl Arguments {
|
||||
|
|
@ -32,6 +38,7 @@ impl Default for Arguments {
|
|||
hostname: String::from("0.0.0.0"),
|
||||
port: 0,
|
||||
graph_path: PathBuf::from("./static/graph.toml"),
|
||||
flags: Flags::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -39,21 +46,33 @@ impl Default for Arguments {
|
|||
fn parse(defaults: &Arguments, args: &[String]) -> Arguments {
|
||||
let mut out_args = defaults.clone();
|
||||
|
||||
let filtered_args = if let Some((head, tail)) = args.split_first() {
|
||||
// The first argument is usually the command path, but this is not
|
||||
// guaranteed on all platforms so we drop it if it is unrecognized.
|
||||
// For now, this is done simply by checking if it starts with a dash
|
||||
let commandless_args = if let Some((head, tail)) = args.split_first() {
|
||||
if head.starts_with('-') { args } else { tail }
|
||||
} else {
|
||||
args
|
||||
};
|
||||
|
||||
for arg in filtered_args.chunks(2) {
|
||||
let mut nonflag_args: Vec<&str> = vec![];
|
||||
for arg in commandless_args {
|
||||
if arg == "--version" || arg == "-v" {
|
||||
out_args.flags.version = true;
|
||||
} else {
|
||||
nonflag_args.push(arg);
|
||||
}
|
||||
}
|
||||
|
||||
for arg in nonflag_args.chunks(2) {
|
||||
if let Some(argument) = arg.first()
|
||||
&& let Some(parameter) = arg.get(1)
|
||||
{
|
||||
if argument.eq("-h") || argument.eq("--hostname") {
|
||||
out_args.hostname = String::from(parameter);
|
||||
} else if argument.eq("-p") || argument.eq("--port") {
|
||||
if *argument == "-h" || *argument == "--hostname" {
|
||||
out_args.hostname = String::from(*parameter);
|
||||
} else if *argument == "-p" || *argument == "--port" {
|
||||
out_args.port = parameter.parse().unwrap_or(out_args.port);
|
||||
} else if argument.eq("-g") || argument.eq("--graph") {
|
||||
} else if *argument == "-g" || *argument == "--graph" {
|
||||
out_args.graph_path = PathBuf::from(parameter);
|
||||
} else {
|
||||
if FIRST_PARSE.load(Ordering::SeqCst) {
|
||||
|
|
@ -78,6 +97,7 @@ mod tests {
|
|||
hostname: String::from("localhost"),
|
||||
port: 3007,
|
||||
graph_path: PathBuf::default(),
|
||||
flags: Flags::default(),
|
||||
};
|
||||
|
||||
assert_eq!(args.make_address(), "localhost:3007");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue