2021-12-08 03:09:45 +00:00
|
|
|
#![deny(clippy::all)]
|
2021-12-09 21:11:52 +00:00
|
|
|
#![deny(unused_must_use)]
|
2021-11-28 02:34:08 +00:00
|
|
|
|
2022-01-18 17:33:14 +00:00
|
|
|
use veilid_core::xx::*;
|
|
|
|
|
2022-03-11 12:35:41 +00:00
|
|
|
use clap::{Arg, ColorChoice, Command};
|
2021-11-22 16:28:30 +00:00
|
|
|
use flexi_logger::*;
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::net::ToSocketAddrs;
|
2022-06-26 21:00:05 +00:00
|
|
|
use std::path::Path;
|
2022-06-28 03:46:29 +00:00
|
|
|
use tools::*;
|
2021-11-22 16:28:30 +00:00
|
|
|
|
|
|
|
mod client_api_connection;
|
|
|
|
mod command_processor;
|
|
|
|
mod settings;
|
2022-06-28 03:46:29 +00:00
|
|
|
mod tools;
|
2021-11-22 16:28:30 +00:00
|
|
|
mod ui;
|
|
|
|
|
2021-11-29 01:08:50 +00:00
|
|
|
#[allow(clippy::all)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub mod veilid_client_capnp {
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/proto/veilid_client_capnp.rs"));
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:33:14 +00:00
|
|
|
fn parse_command_line(default_config_path: &OsStr) -> Result<clap::ArgMatches, String> {
|
2022-03-11 12:35:41 +00:00
|
|
|
let matches = Command::new("veilid-cli")
|
2021-11-22 16:28:30 +00:00
|
|
|
.version("0.1")
|
2022-01-11 03:36:54 +00:00
|
|
|
.color(ColorChoice::Auto)
|
2021-11-22 16:28:30 +00:00
|
|
|
.about("Veilid Console Client")
|
|
|
|
.arg(
|
2022-01-11 03:36:54 +00:00
|
|
|
Arg::new("address")
|
2021-11-22 16:28:30 +00:00
|
|
|
.required(false)
|
|
|
|
.help("Address to connect to"),
|
|
|
|
)
|
|
|
|
.arg(
|
2022-01-11 03:36:54 +00:00
|
|
|
Arg::new("debug")
|
2021-11-22 16:28:30 +00:00
|
|
|
.long("debug")
|
|
|
|
.help("Turn on debug logging"),
|
|
|
|
)
|
|
|
|
.arg(
|
2022-01-11 03:36:54 +00:00
|
|
|
Arg::new("wait-for-debug")
|
2021-11-22 16:28:30 +00:00
|
|
|
.long("wait-for-debug")
|
|
|
|
.help("Wait for debugger to attach"),
|
|
|
|
)
|
|
|
|
.arg(
|
2022-01-11 03:36:54 +00:00
|
|
|
Arg::new("trace")
|
2021-11-22 16:28:30 +00:00
|
|
|
.long("trace")
|
|
|
|
.conflicts_with("debug")
|
|
|
|
.help("Turn on trace logging"),
|
|
|
|
)
|
|
|
|
.arg(
|
2022-01-11 03:36:54 +00:00
|
|
|
Arg::new("config-file")
|
|
|
|
.short('c')
|
2021-11-22 16:28:30 +00:00
|
|
|
.takes_value(true)
|
|
|
|
.value_name("FILE")
|
|
|
|
.default_value_os(default_config_path)
|
2022-06-26 21:00:05 +00:00
|
|
|
.allow_invalid_utf8(true)
|
2021-11-22 16:28:30 +00:00
|
|
|
.help("Specify a configuration file to use"),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
Ok(matches)
|
|
|
|
}
|
|
|
|
|
2022-06-28 03:46:29 +00:00
|
|
|
fn main() -> Result<(), String> {
|
2021-11-22 16:28:30 +00:00
|
|
|
// Get command line options
|
|
|
|
let default_config_path = settings::Settings::get_default_config_path();
|
|
|
|
let matches = parse_command_line(default_config_path.as_os_str())?;
|
|
|
|
if matches.occurrences_of("wait-for-debug") != 0 {
|
|
|
|
use bugsalot::debugger;
|
|
|
|
debugger::wait_until_attached(None).expect("state() not implemented on this platform");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to load configuration
|
2022-06-26 21:00:05 +00:00
|
|
|
let settings_path = if let Some(config_file) = matches.value_of_os("config-file") {
|
|
|
|
if Path::new(config_file).exists() {
|
|
|
|
Some(config_file)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut settings = settings::Settings::new(settings_path)
|
|
|
|
.map_err(|e| format!("configuration is invalid: {}", e))?;
|
2021-11-22 16:28:30 +00:00
|
|
|
|
|
|
|
// Set config from command line
|
|
|
|
if matches.occurrences_of("debug") != 0 {
|
|
|
|
settings.logging.level = settings::LogLevel::Debug;
|
|
|
|
settings.logging.terminal.enabled = true;
|
|
|
|
}
|
|
|
|
if matches.occurrences_of("trace") != 0 {
|
|
|
|
settings.logging.level = settings::LogLevel::Trace;
|
|
|
|
settings.logging.terminal.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create UI object
|
|
|
|
let mut sivui = ui::UI::new(settings.interface.node_log.scrollback, &settings);
|
|
|
|
|
|
|
|
// Set up loggers
|
|
|
|
{
|
|
|
|
let mut specbuilder = LogSpecBuilder::new();
|
|
|
|
specbuilder.default(settings::convert_loglevel(settings.logging.level));
|
|
|
|
specbuilder.module("cursive_core", LevelFilter::Off);
|
|
|
|
specbuilder.module("cursive_buffered_backend", LevelFilter::Off);
|
|
|
|
specbuilder.module("mio", LevelFilter::Off);
|
|
|
|
specbuilder.module("async_std", LevelFilter::Off);
|
|
|
|
specbuilder.module("async_io", LevelFilter::Off);
|
|
|
|
specbuilder.module("polling", LevelFilter::Off);
|
|
|
|
|
|
|
|
let logger = Logger::with(specbuilder.build());
|
|
|
|
|
|
|
|
if settings.logging.terminal.enabled {
|
|
|
|
let flv = sivui.cursive_flexi_logger();
|
|
|
|
if settings.logging.file.enabled {
|
2022-01-18 17:33:14 +00:00
|
|
|
std::fs::create_dir_all(settings.logging.file.directory.clone())
|
|
|
|
.map_err(map_to_string)?;
|
2021-11-22 16:28:30 +00:00
|
|
|
logger
|
2022-06-26 21:00:05 +00:00
|
|
|
.log_to_file_and_writer(
|
|
|
|
FileSpec::default()
|
|
|
|
.directory(settings.logging.file.directory.clone())
|
|
|
|
.suppress_timestamp(),
|
|
|
|
flv,
|
|
|
|
)
|
2021-11-22 16:28:30 +00:00
|
|
|
.start()
|
|
|
|
.expect("failed to initialize logger!");
|
|
|
|
} else {
|
|
|
|
logger
|
2022-06-26 21:00:05 +00:00
|
|
|
.log_to_writer(flv)
|
2021-11-22 16:28:30 +00:00
|
|
|
.start()
|
|
|
|
.expect("failed to initialize logger!");
|
|
|
|
}
|
|
|
|
} else if settings.logging.file.enabled {
|
2022-01-18 17:33:14 +00:00
|
|
|
std::fs::create_dir_all(settings.logging.file.directory.clone())
|
|
|
|
.map_err(map_to_string)?;
|
2021-11-22 16:28:30 +00:00
|
|
|
logger
|
2022-06-26 21:00:05 +00:00
|
|
|
.log_to_file(
|
|
|
|
FileSpec::default()
|
|
|
|
.directory(settings.logging.file.directory.clone())
|
|
|
|
.suppress_timestamp(),
|
|
|
|
)
|
2021-11-22 16:28:30 +00:00
|
|
|
.start()
|
|
|
|
.expect("failed to initialize logger!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get client address
|
2022-03-13 16:45:36 +00:00
|
|
|
let server_addrs = if let Some(address_arg) = matches.value_of("address") {
|
|
|
|
address_arg
|
2021-11-22 16:28:30 +00:00
|
|
|
.to_socket_addrs()
|
2022-01-18 17:33:14 +00:00
|
|
|
.map_err(|e| format!("Invalid server address '{}'", e))?
|
2021-11-22 16:28:30 +00:00
|
|
|
.collect()
|
|
|
|
} else {
|
2022-03-13 16:45:36 +00:00
|
|
|
settings.address.addrs.clone()
|
|
|
|
};
|
2021-11-22 16:28:30 +00:00
|
|
|
let server_addr = server_addrs.first().cloned();
|
|
|
|
|
|
|
|
// Create command processor
|
|
|
|
debug!("Creating Command Processor ");
|
|
|
|
let mut comproc = command_processor::CommandProcessor::new(sivui.clone(), &settings);
|
|
|
|
sivui.set_command_processor(comproc.clone());
|
|
|
|
|
|
|
|
// Create client api client side
|
|
|
|
info!("Starting API connection");
|
|
|
|
let mut capi = client_api_connection::ClientApiConnection::new(comproc.clone());
|
|
|
|
|
|
|
|
// Save client api in command processor
|
|
|
|
comproc.set_client_api_connection(capi.clone());
|
|
|
|
|
|
|
|
// Keep a connection to the server
|
|
|
|
comproc.set_server_address(server_addr);
|
|
|
|
let mut comproc2 = comproc.clone();
|
|
|
|
let connection_future = comproc.connection_manager();
|
|
|
|
|
2022-06-28 03:46:29 +00:00
|
|
|
// Start async
|
|
|
|
block_on(async move {
|
|
|
|
// Start UI
|
|
|
|
let ui_future = async move {
|
|
|
|
sivui.run_async().await;
|
|
|
|
|
|
|
|
// When UI quits, close connection and command processor cleanly
|
|
|
|
comproc2.quit();
|
|
|
|
capi.disconnect().await;
|
|
|
|
};
|
|
|
|
|
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(feature="rt-async-std")] {
|
|
|
|
use async_std::prelude::*;
|
|
|
|
// Wait for ui and connection to complete
|
|
|
|
let _ = ui_future.join(connection_future).await;
|
|
|
|
} else if #[cfg(feature="rt-tokio")] {
|
|
|
|
// Wait for ui and connection to complete
|
|
|
|
let _ = tokio::join!(ui_future, connection_future);
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 16:28:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|