Skip to content

Commit

Permalink
Identity command improvements (#11)
Browse files Browse the repository at this point in the history
* Working on improving commands that use identities

* Fix lints

* Reverted file that shouldn't have changed

* Found and fixed all other todos

* Addressed more CLI TODOs

* Fixes for formatting issues

* Set names of identities

* Set name of identities + clippy

* Small fix

* Added the start of a doc comment, switching over to another PR

* Fixed tests that needed to be updated

* Addressed more feedback and fixed several clippy issues

* Small fix

* Apply suggestions from code review

Co-authored-by: Mazdak Farrokhzad <twingoow@gmail.com>
Signed-off-by: John Detter <4099508+jdetter@users.noreply.github.com>

* Added more doc comments

* Addressing more feedback

* Fixed really old bug in SpacetimeDB

* Tests to verify new functionality

* Fix clippy lints

* Email during identity creation is optional

* Fix output so testsuite passes

---------

Signed-off-by: John Detter <4099508+jdetter@users.noreply.github.com>
Co-authored-by: Boppy <no-reply@boppygames.gg>
Co-authored-by: Mazdak Farrokhzad <twingoow@gmail.com>
  • Loading branch information
3 people authored and cloutiertyler committed Aug 1, 2023
1 parent aab4170 commit 8cf9ed4
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 141 deletions.
77 changes: 77 additions & 0 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::util::is_hex_identity;
use serde::{Deserialize, Serialize};
use std::{
fs,
Expand All @@ -21,6 +22,7 @@ pub struct RawConfig {
identity_configs: Option<Vec<IdentityConfig>>,
}

#[derive(Clone)]
pub struct Config {
proj: RawConfig,
home: RawConfig,
Expand Down Expand Up @@ -83,6 +85,31 @@ impl Config {
self.home.default_identity = Some(default_identity);
}

/// Sets the `nickname` for the provided `identity`.
///
/// If the `identity` already has a `nickname` set, it will be overwritten and returned. If the
/// `identity` is not found, an error will be returned.
///
/// # Returns
/// * `Ok(Option<String>)` - If the identity was found, the old nickname will be returned.
/// * `Err(anyhow::Error)` - If the identity was not found.
pub fn set_identity_nickname(&mut self, identity: &str, nickname: &str) -> Result<Option<String>, anyhow::Error> {
match &mut self.home.identity_configs {
None => {
panic!("Identity {} not found", identity);
}
Some(ref mut configs) => {
let config = configs
.iter_mut()
.find(|c| c.identity == identity)
.ok_or_else(|| anyhow::anyhow!("Identity {} not found", identity))?;
let old_nickname = config.nickname.clone();
config.nickname = Some(nickname.to_string());
Ok(old_nickname)
}
}
}

pub fn default_address(&self) -> Option<&str> {
self.proj
.default_identity
Expand Down Expand Up @@ -224,6 +251,49 @@ impl Config {
self.identity_configs_mut().iter_mut().find(|c| c.identity == identity)
}

/// Converts some given `identity_or_name` into an identity.
///
/// If `identity_or_name` is `None` then `None` is returned. If `identity_or_name` is `Some`,
/// then if its an identity then its just returned. If its not an identity it is assumed to be
/// a name and it is looked up as an identity nickname. If the identity exists it is returned,
/// otherwise we panic.
pub fn resolve_name_to_identity(&self, identity_or_name: Option<&str>) -> Option<String> {
identity_or_name
.map(|identity_or_name| {
if is_hex_identity(identity_or_name) {
&self
.identity_configs()
.iter()
.find(|c| c.identity == *identity_or_name)
.unwrap_or_else(|| panic!("No such identity: {}", identity_or_name))
.identity
} else {
&self
.identity_configs()
.iter()
.find(|c| c.nickname == Some(identity_or_name.to_string()))
.unwrap_or_else(|| panic!("No such identity: {}", identity_or_name))
.identity
}
})
.cloned()
}

/// Converts some given `identity_or_name` into a mutable `IdentityConfig`.
///
/// # Returns
/// * `None` - If an identity config with the given `identity_or_name` does not exist.
/// * `Some` - A mutable reference to the `IdentityConfig` with the given `identity_or_name`.
pub fn get_identity_config_mut(&mut self, identity_or_name: &str) -> Option<&mut IdentityConfig> {
if is_hex_identity(identity_or_name) {
self.get_identity_config_by_identity_mut(identity_or_name)
} else {
self.identity_configs_mut()
.iter_mut()
.find(|c| c.nickname.as_deref() == Some(identity_or_name))
}
}

pub fn delete_identity_config_by_name(&mut self, name: &str) -> Option<IdentityConfig> {
let index = self
.home
Expand Down Expand Up @@ -254,6 +324,13 @@ impl Config {
}
}

/// Deletes all stored identity configs. This function does not save the config after removing
/// all configs.
pub fn delete_all_identity_configs(&mut self) {
self.home.identity_configs = Some(vec![]);
self.home.default_identity = None;
}

pub fn update_default_identity(&mut self) {
if let Some(default_identity) = &self.home.default_identity {
if self
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/subcommands/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub fn cli() -> clap::Command {
pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> {
let database = args.get_one::<String>("database").unwrap();

let identity = args.get_one::<String>("identity");
let auth_header = get_auth_header(&mut config, false, identity.map(|x| x.as_str()))
let identity_or_name = args.get_one::<String>("identity");
let auth_header = get_auth_header(&mut config, false, identity_or_name.map(|x| x.as_str()))
.await
.map(|x| x.0);

Expand Down
Loading

0 comments on commit 8cf9ed4

Please sign in to comment.