Skip to content

Commit

Permalink
Refactor error handling across CLI commands (#94)
Browse files Browse the repository at this point in the history
Co-authored-by: Ammar Arif <[email protected]>
  • Loading branch information
steebchen and kariy authored Sep 18, 2024
1 parent c5af3f6 commit a5290ab
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 285 deletions.
14 changes: 3 additions & 11 deletions cli/src/command/auth/info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use clap::Args;
use slot::graphql::auth::{me::*, Me};
use slot::graphql::{GraphQLQuery, Response};
use slot::graphql::GraphQLQuery;
use slot::{api::Client, credential::Credentials};

#[derive(Debug, Args)]
Expand All @@ -14,16 +14,8 @@ impl InfoArgs {
let client = Client::new_with_token(credentials.access_token);

let request_body = Me::build_query(Variables {});
let res: Response<ResponseData> = client.query(&request_body).await?;

if let Some(errors) = res.errors {
for err in errors {
println!("Error: {}", err.message);
}
return Ok(());
}

print!("{:?}", res.data.unwrap());
let res: ResponseData = client.query(&request_body).await?;
print!("{:?}", res);

Ok(())
}
Expand Down
11 changes: 2 additions & 9 deletions cli/src/command/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,9 @@ async fn handler(

// fetch the account information
let request_body = Me::build_query(Variables {});
let res: graphql_client::Response<ResponseData> = api.query(&request_body).await?;
let data: ResponseData = api.query(&request_body).await?;

// display the errors if any, but still process bcs we have the token
if let Some(errors) = res.errors {
for err in errors {
eprintln!("Error: {}", err.message);
}
}

let account = res.data.and_then(|data| data.me).expect("missing payload");
let account = data.me.expect("missing payload");
let account = Account::try_from(account)?;

// 3. Store the access token locally
Expand Down
109 changes: 49 additions & 60 deletions cli/src/command/deployments/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use katana_primitives::genesis::Genesis;
use katana_primitives::FieldElement;
use slot::graphql::deployments::katana_accounts::KatanaAccountsDeploymentConfig::KatanaConfig;
use slot::graphql::deployments::{katana_accounts::*, KatanaAccounts};
use slot::graphql::{GraphQLQuery, Response};
use slot::graphql::GraphQLQuery;
use starknet::core::types::Felt;

use slot::api::Client;
Expand All @@ -38,68 +38,57 @@ impl AccountsArgs {
let user = Credentials::load()?;
let client = Client::new_with_token(user.access_token);

let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {
println!("Error: {}", err.message);
}
}

if let Some(data) = res.data {
if let Some(deployment) = data.deployment {
if let KatanaConfig(config) = deployment.config {
match config.accounts {
Some(accounts) => {
let mut accounts_vec = Vec::new();
for account in accounts {
// TODO(kariy): update these after updating katana-primitives
let address = ContractAddress::from(
FieldElement::from_bytes_be(
&Felt::from_hex(&account.address).unwrap().to_bytes_be(),
)
.unwrap(),
);

let public_key = FieldElement::from_bytes_be(
&Felt::from_hex(&account.public_key).unwrap().to_bytes_be(),
)
.unwrap();
let private_key = FieldElement::from_bytes_be(
&Felt::from_hex(&account.private_key).unwrap().to_bytes_be(),
let data: ResponseData = client.query(&request_body).await?;

if let Some(deployment) = data.deployment {
if let KatanaConfig(config) = deployment.config {
match config.accounts {
Some(accounts) => {
let mut accounts_vec = Vec::new();
for account in accounts {
// TODO(kariy): update these after updating katana-primitives
let address = ContractAddress::from(
FieldElement::from_bytes_be(
&Felt::from_hex(&account.address).unwrap().to_bytes_be(),
)
.unwrap();

let genesis_account = GenesisAccount {
public_key,
..GenesisAccount::default()
};

accounts_vec.push((
address,
GenesisAccountAlloc::DevAccount(DevGenesisAccount {
private_key,
inner: genesis_account,
}),
));
}
print_genesis_accounts(accounts_vec.iter().map(|(a, b)| (a, b)), None);
}
None => {
let accounts = DevAllocationsGenerator::new(10)
.with_seed(parse_seed(&config.seed))
.generate();

let mut genesis = Genesis::default();
genesis.extend_allocations(
accounts.into_iter().map(|(k, v)| (k, v.into())),
);
print_genesis_accounts(
genesis.accounts().peekable(),
Some(&config.seed),
.unwrap(),
);

let public_key = FieldElement::from_bytes_be(
&Felt::from_hex(&account.public_key).unwrap().to_bytes_be(),
)
.unwrap();
let private_key = FieldElement::from_bytes_be(
&Felt::from_hex(&account.private_key).unwrap().to_bytes_be(),
)
.unwrap();

let genesis_account = GenesisAccount {
public_key,
..GenesisAccount::default()
};

accounts_vec.push((
address,
GenesisAccountAlloc::DevAccount(DevGenesisAccount {
private_key,
inner: genesis_account,
}),
));
}
};
}
print_genesis_accounts(accounts_vec.iter().map(|(a, b)| (a, b)), None);
}
None => {
let accounts = DevAllocationsGenerator::new(10)
.with_seed(parse_seed(&config.seed))
.generate();

let mut genesis = Genesis::default();
genesis
.extend_allocations(accounts.into_iter().map(|(k, v)| (k, v.into())));
print_genesis_accounts(genesis.accounts().peekable(), Some(&config.seed));
}
};
}
}

Expand Down
58 changes: 25 additions & 33 deletions cli/src/command/deployments/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use slot::graphql::deployments::create_deployment::CreateDeploymentCreateDeploym
};
use slot::graphql::deployments::create_deployment::*;
use slot::graphql::deployments::CreateDeployment;
use slot::graphql::{GraphQLQuery, Response};
use slot::graphql::GraphQLQuery;

use super::{services::CreateServiceCommands, Tier};

Expand Down Expand Up @@ -140,40 +140,32 @@ impl CreateArgs {
let user = Credentials::load()?;
let client = Client::new_with_token(user.access_token);

let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {
println!("Error: {}", err.message);
}
let data: ResponseData = client.query(&request_body).await?;

return Ok(());
}
println!("Deployment success 🚀");

if let Some(data) = res.data {
println!("Deployment success 🚀");
match data.create_deployment {
SayaConfig(config) => {
println!("\nConfiguration:");
println!(" RPC URL: {}", config.rpc_url);
}
ToriiConfig(config) => {
println!("\nConfiguration:");
println!(" World: {}", config.world);
println!(" RPC: {}", config.rpc);
println!(" Start Block: {}", config.start_block.unwrap_or(0));
println!(" Index Pending: {}", config.index_pending.unwrap_or(false));
println!("\nEndpoints:");
println!(" GRAPHQL: {}", config.graphql);
println!(" GRPC: {}", config.grpc);
}
KatanaConfig(config) => {
println!("\nEndpoints:");
println!(" RPC: {}", config.rpc);
}
MadaraConfig(config) => {
println!("\nEndpoints:");
println!(" RPC: {}", config.rpc);
}
match data.create_deployment {
SayaConfig(config) => {
println!("\nConfiguration:");
println!(" RPC URL: {}", config.rpc_url);
}
ToriiConfig(config) => {
println!("\nConfiguration:");
println!(" World: {}", config.world);
println!(" RPC: {}", config.rpc);
println!(" Start Block: {}", config.start_block.unwrap_or(0));
println!(" Index Pending: {}", config.index_pending.unwrap_or(false));
println!("\nEndpoints:");
println!(" GRAPHQL: {}", config.graphql);
println!(" GRPC: {}", config.grpc);
}
KatanaConfig(config) => {
println!("\nEndpoints:");
println!(" RPC: {}", config.rpc);
}
MadaraConfig(config) => {
println!("\nEndpoints:");
println!(" RPC: {}", config.rpc);
}
}

Expand Down
13 changes: 3 additions & 10 deletions cli/src/command/deployments/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Args;
use dialoguer::theme::ColorfulTheme;
use dialoguer::Confirm;
use slot::graphql::deployments::{delete_deployment::*, DeleteDeployment};
use slot::graphql::{GraphQLQuery, Response};
use slot::graphql::GraphQLQuery;
use slot::{api::Client, credential::Credentials};

#[derive(clap::ValueEnum, Clone, Debug, serde::Serialize)]
Expand Down Expand Up @@ -61,16 +61,9 @@ impl DeleteArgs {
let user = Credentials::load()?;
let client = Client::new_with_token(user.access_token);

let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {
println!("Error: {}", err.message);
}
}
let _data: ResponseData = client.query(&request_body).await?;

if res.data.is_some() {
println!("Delete success 🚀");
}
println!("Delete success 🚀");

Ok(())
}
Expand Down
81 changes: 37 additions & 44 deletions cli/src/command/deployments/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use slot::graphql::deployments::describe_deployment::DescribeDeploymentDeploymen
KatanaConfig, MadaraConfig, SayaConfig, ToriiConfig,
};
use slot::graphql::deployments::{describe_deployment::*, DescribeDeployment};
use slot::graphql::{GraphQLQuery, Response};
use slot::graphql::GraphQLQuery;
use slot::{api::Client, credential::Credentials};

#[derive(Debug, Args)]
Expand Down Expand Up @@ -37,51 +37,44 @@ impl DescribeArgs {
let user = Credentials::load()?;
let client = Client::new_with_token(user.access_token);

let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {
println!("Error: {}", err.message);
}
}
let data: ResponseData = client.query(&request_body).await?;

if let Some(data) = res.data {
if let Some(deployment) = data.deployment {
println!("Project: {}", deployment.project);
println!(
"Branch: {}",
deployment.branch.unwrap_or_else(|| String::from("Default"))
);
println!("Tier: {:?}", deployment.tier);
if let Some(deployment) = data.deployment {
println!("Project: {}", deployment.project);
println!(
"Branch: {}",
deployment.branch.unwrap_or_else(|| String::from("Default"))
);
println!("Tier: {:?}", deployment.tier);

match deployment.config {
ToriiConfig(config) => {
println!("\nConfiguration:");
println!(" Version: {}", config.version);
println!(" World: {}", config.world);
println!(" RPC: {}", config.rpc);
println!(" Start Block: {}", config.start_block.unwrap_or(0));
println!(
" Indexing Pending: {}",
config.index_pending.unwrap_or(false)
);
println!("\nEndpoints:");
println!(" GraphQL: {}", config.graphql);
println!(" GRPC: {}", config.grpc);
}
KatanaConfig(config) => {
println!("\nEndpoints:");
println!(" Version: {}", config.version);
println!(" RPC: {}", config.rpc);
}
MadaraConfig(config) => {
println!("\nEndpoints:");
println!(" Version: {}", config.version);
println!(" RPC: {}", config.rpc);
}
SayaConfig(config) => {
println!("\nEndpoints:");
println!(" RPC URL: {}", config.rpc_url);
}
match deployment.config {
ToriiConfig(config) => {
println!("\nConfiguration:");
println!(" Version: {}", config.version);
println!(" World: {}", config.world);
println!(" RPC: {}", config.rpc);
println!(" Start Block: {}", config.start_block.unwrap_or(0));
println!(
" Indexing Pending: {}",
config.index_pending.unwrap_or(false)
);
println!("\nEndpoints:");
println!(" GraphQL: {}", config.graphql);
println!(" GRPC: {}", config.grpc);
}
KatanaConfig(config) => {
println!("\nEndpoints:");
println!(" Version: {}", config.version);
println!(" RPC: {}", config.rpc);
}
MadaraConfig(config) => {
println!("\nEndpoints:");
println!(" Version: {}", config.version);
println!(" RPC: {}", config.rpc);
}
SayaConfig(config) => {
println!("\nEndpoints:");
println!(" RPC URL: {}", config.rpc_url);
}
}
}
Expand Down
Loading

0 comments on commit a5290ab

Please sign in to comment.