config work

This commit is contained in:
John Smith
2022-01-18 21:21:11 -05:00
parent 205a6a8fd1
commit effc4aeeac
11 changed files with 196 additions and 50 deletions

View File

@@ -1,10 +1,9 @@
use crate::settings::*;
use std::ffi::OsStr;
use clap::{App, Arg, ArgMatches};
use std::ffi::OsStr;
use std::str::FromStr;
fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap::Error> {
let matches = App::new("veilid-server")
.version("0.1")
.about("Veilid Server")
@@ -22,6 +21,7 @@ fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap
.takes_value(true)
.value_name("FILE")
.default_value_os(default_config_path)
.allow_invalid_utf8(true)
.help("Specify a configuration file to use"),
).arg(
Arg::new("attach")
@@ -54,7 +54,21 @@ fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap
.long("generate-dht-key")
.help("Only generate a new dht key and print it"),
)
.arg(
Arg::new("delete-protected-store")
.long("delete-protected-store")
.help("Delete the entire contents of the protected store (DANGER, NO UNDO!)"),
)
.arg(
Arg::new("delete-table-store")
.long("delete-table-store")
.help("Delete the entire contents of the table store (DANGER, NO UNDO!)"),
)
.arg(
Arg::new("delete-block-store")
.long("delete-block-store")
.help("Delete the entire contents of the block store (DANGER, NO UNDO!)"),
)
.arg(
Arg::new("dump-config")
.long("dump-config")
@@ -72,19 +86,18 @@ fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap
.long("local")
.help("Enable local peer scope")
);
#[cfg(debug_assertions)]
let matches = matches.arg(
Arg::new("wait-for-debug")
.long("wait-for-debug")
.help("Wait for debugger to attach"),
);
#[cfg(debug_assertions)]
let matches = matches.arg(
Arg::new("wait-for-debug")
.long("wait-for-debug")
.help("Wait for debugger to attach"),
);
Ok(matches.get_matches())
}
pub fn process_command_line() -> Result<(Settings, ArgMatches), String> {
// Get command line options
let default_config_path = Settings::get_default_config_path();
let matches = do_clap_matches(default_config_path.as_os_str())
@@ -126,6 +139,7 @@ pub fn process_command_line() -> Result<(Settings, ArgMatches), String> {
}
settingsrw.testing.subnode_index = subnode_index;
}
if matches.occurrences_of("debug") != 0 {
settingsrw.logging.terminal.enabled = true;
settingsrw.logging.terminal.level = LogLevel::Debug;
@@ -140,6 +154,15 @@ pub fn process_command_line() -> Result<(Settings, ArgMatches), String> {
if matches.is_present("local") {
settingsrw.core.network.enable_local_peer_scope = true;
}
if matches.occurrences_of("delete-protected-store") != 0 {
settingsrw.core.protected_store.delete = true;
}
if matches.occurrences_of("delete-block-store") != 0 {
settingsrw.core.block_store.delete = true;
}
if matches.occurrences_of("delete-table-store") != 0 {
settingsrw.core.table_store.delete = true;
}
if matches.occurrences_of("bootstrap") != 0 {
let bootstrap = match matches.value_of("bootstrap") {
Some(x) => {
@@ -170,4 +193,4 @@ pub fn process_command_line() -> Result<(Settings, ArgMatches), String> {
.map_err(|_| "failed to apply subnode index".to_owned())?;
Ok((settings, matches))
}
}

View File

@@ -33,8 +33,8 @@ fn main() -> Result<(), String> {
.map_err(|e| e.to_string());
}
// --- Generate Id ---
if matches.occurrences_of("generate-id") != 0 {
// --- Generate DHT Key ---
if matches.occurrences_of("generate-dht-key") != 0 {
let (key, secret) = veilid_core::generate_secret();
println!("Public: {}\nSecret: {}", key.encode(), secret.encode());
return Ok(());

View File

@@ -39,8 +39,13 @@ core:
allow_insecure_fallback: true
always_use_insecure_storage: false
insecure_fallback_directory: '%INSECURE_FALLBACK_DIRECTORY%'
delete: false
table_store:
directory: '%TABLE_STORE_DIRECTORY%'
delete: false
block_store:
directory: '%BLOCK_STORE_DIRECTORY%'
delete: false
network:
max_connections: 16
connection_initial_timeout: 2000000
@@ -124,6 +129,10 @@ core:
"%TABLE_STORE_DIRECTORY%",
&Settings::get_default_table_store_path().to_string_lossy(),
)
.replace(
"%BLOCK_STORE_DIRECTORY%",
&Settings::get_default_block_store_path().to_string_lossy(),
)
.replace(
"%INSECURE_FALLBACK_DIRECTORY%",
&Settings::get_default_protected_store_insecure_fallback_directory().to_string_lossy(),
@@ -532,6 +541,13 @@ pub struct Testing {
#[derive(Debug, Deserialize, Serialize)]
pub struct TableStore {
pub directory: PathBuf,
pub delete: bool,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct BlockStore {
pub directory: PathBuf,
pub delete: bool,
}
#[derive(Debug, Deserialize, Serialize)]
@@ -539,12 +555,14 @@ pub struct ProtectedStore {
pub allow_insecure_fallback: bool,
pub always_use_insecure_storage: bool,
pub insecure_fallback_directory: PathBuf,
pub delete: bool,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Core {
pub protected_store: ProtectedStore,
pub table_store: TableStore,
pub block_store: BlockStore,
pub network: Network,
}
@@ -693,6 +711,20 @@ impl Settings {
default_config_path
}
pub fn get_default_block_store_path() -> PathBuf {
// Get default configuration file location
let mut default_config_path;
if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
default_config_path = PathBuf::from(my_proj_dirs.data_local_dir());
} else {
default_config_path = PathBuf::from("./");
}
default_config_path.push("block_store");
default_config_path
}
pub fn get_default_protected_store_insecure_fallback_directory() -> PathBuf {
// Get default configuration file location
let mut default_config_path;
@@ -740,6 +772,8 @@ impl Settings {
.to_string_lossy()
.to_string(),
)),
"protected_store.delete" => Ok(Box::new(inner.core.protected_store.delete)),
"table_store.directory" => Ok(Box::new(
inner
.core
@@ -748,6 +782,18 @@ impl Settings {
.to_string_lossy()
.to_string(),
)),
"table_store.delete" => Ok(Box::new(inner.core.table_store.delete)),
"block_store.directory" => Ok(Box::new(
inner
.core
.block_store
.directory
.to_string_lossy()
.to_string(),
)),
"block_store.delete" => Ok(Box::new(inner.core.block_store.delete)),
"network.max_connections" => Ok(Box::new(inner.core.network.max_connections)),
"network.connection_initial_timeout" => {
Ok(Box::new(inner.core.network.connection_initial_timeout))
@@ -1070,16 +1116,27 @@ mod tests {
assert_eq!(s.logging.client.enabled, true);
assert_eq!(s.logging.client.level, LogLevel::Info);
assert_eq!(s.testing.subnode_index, 0);
assert_eq!(
s.core.table_store.directory,
Settings::get_default_table_store_path()
);
assert_eq!(s.core.table_store.delete, false);
assert_eq!(
s.core.block_store.directory,
Settings::get_default_block_store_path()
);
assert_eq!(s.core.block_store.delete, false);
assert_eq!(s.core.protected_store.allow_insecure_fallback, true);
assert_eq!(s.core.protected_store.always_use_insecure_storage, false);
assert_eq!(
s.core.protected_store.insecure_fallback_directory,
Settings::get_default_protected_store_insecure_fallback_directory()
);
assert_eq!(s.core.protected_store.delete, false);
assert_eq!(s.core.network.max_connections, 16);
assert_eq!(s.core.network.connection_initial_timeout, 2_000_000u64);
assert_eq!(s.core.network.node_id, veilid_core::DHTKey::default());