Skip to content

Commit

Permalink
CLI - Remove project-specific config files (#1665)
Browse files Browse the repository at this point in the history
Co-authored-by: Zeke Foppa <bfops@users.noreply.github.com>
  • Loading branch information
bfops and bfops authored Sep 24, 2024
1 parent eeaa00a commit 336056e
Showing 1 changed file with 20 additions and 65 deletions.
85 changes: 20 additions & 65 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ pub struct RawConfig {

#[derive(Debug, Clone)]
pub struct Config {
proj: RawConfig,
home: RawConfig,
}

Expand Down Expand Up @@ -583,10 +582,7 @@ Fetch the server's fingerprint with:

impl Config {
pub fn default_server_name(&self) -> Option<&str> {
self.proj
.default_server
.as_deref()
.or(self.home.default_server.as_deref())
self.home.default_server.as_deref()
}

/// Add a `ServerConfig` to the home configuration.
Expand Down Expand Up @@ -645,18 +641,15 @@ impl Config {
///
/// Returns the URL of the default server if `server` is `None`.
///
/// Entries in the project configuration supersede entries in the home configuration.
///
/// If `server` is `Some` and is a complete URL,
/// including protocol and hostname,
/// returns that URL without accessing the configuration.
///
/// Returns an `Err` if:
/// - `server` is `Some`, but not a complete URL,
/// and the supplied name does not refer to any server
/// in either the project or the home configuration.
/// - `server` is `None`, but neither the home nor the project configuration
/// has a default server.
/// in the configuration.
/// - `server` is `None`, but the configuration does not have a default server.
pub fn get_host_url(&self, server: Option<&str>) -> anyhow::Result<String> {
Ok(format!("{}://{}", self.protocol(server)?, self.host(server)?))
}
Expand All @@ -665,36 +658,32 @@ impl Config {
///
/// Returns the hostname of the default server if `server` is `None`.
///
/// Entries in the project configuration supersede entries in the home configuration.
///
/// If `server` is `Some` and is a complete URL,
/// including protocol and hostname,
/// returns that hostname without accessing the configuration.
///
/// Returns an `Err` if:
/// - `server` is `Some`, but not a complete URL,
/// and the supplied name does not refer to any server
/// in either the project or the home configuration.
/// - `server` is `None`, but neither the home nor the project configuration
/// has a default server.
/// in the configuration.
/// - `server` is `None`, but the configuration does not
/// have a default server.
pub fn host<'a>(&'a self, server: Option<&'a str>) -> anyhow::Result<&'a str> {
if let Some(server) = server {
if contains_protocol(server) {
Ok(host_or_url_to_host_and_protocol(server).0)
} else {
self.proj.host(server).or_else(|_| self.home.host(server))
self.home.host(server)
}
} else {
self.proj.default_host().or_else(|_| self.home.default_host())
self.home.default_host()
}
}

/// Get the protocol of the specified `server`, either `"http"` or `"https"`.
///
/// Returns the protocol of the default server if `server` is `None`.
///
/// Entries in the project configuration supersede entries in the home configuration.
///
/// If `server` is `Some` and is a complete URL,
/// including protocol and hostname,
/// returns that protocol without accessing the configuration.
Expand All @@ -703,31 +692,26 @@ impl Config {
/// Returns an `Err` if:
/// - `server` is `Some`, but not a complete URL,
/// and the supplied name does not refer to any server
/// in either the project or the home configuration.
/// - `server` is `None`, but neither the home nor the project configuration
/// has a default server.
/// in the configuration.
/// - `server` is `None`, but the configuration does not have a default server.
pub fn protocol<'a>(&'a self, server: Option<&'a str>) -> anyhow::Result<&'a str> {
if let Some(server) = server {
if contains_protocol(server) {
Ok(host_or_url_to_host_and_protocol(server).1.unwrap())
} else {
self.proj.protocol(server).or_else(|_| self.home.protocol(server))
self.home.protocol(server)
}
} else {
self.proj.default_protocol().or_else(|_| self.home.default_protocol())
self.home.default_protocol()
}
}

pub fn default_identity(&self, server: Option<&str>) -> anyhow::Result<&str> {
if let Some(server) = server {
let (host, _) = host_or_url_to_host_and_protocol(server);
self.proj
.default_identity(host)
.or_else(|_| self.home.default_identity(host))
self.home.default_identity(host)
} else {
self.proj
.default_server_default_identity()
.or_else(|_| self.home.default_server_default_identity())
self.home.default_server_default_identity()
}
}

Expand Down Expand Up @@ -806,21 +790,6 @@ impl Config {
Ok(toml::from_str(&text)?)
}

fn load_proj_config() -> RawConfig {
// TODO(cloutiertyler): For now we're checking for a spacetime.toml file
// in the current directory. Eventually this should really be that we
// search parent directories above the current directory to find
// spacetime.toml files like a .gitignore file
let cur_dir = std::env::current_dir().expect("No current working directory!");
if let Some(config_path) = Self::find_config_path(&cur_dir) {
Self::load_from_file(&config_path)
.inspect_err(|e| eprintln!("config file {config_path:?} is invalid: {e:#}"))
.unwrap_or_default()
} else {
Default::default()
}
}

pub fn load() -> Self {
let home_path = Self::system_config_path();
let home = if home_path.exists() {
Expand All @@ -831,17 +800,14 @@ impl Config {
RawConfig::new_with_localhost()
};

let proj = Self::load_proj_config();

Self { home, proj }
Self { home }
}

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

Expand Down Expand Up @@ -990,13 +956,9 @@ Import an existing identity with:
pub fn set_default_identity_if_unset(&mut self, server: Option<&str>, identity: &str) -> anyhow::Result<()> {
if let Some(server) = server {
let (host, _) = host_or_url_to_host_and_protocol(server);
self.proj
.set_default_identity_if_unset(host, identity)
.or_else(|_| self.home.set_default_identity_if_unset(host, identity))
self.home.set_default_identity_if_unset(host, identity)
} else {
self.proj
.default_server_set_default_identity_if_unset(identity)
.or_else(|_| self.home.default_server_set_default_identity_if_unset(identity))
self.home.default_server_set_default_identity_if_unset(identity)
}
}

Expand Down Expand Up @@ -1025,23 +987,16 @@ Update the server's fingerprint with:
let (host, _) = host_or_url_to_host_and_protocol(server);
Ok(host)
} else {
self.proj
.default_server()
.or_else(|_| self.home.default_server())
.map(ServerConfig::nick_or_host)
self.home.default_server().map(ServerConfig::nick_or_host)
}
}

pub fn server_fingerprint(&self, server: Option<&str>) -> anyhow::Result<Option<&str>> {
if let Some(server) = server {
let (host, _) = host_or_url_to_host_and_protocol(server);
self.proj
.server_fingerprint(host)
.or_else(|_| self.home.server_fingerprint(host))
self.home.server_fingerprint(host)
} else {
self.proj
.default_server_fingerprint()
.or_else(|_| self.home.default_server_fingerprint())
self.home.default_server_fingerprint()
}
}

Expand Down

2 comments on commit 336056e

@github-actions
Copy link

@github-actions github-actions bot commented on 336056e Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

@github-actions
Copy link

@github-actions github-actions bot commented on 336056e Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callgrind benchmark results

Callgrind Benchmark Report

These benchmarks were run using callgrind,
an instruction-level profiler. They allow comparisons between sqlite (sqlite), SpacetimeDB running through a module (stdb_module), and the underlying SpacetimeDB data storage engine (stdb_raw). Callgrind emulates a CPU to collect the below estimates.

Measurement changes larger than five percent are in bold.

In-memory benchmarks

callgrind: empty transaction

db total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw 5380 5380 0.00% 5414 5414 0.00%
sqlite 5555 5555 0.00% 5971 5971 0.00%

callgrind: filter

db schema indices count preload _column data_type total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str no_index 64 128 2 string 119003 119003 0.00% 119609 119609 0.00%
stdb_raw u32_u64_str no_index 64 128 1 u64 75374 75374 0.00% 75856 75856 0.00%
stdb_raw u32_u64_str btree_each_column 64 128 2 string 22611 22611 0.00% 23121 23125 -0.02%
stdb_raw u32_u64_str btree_each_column 64 128 1 u64 21578 21578 0.00% 22020 22020 0.00%
sqlite u32_u64_str no_index 64 128 2 string 144677 144677 0.00% 146225 146225 0.00%
sqlite u32_u64_str no_index 64 128 1 u64 124036 124036 0.00% 125350 125350 0.00%
sqlite u32_u64_str btree_each_column 64 128 2 string 134476 134476 0.00% 136064 136068 -0.00%
sqlite u32_u64_str btree_each_column 64 128 1 u64 131343 131349 -0.00% 132751 132757 -0.00%

callgrind: insert bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 64 128 898578 898590 -0.00% 952118 952154 -0.00%
stdb_raw u32_u64_str btree_each_column 64 128 1050053 1048949 0.11% 1116937 1115517 0.13%
sqlite u32_u64_str unique_0 64 128 398292 398292 0.00% 414946 414950 -0.00%
sqlite u32_u64_str btree_each_column 64 128 983609 983609 0.00% 1023485 1023489 -0.00%

callgrind: iterate

db schema indices count total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 152676 152676 0.00% 152806 152806 0.00%
stdb_raw u32_u64_str unique_0 64 15701 15701 0.00% 15827 15827 0.00%
sqlite u32_u64_str unique_0 1024 1068223 1068223 0.00% 1071455 1071455 0.00%
sqlite u32_u64_str unique_0 64 76209 76209 0.00% 77179 77179 0.00%

callgrind: serialize_product_value

count format total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
64 json 47182 47182 0.00% 49800 49800 0.00%
64 bsatn 25716 25716 0.00% 27994 27994 0.00%
16 json 12078 12078 0.00% 13948 13948 0.00%
16 bsatn 8117 8117 0.00% 9477 9477 0.00%

callgrind: update bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 1024 20570847 20573396 -0.01% 21393455 21396034 -0.01%
stdb_raw u32_u64_str unique_0 64 128 1296978 1296980 -0.00% 1384330 1384304 0.00%
sqlite u32_u64_str unique_0 1024 1024 1802006 1802006 0.00% 1810976 1810976 0.00%
sqlite u32_u64_str unique_0 64 128 128358 128370 -0.01% 131016 131036 -0.02%
On-disk benchmarks

callgrind: empty transaction

db total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw 5390 5390 0.00% 5428 5428 0.00%
sqlite 5613 5613 0.00% 6157 6157 0.00%

callgrind: filter

db schema indices count preload _column data_type total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str no_index 64 128 2 string 117924 117924 0.00% 118574 118574 0.00%
stdb_raw u32_u64_str no_index 64 128 1 u64 75384 75384 0.00% 75894 75894 0.00%
stdb_raw u32_u64_str btree_each_column 64 128 2 string 22622 22621 0.00% 23128 23127 0.00%
stdb_raw u32_u64_str btree_each_column 64 128 1 u64 21588 21588 0.00% 22054 22054 0.00%
sqlite u32_u64_str no_index 64 128 2 string 146598 146598 0.00% 148426 148426 0.00%
sqlite u32_u64_str no_index 64 128 1 u64 125947 125947 0.00% 127495 127495 0.00%
sqlite u32_u64_str btree_each_column 64 128 2 string 136598 136598 0.00% 138784 138784 0.00%
sqlite u32_u64_str btree_each_column 64 128 1 u64 133439 133439 0.00% 135357 135353 0.00%

callgrind: insert bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 64 128 848119 847848 0.03% 870261 869994 0.03%
stdb_raw u32_u64_str btree_each_column 64 128 996921 997144 -0.02% 1062817 1063004 -0.02%
sqlite u32_u64_str unique_0 64 128 415829 415829 0.00% 431839 431835 0.00%
sqlite u32_u64_str btree_each_column 64 128 1021870 1021870 0.00% 1061170 1061178 -0.00%

callgrind: iterate

db schema indices count total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 152686 152686 0.00% 152812 152812 0.00%
stdb_raw u32_u64_str unique_0 64 15711 15711 0.00% 15837 15837 0.00%
sqlite u32_u64_str unique_0 1024 1071291 1071291 0.00% 1075017 1075017 0.00%
sqlite u32_u64_str unique_0 64 77981 77981 0.00% 79295 79295 0.00%

callgrind: serialize_product_value

count format total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
64 json 47182 47182 0.00% 49800 49800 0.00%
64 bsatn 25716 25716 0.00% 27994 27994 0.00%
16 json 12078 12078 0.00% 13948 13948 0.00%
16 bsatn 8117 8117 0.00% 9477 9477 0.00%

callgrind: update bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 1024 19297185 19297708 -0.00% 20178181 20179154 -0.00%
stdb_raw u32_u64_str unique_0 64 128 1253172 1253128 0.00% 1340304 1340176 0.01%
sqlite u32_u64_str unique_0 1024 1024 1809567 1809567 0.00% 1818225 1818225 0.00%
sqlite u32_u64_str unique_0 64 128 132478 132478 0.00% 135432 135432 0.00%

Please sign in to comment.