Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI - Remove unused default_identity stuff #1969

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 0 additions & 76 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub struct ServerConfig {
pub nickname: Option<String>,
pub host: String,
pub protocol: String,
pub default_identity: Option<String>,
pub ecdsa_public_key: Option<String>,
}

Expand All @@ -29,30 +28,12 @@ impl ServerConfig {
format!("{}://{}", self.protocol, self.host)
}

pub fn set_default_identity(&mut self, default_identity: String) {
self.default_identity = Some(default_identity);
// TODO: verify the identity exists and its token conforms to the server's `ecdsa_public_key`
}

pub fn nick_or_host_or_url_is(&self, name: &str) -> bool {
self.nickname.as_deref() == Some(name) || self.host == name || {
let (host, _) = host_or_url_to_host_and_protocol(name);
self.host == host
}
}

fn default_identity(&self) -> anyhow::Result<&str> {
self.default_identity.as_deref().ok_or_else(|| {
let server = self.nick_or_host();
anyhow::anyhow!(
"No default identity for server: {server}
Set the default identity with:
\tspacetime identity set-default -s {server} <identity>
Or initialize a default identity with:
\tspacetime identity init-default -s {server}"
)
})
}
}

#[derive(Default, Deserialize, Serialize, Debug, Clone)]
Expand Down Expand Up @@ -97,14 +78,12 @@ fn hanging_default_server_context(server: &str) -> String {
impl RawConfig {
fn new_with_localhost() -> Self {
let local = ServerConfig {
default_identity: None,
host: "127.0.0.1:3000".to_string(),
protocol: "http".to_string(),
nickname: Some("local".to_string()),
ecdsa_public_key: None,
};
let testnet = ServerConfig {
default_identity: None,
host: "testnet.spacetimedb.com".to_string(),
protocol: "https".to_string(),
nickname: Some("testnet".to_string()),
Expand Down Expand Up @@ -187,7 +166,6 @@ impl RawConfig {
host,
protocol,
ecdsa_public_key,
default_identity: None,
});
Ok(())
}
Expand All @@ -214,33 +192,6 @@ impl RawConfig {
.map(|cfg| cfg.protocol.as_ref())
}

fn default_identity(&self, server: &str) -> anyhow::Result<&str> {
self.find_server(server).and_then(ServerConfig::default_identity)
}

fn default_server_default_identity(&self) -> anyhow::Result<&str> {
self.default_server().and_then(ServerConfig::default_identity)
}

fn set_server_default_identity(&mut self, server: &str, default_identity: String) -> anyhow::Result<()> {
let cfg = self.find_server_mut(server)?;
// TODO: create the server config if it doesn't already exist
// TODO: fetch the server's fingerprint to check if it has changed
cfg.default_identity = Some(default_identity);
Ok(())
}

fn set_default_server_default_identity(&mut self, default_identity: String) -> anyhow::Result<()> {
if let Some(default_server) = &self.default_server {
// Unfortunate clone,
// because `set_server_default_identity` needs a unique ref to `self`.
let def = default_server.to_string();
self.set_server_default_identity(&def, default_identity)
} else {
Err(anyhow::anyhow!(NO_DEFAULT_SERVER_ERROR_MESSAGE))
}
}

fn set_default_server(&mut self, server: &str) -> anyhow::Result<()> {
// Check that such a server exists before setting the default.
self.find_server(server)
Expand Down Expand Up @@ -541,33 +492,6 @@ impl Config {
}
}

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.home.default_identity(host)
} else {
self.home.default_server_default_identity()
}
}

/// Set the default identity for `server` in the home configuration.
///
/// Does not validate that `default_identity` applies to `server`.
///
/// Returns an `Err` if:
/// - `server` is `Some`, but does not refer to any server
/// in the home configuration.
/// - `server` is `None`, but the home configuration
/// does not have a default server.
pub fn set_default_identity(&mut self, default_identity: String, server: Option<&str>) -> anyhow::Result<()> {
if let Some(server) = server {
let (host, _) = host_or_url_to_host_and_protocol(server);
self.home.set_server_default_identity(host, default_identity)
} else {
self.home.set_default_server_default_identity(default_identity)
}
}

pub fn server_configs(&self) -> &[ServerConfig] {
&self.home.server_configs
}
Expand Down
Loading