Skip to content

Commit

Permalink
Create function to setup rpc client
Browse files Browse the repository at this point in the history
  • Loading branch information
tripledoublev committed Nov 28, 2024
1 parent 380199e commit 612cc9d
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ enum Commands {
Start,
}

async fn setup_rpc_client(
base_dir: &std::path::Path,
backend_url: &str,
) -> anyhow::Result<RpcClient> {
let (veilid_api, _update_rx) = init_veilid(base_dir, "save-dweb-backup".to_string()).await?;
RpcClient::from_veilid(veilid_api, backend_url).await
}


#[tokio::main]
async fn main() -> anyhow::Result<()> {
let matches = Command::new("Save DWeb Backend")
Expand Down Expand Up @@ -85,14 +94,11 @@ async fn main() -> anyhow::Result<()> {
anyhow!("Error: --backend-url is required for the 'join' command")
})?;

let (veilid_api, _update_rx) =
init_veilid(&base_dir, "save-dweb-backup".to_string()).await?;

let group_url = sub_matches.get_one::<String>("group_url").unwrap();
println!("Joining group: {}", group_url);

let rpc_client = setup_rpc_client(&base_dir, backend_url).await?;

let rpc_client =
RpcClient::from_veilid(veilid_api.clone(), backend_url.as_str()).await?;
rpc_client.join_group(group_url.to_string()).await?;
println!("Successfully joined group.");
}
Expand All @@ -101,12 +107,10 @@ async fn main() -> anyhow::Result<()> {
anyhow!("Error: --backend-url is required for the 'list' command")
})?;

let (veilid_api, _update_rx) =
init_veilid(&base_dir, "save-dweb-backup".to_string()).await?;

println!("Listing all groups...");
let rpc_client =
RpcClient::from_veilid(veilid_api.clone(), backend_url.as_str()).await?;

let rpc_client = setup_rpc_client(&base_dir, backend_url).await?;

let response = rpc_client.list_groups().await?;
for group_id in response.group_ids {
println!("Group ID: {}", group_id);
Expand All @@ -117,14 +121,11 @@ async fn main() -> anyhow::Result<()> {
anyhow!("Error: --backend-url is required for the 'remove' command")
})?;

let (veilid_api, _update_rx) =
init_veilid(&base_dir, "save-dweb-backup".to_string()).await?;

let group_id = sub_matches.get_one::<String>("group_id").unwrap();
println!("Removing group: {}", group_id);
let rpc_client =
RpcClient::from_veilid(veilid_api.clone(), backend_url.as_str()).await?;

let rpc_client = setup_rpc_client(&base_dir, backend_url).await?;

rpc_client.remove_group(group_id.to_string()).await?;
println!("Successfully removed group.");
}
Expand Down

0 comments on commit 612cc9d

Please sign in to comment.