Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Boppy committed Jul 20, 2023
1 parent 2527e52 commit 0cdf9f0
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 56 deletions.
2 changes: 1 addition & 1 deletion crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Command> {
vec![
Expand Down
18 changes: 10 additions & 8 deletions crates/cli/src/subcommands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,23 @@ async fn schema_json(config: Config, address: &str, auth_header: &Option<String>
/// 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<Item = (&'v str, &'v Value)> {
let entities = match value.as_object()
fn find_of_type_in_schema<'v, 't: 'v>(value: &'v Value, ty: &'t str) -> impl Iterator<Item = (&'v str, &'v Value)> {
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));
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/util.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
12 changes: 4 additions & 8 deletions crates/standalone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -575,8 +574,5 @@ pub async fn exec_subcommand(cmd: &str, args: &ArgMatches, is_standalone: bool)
}

pub fn get_subcommands() -> Vec<Command> {
vec![
start::cli(true),
version::cli(),
]
}
vec![start::cli(true), version::cli()]
}
2 changes: 1 addition & 1 deletion crates/standalone/src/main.rs
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
2 changes: 1 addition & 1 deletion crates/standalone/src/subcommands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod start;
pub mod version;
pub mod version;
75 changes: 48 additions & 27 deletions crates/standalone/src/subcommands/start.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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::<String>("listen_addr").unwrap();
let allow_create = args.get_flag("allow_create");
let log_conf_path = args.get_one::<String>("log_conf_path").cloned().or(
if is_standalone { None } else { Some(format!("{}/.spacetime/log.conf", home_dir)) });
let log_dir_path = args.get_one::<String>("log_dir_path").cloned().or(
if is_standalone { None } else { Some(format!("{}/.spacetime", home_dir)) });
let stdb_path = args.get_one::<String>("database_path").cloned().or(
if is_standalone { None } else { Some(format!("{}/.spacetime/stdb", home_dir)) });
let jwt_pub_key_path = args.get_one::<String>("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::<String>("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::<String>("log_conf_path").cloned().or(if is_standalone {
None
} else {
Some(format!("{}/.spacetime/log.conf", home_dir))
});
let log_dir_path = args.get_one::<String>("log_dir_path").cloned().or(if is_standalone {
None
} else {
Some(format!("{}/.spacetime", home_dir))
});
let stdb_path = args.get_one::<String>("database_path").cloned().or(if is_standalone {
None
} else {
Some(format!("{}/.spacetime/stdb", home_dir))
});
let jwt_pub_key_path = args
.get_one::<String>("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::<String>("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);
}

Expand Down
24 changes: 15 additions & 9 deletions crates/standalone/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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.
/// Otherwise if the directory does exist, do nothing.
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)?;
Expand All @@ -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)?;
Expand Down Expand Up @@ -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()
}
}

0 comments on commit 0cdf9f0

Please sign in to comment.