Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions src/cli/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ pub enum AccountCmd {
#[clap(short, long, default_value_t = false)]
deploy: bool,
},

/// Remove existing account from the local store
#[clap(short_flag = 'r')]
Remove {
#[clap()]
id: String,
},
}

#[derive(Debug, Parser, Clone)]
Expand Down Expand Up @@ -64,6 +71,7 @@ impl AccountCmd {
new_account(template, *deploy)?;
}
AccountCmd::View { id: _ } => todo!(),
AccountCmd::Remove { id } => remove_account(id)?,
}
Ok(())
}
Expand Down Expand Up @@ -167,3 +175,20 @@ fn new_account(template: &Option<AccountTemplate>, deploy: bool) -> Result<(), S

Ok(())
}

// ACCOUNT REMOVE

fn remove_account(id: &str) -> Result<(), String> {
let client = Client::new(ClientConfig::default()).map_err(|err| err.to_string())?;

let without_prefix = id.trim_start_matches("0x");
let account_id: u64 =
u64::from_str_radix(&without_prefix, 16).map_err(|err| err.to_string())?;
client
.store()
.remove_account(account_id)
.map_err(|err| err.to_string())?;

println!("Succesfully removed Account ID: {}", id);
Ok(())
}
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum StoreError {
QueryError(rusqlite::Error),
InputSerializationError(serde_json::Error),
DataDeserializationError(serde_json::Error),
NotFound,
}

impl fmt::Display for StoreError {
Expand All @@ -53,6 +54,7 @@ impl fmt::Display for StoreError {
DataDeserializationError(err) => {
write!(f, "error deserializing data from the store: {err}")
}
NotFound => write!(f, "requested data was not found in the store"),
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ impl Store {
.map(|_| ())
.map_err(StoreError::QueryError)
}

pub fn remove_account(&self, account_id: u64) -> Result<(), StoreError> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's make sure related objects are deleted as well for this command. In the future this can be optional if we see a scenario where it's positive to eliminate only partial data related to an account.

Copy link
Author

Choose a reason for hiding this comment

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

Added in latest commit, I'm unsure what to do with the associated row on the account_code table, should we only remove it if no other account makes use of it?

let q = self
.db
.execute(
"DELETE FROM accounts WHERE id = ?",
Copy link
Collaborator

Choose a reason for hiding this comment

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

If it's simple enough, it would be nice to see a test that tests this function.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, to do this we should merge this branch with cli testing

params![account_id as i64],
)
.map_err(StoreError::QueryError)?;

if q == 0 {
return Err(StoreError::NotFound);
}

Ok(())
}
}

// STORE CONFIG
Expand Down