Skip to content
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

respect the --format flag everywhere #20

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions src/commands/pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ pub async fn create_pin(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::er
)
.await?;

println!("Created pin with CID: {}", &pin.spec.cid);
// Replace println with print_object for consistent formatting
print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": format!("Created pin with CID: {}", &pin.spec.cid)
}))?;
Ok(())
}

Expand All @@ -97,7 +101,6 @@ pub async fn create_pin(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::er
/// * `_sub_m` - A reference to the ArgMatches struct containing parsed command-line arguments.
/// This is used to access the CID of the pin to delete and any additional options.
pub async fn delete_pin(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
println!("Deleting a pin");
let pin_cid = _sub_m
.get_one::<String>("cid")
.ok_or("Pin CID is required")?;
Expand All @@ -123,6 +126,10 @@ pub async fn delete_pin(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::er
)
.await?;

println!("Deleted pin with CID: {}", pin_cid);
// Use print_object for consistent formatting
print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": format!("Deleted pin with CID: {}", pin_cid)
}))?;
Ok(())
}
47 changes: 34 additions & 13 deletions src/commands/sudo.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use clap::{Arg, Command, ValueHint};
use gevulot_rs::proto::gevulot::gevulot::{
MsgSudoDeletePin, MsgSudoDeleteTask, MsgSudoDeleteWorker, MsgSudoFreezeAccount,
};

use crate::connect_to_gevulot;

const OK: &str = "ok";

use clap::{Arg, Command, ValueHint};
use crate::{connect_to_gevulot, print_object};

pub fn get_command(chain_args: &[Arg]) -> clap::Command {
Command::new("sudo")
Expand Down Expand Up @@ -83,9 +80,15 @@ pub async fn sudo_delete_pin(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn st
cid: pin_id.clone(),
};
client.sudo.delete_pin(msg).await?;
println!("{}", OK);
print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": "Pin deleted successfully"
}))?;
} else {
println!("Pin ID is required");
print_object(_sub_m, &serde_json::json!({
"status": "error",
"message": "Pin ID is required"
}))?;
}
Ok(())
}
Expand All @@ -107,9 +110,15 @@ pub async fn sudo_delete_worker(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn
id: worker_id.clone(),
};
client.sudo.delete_worker(msg).await?;
println!("{}", OK);
print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": "Worker deleted successfully"
}))?;
} else {
println!("Worker ID is required");
print_object(_sub_m, &serde_json::json!({
"status": "error",
"message": "Worker ID is required"
}))?;
}
Ok(())
}
Expand All @@ -131,9 +140,15 @@ pub async fn sudo_delete_task(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn s
id: task_id.clone(),
};
client.sudo.delete_task(msg).await?;
println!("{}", OK);
print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": "Task deleted successfully"
}))?;
} else {
println!("Task ID is required");
print_object(_sub_m, &serde_json::json!({
"status": "error",
"message": "Task ID is required"
}))?;
}
Ok(())
}
Expand All @@ -155,9 +170,15 @@ pub async fn sudo_freeze_account(_sub_m: &clap::ArgMatches) -> Result<(), Box<dy
account: account.clone(),
};
client.sudo.freeze_account(msg).await?;
println!("{}", OK);
print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": "Account frozen successfully"
}))?;
} else {
println!("Account address is required");
print_object(_sub_m, &serde_json::json!({
"status": "error",
"message": "Account address is required"
}))?;
}
Ok(())
}
11 changes: 9 additions & 2 deletions src/commands/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ pub async fn get_task(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::erro
let task: gevulot_rs::models::Task = task.into();
print_object(_sub_m, &task)?;
} else {
println!("Task ID is required");
print_object(_sub_m, &serde_json::json!({
"status": "error",
"message": "Task ID is required"
}))?;
}
Ok(())
}
Expand Down Expand Up @@ -109,6 +112,10 @@ pub async fn create_task(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::e
)
.await?;

