-
Notifications
You must be signed in to change notification settings - Fork 245
Add delete account feature to remove an account from the accounts file. #743
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b276560
Add feature delete account from accounts file.
devnet0x 5157707
Add feature to delete account from account file.
devnet0x 65cda98
Add delete command feature to remove accounts from accounts file.
devnet0x 9c9fe87
Apply suggestions from code review
devnet0x a36c4a1
Merge branch 'master' into delete-account
devnet0x ded467c
Resolve commented issues from original PR (add profile remove, user c…
devnet0x 56fac41
Merge branch 'delete-account'
devnet0x 1a9297e
Merge branch 'foundry-rs:master' into master
devnet0x 6bfe6b4
Solve second round of conversations (mainly fix delete-profile behavi…
devnet0x f0c736d
Update docs/src/starknet/account.md
devnet0x 8f1f977
Update docs/src/starknet/account.md
devnet0x fa67062
Update crates/cast/src/main.rs
devnet0x c4e0253
Update crates/cast/src/starknet_commands/account/delete.rs
devnet0x 4f56220
Update crates/cast/src/starknet_commands/account/delete.rs
devnet0x b56379b
Update crates/cast/src/starknet_commands/account/delete.rs
devnet0x 43b18dc
Merge branch 'foundry-rs:master' into master
devnet0x e896f79
Change delete-profile arg to bool and move scarb result message to re…
devnet0x 7d2989d
Merge branch 'master' into master
drknzz eefb4d3
Add section in docs and fix a lint recommendation
devnet0x 0de23d0
Merge branch 'foundry-rs:master' into master
devnet0x e9006e8
Merge branch 'master' of github.com:devnet0x/starknet-foundry
devnet0x 07d4802
Update crates/cast/src/starknet_commands/account/delete.rs
devnet0x 0bcec42
Merge branch 'foundry-rs:master' into master
devnet0x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| use anyhow::{anyhow, bail, Context, Result}; | ||
| use camino::Utf8PathBuf; | ||
| use cast::helpers::response_structs::AccountDeleteResponse; | ||
| use cast::helpers::scarb_utils::get_scarb_manifest; | ||
| use clap::Args; | ||
| use promptly::prompt; | ||
| use serde_json::Map; | ||
| use std::fs::File; | ||
| use std::io::Read; | ||
| use std::io::Write; | ||
|
|
||
| #[derive(Args, Debug)] | ||
| #[command(about = "Delete account information from the accounts file")] | ||
| pub struct Delete { | ||
| /// Name of the account to be deleted | ||
| #[clap(short, long)] | ||
| pub name: Option<String>, | ||
devnet0x marked this conversation as resolved.
Show resolved
Hide resolved
devnet0x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// If passed with false, Scarb profile won't be removed | ||
| #[clap(long, num_args = 1, default_value = "true")] | ||
| pub delete_profile: Option<bool>, | ||
|
|
||
| /// Network where the account exists; defaults to network of rpc node | ||
| #[clap(long)] | ||
| pub network: Option<String>, | ||
| } | ||
|
|
||
| #[allow(clippy::too_many_arguments)] | ||
| pub fn delete( | ||
| name: &str, | ||
| path: &Utf8PathBuf, | ||
| path_to_scarb_toml: &Option<Utf8PathBuf>, | ||
| delete_profile: Option<bool>, | ||
| network_name: &str, | ||
| ) -> Result<AccountDeleteResponse> { | ||
| let contents = std::fs::read_to_string(path.clone()).context("Couldn't read accounts file")?; | ||
| let items: serde_json::Value = serde_json::from_str(&contents) | ||
| .map_err(|_| anyhow!("Failed to parse accounts file at {path}"))?; | ||
|
|
||
| if items[&network_name].is_null() { | ||
| bail!("No accounts defined for network {}", network_name); | ||
| } | ||
| if items[&network_name][&name].is_null() { | ||
| bail!("Account with name {name} does not exist") | ||
| } | ||
|
|
||
| let mut items: Map<String, serde_json::Value> = | ||
| serde_json::from_str(&contents).expect("failed to read file { path }"); | ||
|
|
||
| // Let's ask confirmation | ||
| let prompt_text = | ||
| format!("Do you want to remove the account {name} deployed to network {network_name} from local file {path}? (Y/n)"); | ||
| let input: String = prompt(prompt_text)?; | ||
|
|
||
| if !input.starts_with('Y') { | ||
| bail!("Delete aborted"); | ||
| } | ||
|
|
||
| // get to the nested object "nested" | ||
| let nested = items | ||
| .get_mut(network_name) | ||
| .expect("can't find network") | ||
| .as_object_mut() | ||
| .expect("invalid network format"); | ||
|
|
||
| // now remove the child from there | ||
| nested.remove(name); | ||
devnet0x marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| std::fs::write(path.clone(), serde_json::to_string_pretty(&items).unwrap())?; | ||
|
|
||
| let mut scarb_result = "Account not removed from Scarb.toml".to_string(); | ||
| // delete profile if delete_profile is true or not passed | ||
| if delete_profile == Some(true) { | ||
| let manifest_path = match path_to_scarb_toml.clone() { | ||
| Some(path) => path, | ||
| None => get_scarb_manifest().context("Failed to obtain manifest path from scarb")?, | ||
| }; | ||
| let mut toml_content = String::new(); | ||
| let mut file = File::open(manifest_path.clone()).expect("Failed to open file"); | ||
| file.read_to_string(&mut toml_content) | ||
| .expect("Failed to read file"); | ||
|
|
||
| // Parse the TOML content | ||
| let mut parsed_toml: toml::Value = | ||
| toml::de::from_str(&toml_content).expect("Failed to parse TOML"); | ||
|
|
||
| // Remove the nested section from the Value object. | ||
| if let Some(table) = parsed_toml | ||
| .get_mut("tool") | ||
| .and_then(toml::Value::as_table_mut) | ||
| { | ||
| if let Some(nested_table) = table.get_mut("sncast").and_then(toml::Value::as_table_mut) | ||
| { | ||
| if nested_table.remove(name).is_some() { | ||
| scarb_result = "Account removed from Scarb.toml".to_string(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Serialize the modified TOML data back to a string | ||
| let modified_toml = toml::to_string(&parsed_toml).expect("Failed to serialize TOML"); | ||
|
|
||
| // Write the modified content back to the file | ||
| let mut file = File::create(manifest_path).expect("Failed to create file"); | ||
| file.write_all(modified_toml.as_bytes()) | ||
| .expect("Failed to write to file"); | ||
| }; | ||
|
|
||
| let result = "Account successfully removed".to_string(); | ||
| Ok(AccountDeleteResponse { | ||
| result, | ||
| scarb_result, | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.