Skip to content

Commit

Permalink
[release/v1.0.0-rc1-nice-to-have]: Merge remote-tracking branch 'orig…
Browse files Browse the repository at this point in the history
…in/noa/directory-structure' into release/v1.0.0-rc1-nice-to-have
  • Loading branch information
bfops committed Nov 1, 2024
2 parents 1ebe6d3 + badf7d6 commit ca98aad
Show file tree
Hide file tree
Showing 56 changed files with 1,845 additions and 975 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ members = [
"crates/fs-utils",
"crates/lib",
"crates/metrics",
"crates/paths",
"crates/physical-plan",
"crates/primitives",
"crates/sats",
Expand Down Expand Up @@ -102,6 +103,7 @@ spacetimedb-execution = { path = "crates/execution", version = "1.0.0-rc1" }
spacetimedb-expr = { path = "crates/expr", version = "1.0.0-rc1" }
spacetimedb-lib = { path = "crates/lib", default-features = false, version = "1.0.0-rc1" }
spacetimedb-metrics = { path = "crates/metrics", version = "1.0.0-rc1" }
spacetimedb-paths = { path = "crates/paths", version = "1.0.0-rc1" }
spacetimedb-physical-plan = { path = "crates/physical-plan", version = "1.0.0-rc1" }
spacetimedb-primitives = { path = "crates/primitives", version = "1.0.0-rc1" }
spacetimedb-sats = { path = "crates/sats", version = "1.0.0-rc1" }
Expand Down Expand Up @@ -173,9 +175,11 @@ indicatif = "0.16"
insta = { version = "1.21.0", features = ["toml"] }
is-terminal = "0.4"
itertools = "0.12"
itoa = "1"
jsonwebtoken = { git = "https://github.com/jsdt/jsonwebtoken.git", rev = "7f0cef63c74f58dfa912e88da844932ba4c71562"}
lazy_static = "1.4.0"
log = "0.4.17"
memchr = "2"
mimalloc = "0.1.39"
nohash-hasher = "0.2"
once_cell = "1.16"
Expand Down Expand Up @@ -205,6 +209,7 @@ rustyline = { version = "12.0.0", features = [] }
scoped-tls = "1.0.1"
scopeguard = "1.1.0"
second-stack = "0.3"
semver = "1"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = { version = "1.0.128", features = ["raw_value", "arbitrary_precision"] }
serde_path_to_error = "0.1.9"
Expand Down Expand Up @@ -253,6 +258,7 @@ uuid = { version = "1.2.1", features = ["v4"] }
walkdir = "2.2.5"
wasmbin = "0.6"
webbrowser = "1.0.2"
xdg = "2.5"

# Vendor the openssl we rely on, rather than depend on a
# potentially very old system version.
Expand Down
11 changes: 6 additions & 5 deletions crates/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ name = "summarize"
bench = false

[dependencies]
spacetimedb-client-api = { path = "../client-api" }
spacetimedb-core = { path = "../core", features = ["test"] }
spacetimedb-data-structures.workspace = true
spacetimedb-lib = { path = "../lib" }
spacetimedb-core = { path = "../core", features = ["test"] }
spacetimedb-paths.workspace = true
spacetimedb-primitives = { path = "../primitives" }
spacetimedb-sats = { path = "../sats" }
spacetimedb-schema = { workspace = true, features = ["test"] }
spacetimedb-standalone = { path = "../standalone" }
spacetimedb-client-api = { path = "../client-api" }
spacetimedb-testing = { path = "../testing" }
spacetimedb-primitives = { path = "../primitives" }
spacetimedb-table = { path = "../table" }
spacetimedb-schema = { workspace = true, features = ["test"] }
spacetimedb-testing = { path = "../testing" }

anyhow.workspace = true
anymap.workspace = true
Expand Down
8 changes: 5 additions & 3 deletions crates/bench/src/spacetime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use spacetimedb_lib::{
sats::{product, ArrayValue},
AlgebraicValue,
};
use spacetimedb_paths::RootDir;
use spacetimedb_primitives::ColId;
use spacetimedb_testing::modules::{start_runtime, CompilationMode, CompiledModule, LoggerRecord, ModuleHandle};
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -77,9 +78,10 @@ impl BenchDatabase for SpacetimeModule {
let module = runtime.block_on(async {
// We keep a saved database at "crates/bench/.spacetime".
// This is mainly used for caching wasmtime native artifacts.
BENCHMARKS_MODULE
.load_module(config, Some(Path::new(env!("CARGO_MANIFEST_DIR"))))
.await
// It's fine that we're constructing this path ad-hoc, as it's just
// a path location for tests, not part of our stable directory structure.
let path = RootDir(Path::new(env!("CARGO_MANIFEST_DIR")).join(".spacetime"));
BENCHMARKS_MODULE.load_module(config, Some(&path)).await
});

for table in module.client.module.info.module_def.tables() {
Expand Down
7 changes: 4 additions & 3 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ bench = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
spacetimedb-primitives.workspace = true
spacetimedb-client-api-messages.workspace = true
spacetimedb-core.workspace = true
spacetimedb-data-structures.workspace = true
spacetimedb-fs-utils.workspace = true
spacetimedb-lib.workspace = true
spacetimedb-paths.workspace = true
spacetimedb-primitives.workspace = true
spacetimedb-schema.workspace = true
spacetimedb-standalone = { workspace = true, optional = true }
spacetimedb-client-api-messages.workspace = true
spacetimedb-fs-utils.workspace = true

anyhow.workspace = true
base64.workspace = true
Expand Down
71 changes: 21 additions & 50 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ use crate::util::{contains_protocol, host_or_url_to_host_and_protocol};
use anyhow::Context;
use jsonwebtoken::DecodingKey;
use serde::{Deserialize, Serialize};
use spacetimedb_fs_utils::{atomic_write, create_parent_dir};
use std::{
fs,
path::{Path, PathBuf},
};
use spacetimedb_fs_utils::atomic_write;
use spacetimedb_paths::cli::CliTomlPath;

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ServerConfig {
Expand Down Expand Up @@ -69,13 +66,9 @@ pub struct RawConfig {
#[derive(Debug, Clone)]
pub struct Config {
home: RawConfig,
home_path: CliTomlPath,
}

const HOME_CONFIG_DIR: &str = ".spacetime";
const CONFIG_FILENAME: &str = "config.toml";
const SPACETIME_FILENAME: &str = "spacetime.toml";
const DOT_SPACETIME_FILENAME: &str = ".spacetime.toml";

const NO_DEFAULT_SERVER_ERROR_MESSAGE: &str = "No default server configuration.
Set an existing server as the default with:
\tspacetime server set-default <server>
Expand Down Expand Up @@ -572,57 +565,35 @@ impl Config {
&self.home.server_configs
}

fn find_config_path(config_dir: &Path) -> Option<PathBuf> {
[DOT_SPACETIME_FILENAME, SPACETIME_FILENAME, CONFIG_FILENAME]
.iter()
.map(|filename| config_dir.join(filename))
.find(|path| path.exists())
}

fn system_config_path() -> PathBuf {
if let Some(config_path) = std::env::var_os("SPACETIME_CONFIG_FILE") {
config_path.into()
} else {
let mut config_path = dirs::home_dir().unwrap();
config_path.push(HOME_CONFIG_DIR);
Self::find_config_path(&config_path).unwrap_or_else(|| config_path.join(CONFIG_FILENAME))
}
}

fn load_from_file(config_path: &Path) -> anyhow::Result<RawConfig> {
let text = fs::read_to_string(config_path)?;
Ok(toml::from_str(&text)?)
}

pub fn load() -> anyhow::Result<Self> {
let home_path = Self::system_config_path();
let config = if home_path.exists() {
Self {
home: Self::load_from_file(&home_path)
.inspect_err(|e| eprintln!("config file {home_path:?} is invalid: {e:#?}"))?,
pub fn load(home_path: CliTomlPath) -> anyhow::Result<Self> {
let home = spacetimedb::config::parse_config::<RawConfig>(home_path.as_ref())
.with_context(|| format!("config file {} is invalid", home_path.display()))?;
Ok(match home {
Some(home) => Self { home, home_path },
None => {
let config = Self {
home: RawConfig::new_with_localhost(),
home_path,
};
config.save();
config
}
} else {
let config = Self {
home: RawConfig::new_with_localhost(),
};
config.save();
config
};
Ok(config)
})
}

#[doc(hidden)]
/// Used in tests.
pub fn new_with_localhost() -> Self {
pub fn new_with_localhost(home_path: CliTomlPath) -> Self {
Self {
home: RawConfig::new_with_localhost(),
home_path,
}
}

pub fn save(&self) {
let home_path = Self::system_config_path();
let home_path = &self.home_path;
// If the `home_path` is in a directory, ensure it exists.
create_parent_dir(home_path.as_ref()).unwrap();
home_path.create_parent().unwrap();

let config = toml::to_string_pretty(&self.home).unwrap();

Expand All @@ -637,7 +608,7 @@ impl Config {
//
// We should address this issue, but we currently don't expect it to arise very frequently
// (see https://github.com/clockworklabs/SpacetimeDB/pull/1341#issuecomment-2150857432).
if let Err(e) = atomic_write(&home_path, config) {
if let Err(e) = atomic_write(&home_path.0, config) {
eprintln!("Could not save config file: {e}")
}
}
Expand Down
10 changes: 8 additions & 2 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod util;
use clap::{ArgMatches, Command};

pub use config::Config;
use spacetimedb_paths::SpacetimePaths;
use spacetimedb_standalone::subcommands::start::ProgramMode;
pub use subcommands::*;
pub use tasks::build;
Expand Down Expand Up @@ -42,7 +43,12 @@ pub fn get_subcommands() -> Vec<Command> {
]
}

pub async fn exec_subcommand(config: Config, cmd: &str, args: &ArgMatches) -> Result<(), anyhow::Error> {
pub async fn exec_subcommand(
config: Config,
paths: &SpacetimePaths,
cmd: &str,
args: &ArgMatches,
) -> Result<(), anyhow::Error> {
match cmd {
"version" => version::exec(config, args).await,
"call" => call::exec(config, args).await,
Expand All @@ -60,7 +66,7 @@ pub async fn exec_subcommand(config: Config, cmd: &str, args: &ArgMatches) -> Re
"server" => server::exec(config, args).await,
"subscribe" => subscribe::exec(config, args).await,
#[cfg(feature = "standalone")]
"start" => start::exec(args).await,
"start" => start::exec(Some(paths), args).await,
"login" => login::exec(config, args).await,
"logout" => logout::exec(config, args).await,
"upgrade" => upgrade::exec(config, args).await,
Expand Down
Loading

0 comments on commit ca98aad

Please sign in to comment.