Skip to content

Commit

Permalink
keys: tell the user where a key is saved
Browse files Browse the repository at this point in the history
Close #1810
  • Loading branch information
leighmcculloch committed Dec 19, 2024
1 parent e5454e6 commit de5dc13
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
13 changes: 9 additions & 4 deletions cmd/soroban-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use clap::command;

use crate::config::{locator, secret};
use crate::{
config::{locator, secret},
print::Print,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -26,8 +29,10 @@ pub struct Cmd {

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
Ok(self
.config_locator
.write_identity(&self.name, &self.secrets.read_secret()?)?)
let print = Print::new(false);
let secret = self.secrets.read_secret()?;
let path = self.config_locator.write_identity(&self.name, &secret)?;
print.checkln(format!("Key saved with alias {:?} in {path:?}", self.name));
Ok(())
}
}
6 changes: 3 additions & 3 deletions cmd/soroban-cli/src/commands/network/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub struct Cmd {

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
Ok(self
.config_locator
.write_network(&self.name, &self.network)?)
self.config_locator
.write_network(&self.name, &self.network)?;
Ok(())
}
}
12 changes: 8 additions & 4 deletions cmd/soroban-cli/src/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ impl Args {
)
}

pub fn write_identity(&self, name: &str, secret: &Secret) -> Result<(), Error> {
pub fn write_identity(&self, name: &str, secret: &Secret) -> Result<PathBuf, Error> {
KeyType::Identity.write(name, secret, &self.config_dir()?)
}

pub fn write_network(&self, name: &str, network: &Network) -> Result<(), Error> {
pub fn write_network(&self, name: &str, network: &Network) -> Result<PathBuf, Error> {
KeyType::Network.write(name, network, &self.config_dir()?)
}

Expand Down Expand Up @@ -441,10 +441,14 @@ impl KeyType {
key: &str,
value: &T,
pwd: &Path,
) -> Result<(), Error> {
) -> Result<PathBuf, Error> {
let filepath = ensure_directory(self.path(pwd, key))?;
let data = toml::to_string(value).map_err(|_| Error::ConfigSerialization)?;
std::fs::write(&filepath, data).map_err(|error| Error::IdCreationFailed { filepath, error })
std::fs::write(&filepath, data).map_err(|error| Error::IdCreationFailed {
filepath: filepath.clone(),
error,
})?;
Ok(filepath)
}

fn root(&self, pwd: &Path) -> PathBuf {
Expand Down

0 comments on commit de5dc13

Please sign in to comment.