println!("Created task with ID: {}", resp.id);
print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": "Task created successfully",
"task_id": resp.id
}))?;
Ok(())
}
17 changes: 13 additions & 4 deletions src/commands/workers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ pub async fn get_worker(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::er
let worker: gevulot_rs::models::Worker = worker.into();
print_object(_sub_m, &worker)?;
} else {
println!("Worker ID is required");
print_object(_sub_m, &serde_json::json!({
"status": "error",
"message": "Worker ID is required"
}))?;
}
Ok(())
}
Expand Down Expand Up @@ -79,7 +82,11 @@ pub async fn create_worker(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std:
)
.await?;

println!("{}", resp.id);
print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": "Worker created successfully",
"worker_id": resp.id
}))?;
Ok(())
}

Expand All @@ -94,7 +101,6 @@ pub async fn create_worker(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std:
///
/// A Result containing () if successful, or a Box<dyn std::error::Error> if an error occurs.
pub async fn delete_worker(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
println!("Deleting a worker");
let worker_id = _sub_m
.get_one::<String>("id")
.ok_or("Worker ID is required")?;
Expand All @@ -117,6 +123,9 @@ pub async fn delete_worker(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std:
)
.await?;

println!("deleted {}", worker_id);
print_object(_sub_m, &serde_json::json!({
"status": "success",
"message": format!("Worker {} deleted successfully", worker_id)
}))?;
Ok(())
}
65 changes: 56 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,15 @@ async fn send_tokens(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::error
.await
.token_transfer(receiver, amount.parse()?)
.await?;

let output = serde_json::json!({
"success": true,
"amount": amount,
"receiver": receiver
});

print_object(_sub_m, &output)?;

Ok(())
}

Expand Down Expand Up @@ -605,9 +614,14 @@ async fn account_info(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::erro
.await
.get_account_balance(address)
.await?;
println!("Account number: {}", account.account_number);
println!("Account sequence: {}", account.sequence);
println!("Balance: {:#?}", balance.amount);

let output = serde_json::json!({
"account_number": account.account_number,
"sequence": account.sequence,
"balance": balance.amount.to_string()
});

print_object(_sub_m, &output)?;
Ok(())
}

Expand Down Expand Up @@ -641,13 +655,40 @@ async fn generate_key(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::erro
// Get the ECDSA/secp256k1 signing and verification keys for the xprv and xpub
let sk = SigningKey::from_slice(&child_xprv.private_key().to_bytes())?;

println!("{}", sk.public_key().account_id("gvlt").unwrap());
let account_id = sk.public_key().account_id("gvlt").unwrap();
let phrase = mnemonic.phrase();

let output = serde_json::json!({
"account_id": account_id,
"mnemonic": phrase
});

if let Some(file) = _sub_m.get_one::<String>("file") {
let mut file = File::create(file)?;
file.write_all(mnemonic.phrase().as_bytes())?;
} else {
println!("{}", mnemonic.phrase());
file.write_all(phrase.as_bytes())?;
}

match _sub_m.get_one::<String>("format").map(String::as_str) {
Some("json") => {
let json = serde_json::to_string(&output)?;
println!("{}", json);
}
Some("prettyjson") => {
let prettyjson = serde_json::to_string_pretty(&output)?;
println!("{}", prettyjson);
}
Some("toml") => {
let toml = toml::to_string(&output)?;
println!("{}", toml);
}
Some("yaml") => {
let yaml = serde_yaml::to_string(&output)?;
println!("{}", yaml);
}
_ => {
println!("{}", account_id);
println!("{}", phrase);
}
}

Ok(())
Expand Down Expand Up @@ -686,8 +727,10 @@ async fn compute_key(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::error
// Get the ECDSA/secp256k1 signing and verification keys for the xprv and xpub
let sk = SigningKey::from_slice(&child_xprv.private_key().to_bytes())?;

println!("{}", sk.public_key().account_id("gvlt").unwrap());
let account_id = sk.public_key().account_id("gvlt").unwrap();

let output = serde_json::json!({ "account_id": account_id });
print_object(_sub_m, &output)?;
Ok(())
}

Expand Down Expand Up @@ -719,7 +762,11 @@ async fn generate_completion(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn st
Ok(())
}
async fn list_workflows(_sub_m: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
println!("Listing all workflows");
let output = serde_json::json!({
"message": "Listing all workflows",
"status": "not_implemented"
});
print_object(_sub_m, &output)?;
todo!();
}

Expand Down