Skip to content

Commit

Permalink
chore: more logging for the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Oct 16, 2024
1 parent a1a7cc8 commit a89e68c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions autonomi-cli/src/actions/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ pub async fn connect_to_network(peers: Vec<Multiaddr>) -> Result<Client> {

match Client::connect(&peers).await {
Ok(client) => {
info!("Connected to the Network");
progress_bar.finish_with_message("Connected to the Network");
Ok(client)
}
Err(e) => {
error!("Failed to connect to the network: {e}");
progress_bar.finish_with_message("Failed to connect to the network");
bail!("Failed to connect to the network: {e}")
}
Expand Down
2 changes: 2 additions & 0 deletions autonomi-cli/src/actions/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ pub async fn download(addr: &str, dest_path: &str, client: &mut Client) -> Resul
progress_bar.finish_and_clear();

if all_errs.is_empty() {
info!("Successfully downloaded data at: {addr}");
println!("Successfully downloaded data at: {addr}");
Ok(())
} else {
let err_no = all_errs.len();
eprintln!("{err_no} errors while downloading data at: {addr}");
eprintln!("{all_errs:#?}");
error!("Errors while downloading data at {addr}: {all_errs:#?}");
Err(eyre!("Errors while downloading data"))
}
}
5 changes: 5 additions & 0 deletions autonomi-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ pub async fn cost(file: &str, peers: Vec<Multiaddr>) -> Result<()> {
let client = crate::actions::connect_to_network(peers).await?;

println!("Getting upload cost...");
info!("Calculating cost for file: {file}");
let cost = client
.file_cost(&PathBuf::from(file))
.await
.wrap_err("Failed to calculate cost for file")?;

println!("Estimate cost to upload file: {file}");
println!("Total cost: {cost}");
info!("Total cost: {cost} for file: {file}");
Ok(())
}
pub async fn upload(file: &str, peers: Vec<Multiaddr>) -> Result<()> {
Expand All @@ -33,6 +35,7 @@ pub async fn upload(file: &str, peers: Vec<Multiaddr>) -> Result<()> {
let (upload_summary_thread, upload_completed_tx) = collect_upload_summary(event_receiver);

println!("Uploading data to network...");
info!("Uploading file: {file}");

let xor_name = client
.dir_upload(PathBuf::from(file), &wallet)
Expand All @@ -42,6 +45,7 @@ pub async fn upload(file: &str, peers: Vec<Multiaddr>) -> Result<()> {

println!("Successfully uploaded: {file}");
println!("At address: {addr}");
info!("Successfully uploaded: {file} at address: {addr}");
if let Ok(()) = upload_completed_tx.send(()) {
let summary = upload_summary_thread.await?;
if summary.record_count == 0 {
Expand All @@ -50,6 +54,7 @@ pub async fn upload(file: &str, peers: Vec<Multiaddr>) -> Result<()> {
println!("Number of chunks uploaded: {}", summary.record_count);
println!("Total cost: {} AttoTokens", summary.tokens_spent);
}
info!("Summary for upload of file {file} at {addr:?}: {summary:?}");
}

Ok(())
Expand Down
16 changes: 15 additions & 1 deletion autonomi-cli/src/commands/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn generate_key(overwrite: bool) -> Result<()> {
// check if the key already exists
let key_path = crate::keys::get_register_signing_key_path()?;
if key_path.exists() && !overwrite {
error!("Register key already exists at: {key_path:?}");
return Err(eyre!("Register key already exists at: {}", key_path.display()))
.with_suggestion(|| "if you want to overwrite the existing key, run the command with the --overwrite flag")
.with_warning(|| "overwriting the existing key might result in loss of access to any existing registers created using that key");
Expand All @@ -30,6 +31,7 @@ pub fn generate_key(overwrite: bool) -> Result<()> {
let key = RegisterSecretKey::random();
let path = crate::keys::create_register_signing_key_file(key)
.wrap_err("Failed to create new register key")?;
info!("Created new register key at: {path:?}");
println!("✅ Created new register key at: {}", path.display());
Ok(())
}
Expand All @@ -43,6 +45,7 @@ pub async fn cost(name: &str, peers: Vec<Multiaddr>) -> Result<()> {
.register_cost(name.to_string(), register_key)
.await
.wrap_err("Failed to get cost for register")?;
info!("Estimated cost to create a register with name {name}: {cost}");
println!("✅ The estimated cost to create a register with name {name} is: {cost}");
Ok(())
}
Expand All @@ -56,8 +59,10 @@ pub async fn create(name: &str, value: &str, public: bool, peers: Vec<Multiaddr>
let (upload_summary_thread, upload_completed_tx) = collect_upload_summary(event_receiver);

println!("Creating register with name: {name}");
info!("Creating register with name: {name}");
let register = if public {
println!("With public write access");
info!("With public write access");
let permissions = RegisterPermissions::new_anyone_can_write();
client
.register_create_with_permissions(
Expand All @@ -71,6 +76,7 @@ pub async fn create(name: &str, value: &str, public: bool, peers: Vec<Multiaddr>
.wrap_err("Failed to create register")?
} else {
println!("With private write access");
info!("With private write access");
client
.register_create(
value.as_bytes().to_vec().into(),
Expand All @@ -87,6 +93,7 @@ pub async fn create(name: &str, value: &str, public: bool, peers: Vec<Multiaddr>
println!("✅ Register created at address: {address}");
println!("With name: {name}");
println!("And initial value: [{value}]");
info!("✅ Register created at address: {address} with name: {name}");

if let Ok(()) = upload_completed_tx.send(()) {
let summary = upload_summary_thread.await?;
Expand All @@ -95,6 +102,7 @@ pub async fn create(name: &str, value: &str, public: bool, peers: Vec<Multiaddr>
} else {
println!("Total cost: {} AttoTokens", summary.tokens_spent);
}
info!("Summary of register creation: {summary:?}");
}

Ok(())
Expand All @@ -116,20 +124,24 @@ pub async fn edit(address: String, name: bool, value: &str, peers: Vec<Multiaddr
};

println!("Getting register at address: {address}");
info!("Getting register at address: {address}");
let register = client
.register_get(address)
.await
.wrap_err(format!("Failed to get register at address: {address}"))?;
println!("Found register at address: {address}");

println!("Found register at address: {address}");
println!("Updating register with new value: {value}");
info!("Updating register at address: {address} with new value: {value}");

client
.register_update(register, value.as_bytes().to_vec().into(), register_key)
.await
.wrap_err(format!("Failed to update register at address: {address}"))?;

println!("✅ Successfully updated register");
println!("With value: [{value}]");
info!("Successfully updated register at address: {address}");

Ok(())
}
Expand All @@ -150,13 +162,15 @@ pub async fn get(address: String, name: bool, peers: Vec<Multiaddr>) -> Result<(
};

println!("Getting register at address: {address}");
info!("Getting register at address: {address}");
let register = client
.register_get(address)
.await
.wrap_err(format!("Failed to get register at address: {address}"))?;
let values = register.values();

println!("✅ Register found at address: {address}");
info!("Register found at address: {address}");
match values.as_slice() {
[one] => println!("With value: [{:?}]", String::from_utf8_lossy(one)),
_ => {
Expand Down
2 changes: 2 additions & 0 deletions autonomi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ async fn handle_event_receiver(
}

/// Events that can be broadcasted by the client.
#[derive(Debug, Clone)]
pub enum ClientEvent {
UploadComplete(UploadSummary),
}

/// Summary of an upload operation.
#[derive(Debug, Clone)]
pub struct UploadSummary {
pub record_count: usize,
pub tokens_spent: Amount,
Expand Down

0 comments on commit a89e68c

Please sign in to comment.