diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 33d7051a3e..4f751b465d 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -7,8 +7,8 @@ pub mod util; use clap::{ArgMatches, Command}; pub use config::Config; -pub use subcommands::*; use spacetimedb_standalone::subcommands::start; +pub use subcommands::*; pub fn get_subcommands() -> Vec { vec![ diff --git a/crates/cli/src/subcommands/call.rs b/crates/cli/src/subcommands/call.rs index f3b1cf6dfa..e29423a956 100644 --- a/crates/cli/src/subcommands/call.rs +++ b/crates/cli/src/subcommands/call.rs @@ -246,21 +246,23 @@ async fn schema_json(config: Config, address: &str, auth_header: &Option /// Returns all the names of items in `value` that match `type`. /// /// For example, `type` can be `"reducer"`. -fn find_of_type_in_schema<'v, 't: 'v>( - value: &'v Value, - ty: &'t str, -) -> impl Iterator { - let entities = match value.as_object() +fn find_of_type_in_schema<'v, 't: 'v>(value: &'v Value, ty: &'t str) -> impl Iterator { + let entities = match value + .as_object() .and_then(|o| o.get("entities")) - .and_then(|e| e.as_object()) { + .and_then(|e| e.as_object()) + { Some(e) => e, - None => return Either::Left(iter::empty()) + None => return Either::Left(iter::empty()), }; let iter = entities .into_iter() .filter(move |(_, value)| { - let obj = match value.as_object() { Some(o) => o, None => return false }; + let obj = match value.as_object() { + Some(o) => o, + None => return false, + }; obj.get("type").filter(|x| x.as_str() == Some(ty)).is_some() }) .map(|(key, value)| (key.as_str(), value)); diff --git a/crates/cli/src/util.rs b/crates/cli/src/util.rs index c55f80f2b8..c29677842f 100644 --- a/crates/cli/src/util.rs +++ b/crates/cli/src/util.rs @@ -1,8 +1,8 @@ -use std::process::exit; use reqwest::RequestBuilder; use serde::Deserialize; use spacetimedb_lib::name::{is_address, DnsLookupResponse, RegisterTldResult, ReverseDNSResponse}; use spacetimedb_lib::Identity; +use std::process::exit; use crate::config::{Config, IdentityConfig}; diff --git a/crates/standalone/src/lib.rs b/crates/standalone/src/lib.rs index fabd1a25ca..1f331c43ef 100644 --- a/crates/standalone/src/lib.rs +++ b/crates/standalone/src/lib.rs @@ -3,7 +3,9 @@ pub mod subcommands; pub mod util; mod worker_db; +use crate::subcommands::{start, version}; use anyhow::Context; +use clap::{ArgMatches, Command}; use openssl::ec::{EcGroup, EcKey}; use openssl::nid::Nid; use openssl::pkey::PKey; @@ -30,9 +32,7 @@ use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; use std::sync::Arc; -use clap::{ArgMatches, Command}; use worker_db::WorkerDb; -use crate::subcommands::{start, version}; pub struct StandaloneEnv { worker_db: WorkerDb, @@ -565,7 +565,6 @@ impl StandaloneEnv { } } - pub async fn exec_subcommand(cmd: &str, args: &ArgMatches, is_standalone: bool) -> Result<(), anyhow::Error> { match cmd { "start" => start::exec(args, is_standalone).await, @@ -575,8 +574,5 @@ pub async fn exec_subcommand(cmd: &str, args: &ArgMatches, is_standalone: bool) } pub fn get_subcommands() -> Vec { - vec![ - start::cli(true), - version::cli(), - ] -} \ No newline at end of file + vec![start::cli(true), version::cli()] +} diff --git a/crates/standalone/src/main.rs b/crates/standalone/src/main.rs index e94cb31878..15cdda06e5 100644 --- a/crates/standalone/src/main.rs +++ b/crates/standalone/src/main.rs @@ -1,9 +1,9 @@ use clap::Command; use tokio::runtime::Builder; +use spacetimedb_standalone::*; use std::panic; use std::process; -use spacetimedb_standalone::*; async fn async_main() -> anyhow::Result<()> { let (cmd, subcommand_args) = util::match_subcommand_or_exit(get_command()); diff --git a/crates/standalone/src/subcommands/mod.rs b/crates/standalone/src/subcommands/mod.rs index c0bfa767ad..66ca965c33 100644 --- a/crates/standalone/src/subcommands/mod.rs +++ b/crates/standalone/src/subcommands/mod.rs @@ -1,2 +1,2 @@ pub mod start; -pub mod version; \ No newline at end of file +pub mod version; diff --git a/crates/standalone/src/subcommands/start.rs b/crates/standalone/src/subcommands/start.rs index fde939e44b..7e5268dfde 100644 --- a/crates/standalone/src/subcommands/start.rs +++ b/crates/standalone/src/subcommands/start.rs @@ -1,12 +1,12 @@ -use std::net::TcpListener; -use std::sync::Arc; -use clap::{Arg, ArgMatches}; -use clap::ArgAction::SetTrue; -use spacetimedb::{startup, worker_metrics}; -use spacetimedb::db::db_metrics; use crate::routes::router; -use crate::StandaloneEnv; use crate::util::{create_dir_or_err, create_file_with_contents}; +use crate::StandaloneEnv; +use clap::ArgAction::SetTrue; +use clap::{Arg, ArgMatches}; +use spacetimedb::db::db_metrics; +use spacetimedb::{startup, worker_metrics}; +use std::net::TcpListener; +use std::sync::Arc; pub fn cli(is_standalone: bool) -> clap::Command { clap::Command::new("start") @@ -15,33 +15,29 @@ pub fn cli(is_standalone: bool) -> clap::Command { Arg::new("listen_addr") .long("listen-addr") .short('l') - .default_value(if is_standalone { - "0.0.0.0:80" - } else { - "127.0.0.1:3000" - }) + .default_value(if is_standalone { "0.0.0.0:80" } else { "127.0.0.1:3000" }) .help("The address and port where SpacetimeDB should listen for connections"), ) .arg( Arg::new("log_conf_path") .long("log-conf-path") - .help("The path of the file that contains the log configuration for SpacetimeDB") + .help("The path of the file that contains the log configuration for SpacetimeDB"), ) .arg( Arg::new("log_dir_path") .long("log-dir-path") - .help("The path to the directory that should contain logs for SpacetimeDB") + .help("The path to the directory that should contain logs for SpacetimeDB"), ) .arg( Arg::new("database_path") .long("database-path") - .help("The path to the directory that should contain the database files for SpacetimeDB") + .help("The path to the directory that should contain the database files for SpacetimeDB"), ) .arg( Arg::new("allow_create") .long("allow-create") .action(SetTrue) - .help("Allows for the creation of files and directories that don't exist") + .help("Allows for the creation of files and directories that don't exist"), ) .arg( Arg::new("enable_tracy") @@ -73,20 +69,45 @@ pub async fn exec(args: &ArgMatches, is_standalone: bool) -> anyhow::Result<()> let home_dir = std::env::var("HOME")?; let listen_addr = args.get_one::("listen_addr").unwrap(); let allow_create = args.get_flag("allow_create"); - let log_conf_path = args.get_one::("log_conf_path").cloned().or( - if is_standalone { None } else { Some(format!("{}/.spacetime/log.conf", home_dir)) }); - let log_dir_path = args.get_one::("log_dir_path").cloned().or( - if is_standalone { None } else { Some(format!("{}/.spacetime", home_dir)) }); - let stdb_path = args.get_one::("database_path").cloned().or( - if is_standalone { None } else { Some(format!("{}/.spacetime/stdb", home_dir)) }); - let jwt_pub_key_path = args.get_one::("jwt_pub_key_path").cloned().or( - if is_standalone { None } else { Some(format!("{}/.spacetime/id_ecdsa.pub", home_dir)) }); - let jwt_priv_key_path = args.get_one::("jwt_pub_key_path").cloned().or( - if is_standalone { None } else { Some(format!("{}/.spacetime/id_ecdsa", home_dir)) }); + let log_conf_path = args.get_one::("log_conf_path").cloned().or(if is_standalone { + None + } else { + Some(format!("{}/.spacetime/log.conf", home_dir)) + }); + let log_dir_path = args.get_one::("log_dir_path").cloned().or(if is_standalone { + None + } else { + Some(format!("{}/.spacetime", home_dir)) + }); + let stdb_path = args.get_one::("database_path").cloned().or(if is_standalone { + None + } else { + Some(format!("{}/.spacetime/stdb", home_dir)) + }); + let jwt_pub_key_path = args + .get_one::("jwt_pub_key_path") + .cloned() + .or(if is_standalone { + None + } else { + Some(format!("{}/.spacetime/id_ecdsa.pub", home_dir)) + }); + let jwt_priv_key_path = args + .get_one::("jwt_pub_key_path") + .cloned() + .or(if is_standalone { + None + } else { + Some(format!("{}/.spacetime/id_ecdsa", home_dir)) + }); let enable_tracy = args.get_flag("enable_tracy"); if let Some(log_conf_path) = log_conf_path { - create_file_with_contents(allow_create, &log_conf_path, include_str!("../../../../crates/standalone/log.conf"))?; + create_file_with_contents( + allow_create, + &log_conf_path, + include_str!("../../../../crates/standalone/log.conf"), + )?; std::env::set_var("SPACETIMEDB_LOG_CONFIG", log_conf_path); } diff --git a/crates/standalone/src/util.rs b/crates/standalone/src/util.rs index f300af66cd..1ac2982614 100644 --- a/crates/standalone/src/util.rs +++ b/crates/standalone/src/util.rs @@ -1,8 +1,8 @@ +use clap::error::{ContextKind, ContextValue}; +use clap::{ArgMatches, Command}; use std::env; use std::path::Path; use std::process::exit; -use clap::{ArgMatches, Command}; -use clap::error::{ContextKind, ContextValue}; /// If allow_create is set to true and the directory is missing, create it. Otherwise /// if allow_create is set to false and the directory is missing an error is returned. @@ -10,7 +10,10 @@ use clap::error::{ContextKind, ContextValue}; pub fn create_dir_or_err(allow_create: bool, path: &str) -> anyhow::Result<()> { if !Path::new(path).is_dir() { if !allow_create { - return Err(anyhow::anyhow!("Directory {} does not exist, pass --allow-create to create it", path)); + return Err(anyhow::anyhow!( + "Directory {} does not exist, pass --allow-create to create it", + path + )); } println!("Creating directory {}", path); std::fs::create_dir_all(path)?; @@ -25,7 +28,10 @@ pub fn create_file_with_contents(allow_create: bool, path: &str, contents: &str) create_dir_or_err(allow_create, Path::new(path).parent().unwrap().to_str().unwrap())?; if !Path::new(path).is_file() { if !allow_create { - return Err(anyhow::anyhow!("File {} does not exist, pass --allow-create to create it", path)); + return Err(anyhow::anyhow!( + "File {} does not exist, pass --allow-create to create it", + path + )); } println!("Creating file {}", path); std::fs::write(path, contents)?; @@ -66,9 +72,9 @@ pub fn match_subcommand_or_exit(command: Command) -> (String, ArgMatches) { pub fn get_exe_name() -> String { let exe_path = env::current_exe().expect("Failed to get executable path"); - let executable_name = Path::new(&exe_path) - .file_stem() - .and_then(|n| n.to_str()) - .unwrap_or("unknown"); + let executable_name = Path::new(&exe_path) + .file_stem() + .and_then(|n| n.to_str()) + .unwrap_or("unknown"); executable_name.to_string() -} \ No newline at end of file +}