Skip to content

Commit

Permalink
Factorize the initialization of a client in the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
FirelightFlagboy committed Dec 2, 2024
1 parent e0fb3e2 commit 4ce42f1
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 172 deletions.
20 changes: 5 additions & 15 deletions cli/src/commands/device/export_recovery_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,13 @@ crate::clap_parser_with_shared_opts_builder!(
}
);

pub async fn main(args: Args) -> anyhow::Result<()> {
let Args {
output,
device,
config_dir,
password_stdin,
} = args;
log::trace!(
"Exporting recovery device at {} (confdir={}, device={})",
output.display(),
config_dir.display(),
device.as_deref().unwrap_or("N/A")
);
crate::build_main_with_client!(export_recovery_device);

let mut handle = start_spinner("Saving recovery device file".into());
pub async fn export_recovery_device(args: Args, client: &StartedClient) -> anyhow::Result<()> {
let Args { output, .. } = args;
log::trace!("Exporting recovery device at {}", output.display());

let client = load_client(&config_dir, device.clone(), password_stdin).await?;
let mut handle = start_spinner("Saving recovery device file".into());

let now = DateTime::now();
let (passphrase, data) = client
Expand Down
40 changes: 12 additions & 28 deletions cli/src/commands/invite/cancel.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS

use libparsec::{
authenticated_cmds::latest::invite_cancel::{self, InviteCancelRep},
InvitationToken,
};
use anyhow::Context;
use libparsec::InvitationToken;

use crate::utils::*;

Expand All @@ -16,32 +14,18 @@ crate::clap_parser_with_shared_opts_builder!(
}
);

pub async fn main(args: Args) -> anyhow::Result<()> {
let Args {
token,
device,
config_dir,
password_stdin,
} = args;
log::trace!(
"Cancelling invitation (confdir={}, device={})",
config_dir.display(),
device.as_deref().unwrap_or("N/A")
);

let (cmds, _) = load_cmds(&config_dir, device, password_stdin).await?;
let mut handle = start_spinner("Deleting invitation".into());
crate::build_main_with_client!(invite_cancel);

pub async fn invite_cancel(args: Args, client: &StartedClient) -> anyhow::Result<()> {
let Args { token, .. } = args;
log::trace!("Cancelling invitation");

let rep = cmds.send(invite_cancel::Req { token }).await?;
let mut handle = start_spinner("Deleting invitation".into());

match rep {
InviteCancelRep::Ok => (),
rep => {
return Err(anyhow::anyhow!(
"Server error while cancelling invitation: {rep:?}"
));
}
};
client
.cancel_invitation(token)
.await
.context("Server refused to cancel invitation")?;

handle.stop_with_message("Invitation deleted".into());

Expand Down
54 changes: 19 additions & 35 deletions cli/src/commands/invite/device.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS

use libparsec::{
authenticated_cmds::latest::invite_new_device::{self, InviteNewDeviceRep},
InvitationType, ParsecInvitationAddr,
};
use anyhow::Context;
use libparsec::{InvitationType, ParsecInvitationAddr};

use crate::utils::*;

Expand All @@ -12,39 +10,25 @@ crate::clap_parser_with_shared_opts_builder!(
pub struct Args {}
);

pub async fn main(args: Args) -> anyhow::Result<()> {
let Args {
device,
config_dir,
password_stdin,
} = args;
log::trace!(
"Inviting a device (confdir={}, device={})",
config_dir.display(),
device.as_deref().unwrap_or("N/A")
);

let (cmds, device) = load_cmds(&config_dir, device, password_stdin).await?;
crate::build_main_with_client!(invite_device);

pub async fn invite_device(_args: Args, client: &StartedClient) -> anyhow::Result<()> {
log::trace!("Inviting a device");

let mut handle = start_spinner("Creating device invitation".into());

let rep = cmds
.send(invite_new_device::Req { send_email: false })
.await?;

let url = match rep {
InviteNewDeviceRep::Ok { token, .. } => ParsecInvitationAddr::new(
device.organization_addr.clone(),
device.organization_id().clone(),
InvitationType::Device,
token,
)
.to_url(),
rep => {
return Err(anyhow::anyhow!(
"Server refused to create device invitation: {rep:?}"
));
}
};
let (token, _email_sent_status) = client
.new_device_invitation(false)
.await
.context("Server refused to create device invitation")?;

let url = ParsecInvitationAddr::new(
client.organization_addr(),
client.organization_id().clone(),
InvitationType::Device,
token,
)
.to_url();

handle.stop_with_message(format!("Invitation URL: {YELLOW}{url}{RESET}"));

Expand Down
47 changes: 16 additions & 31 deletions cli/src/commands/invite/greet.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS

use std::sync::Arc;

use anyhow::Context;
use libparsec::{
authenticated_cmds::latest::invite_list::{self, InviteListItem},
authenticated_cmds::latest::invite_list::InviteListItem,
internal::{
DeviceGreetInProgress1Ctx, DeviceGreetInProgress2Ctx, DeviceGreetInProgress3Ctx,
DeviceGreetInProgress4Ctx, DeviceGreetInitialCtx, EventBus, UserGreetInProgress1Ctx,
DeviceGreetInProgress4Ctx, DeviceGreetInitialCtx, UserGreetInProgress1Ctx,
UserGreetInProgress2Ctx, UserGreetInProgress3Ctx, UserGreetInProgress4Ctx,
UserGreetInitialCtx,
},
AuthenticatedCmds, InvitationToken,
InvitationToken,
};
use libparsec_client::Client;

use crate::utils::*;

Expand All @@ -24,36 +24,26 @@ crate::clap_parser_with_shared_opts_builder!(
}
);

pub async fn main(args: Args) -> anyhow::Result<()> {
let Args {
token,
device,
config_dir,
password_stdin,
} = args;
log::trace!(
"Greeting invitation (confdir={}, device={})",
config_dir.display(),
device.as_deref().unwrap_or("N/A")
);
crate::build_main_with_client!(device_greet);

let (cmds, device) = load_cmds(&config_dir, device, password_stdin).await?;
pub async fn device_greet(args: Args, client: &StartedClient) -> anyhow::Result<()> {
let Args { token, .. } = args;
log::trace!("Greeting invitation");

let invitation = step0(&cmds, token).await?;
let invitation = step0(client, token).await?;

match invitation {
InviteListItem::User { .. } => {
let ctx = UserGreetInitialCtx::new(device, Arc::new(cmds), EventBus::default(), token);
InviteListItem::User { token, .. } => {
let ctx = client.start_user_invitation_greet(token);

let ctx = step1_user(ctx).await?;
let ctx = step2_user(ctx).await?;
let ctx = step3_user(ctx).await?;
let ctx = step4_user(ctx).await?;
step5_user(ctx).await
}
InviteListItem::Device { .. } => {
let ctx =
DeviceGreetInitialCtx::new(device, Arc::new(cmds), EventBus::default(), token);
InviteListItem::Device { token, .. } => {
let ctx = client.start_device_invitation_greet(token);

let ctx = step1_device(ctx).await?;
let ctx = step2_device(ctx).await?;
Expand All @@ -70,17 +60,12 @@ pub async fn main(args: Args) -> anyhow::Result<()> {

/// Step 0: retrieve info
async fn step0(
cmds: &AuthenticatedCmds,
client: &Client,
invitation_token: InvitationToken,
) -> anyhow::Result<InviteListItem> {
let mut handle = start_spinner("Retrieving invitation info".into());

let rep = cmds.send(invite_list::Req).await?;

let invitations = match rep {
invite_list::InviteListRep::Ok { invitations } => invitations,
rep => return Err(anyhow::anyhow!("Server error: {rep:?}")),
};
let invitations = client.list_invitations().await.context("Server error")?;

let invitation = match invitations.into_iter().find(|invitation| match invitation {
InviteListItem::User { token, .. } if *token == invitation_token => true,
Expand Down
16 changes: 4 additions & 12 deletions cli/src/commands/invite/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ crate::clap_parser_with_shared_opts_builder!(
pub struct Args {}
);

pub async fn main(args: Args) -> anyhow::Result<()> {
let Args {
device,
config_dir,
password_stdin,
} = args;
log::trace!(
"Listing invitations (confdir={}, device={})",
config_dir.display(),
device.as_deref().unwrap_or("N/A")
);
crate::build_main_with_client!(list_invite);

pub async fn list_invite(_args: Args, client: &StartedClient) -> anyhow::Result<()> {
log::trace!("Listing invitations");

let client = load_client(&config_dir, device, password_stdin).await?;
let mut handle = start_spinner("Listing invitations".into());

let invitations = client.list_invitations().await?;
Expand Down
18 changes: 5 additions & 13 deletions cli/src/commands/invite/shared_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,13 @@ crate::clap_parser_with_shared_opts_builder!(
}
);

pub async fn main(args: Args) -> anyhow::Result<()> {
crate::build_main_with_client!(invite_shared_recovery);

pub async fn invite_shared_recovery(args: Args, client: &StartedClient) -> anyhow::Result<()> {
let Args {
email,
send_email,
device,
config_dir,
password_stdin,
email, send_email, ..
} = args;
log::trace!(
"Inviting an user to perform a shared recovery (confdir={}, device={})",
config_dir.display(),
device.as_deref().unwrap_or("N/A")
);

let client = load_client(&config_dir, device, password_stdin).await?;
log::trace!("Inviting an user to perform a shared recovery");

{
let _spinner = start_spinner("Poll server for new certificates".into());
Expand Down
56 changes: 19 additions & 37 deletions cli/src/commands/invite/user.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS

use libparsec::{
authenticated_cmds::latest::invite_new_user::{self, InviteNewUserRep},
InvitationType, ParsecInvitationAddr,
};
use anyhow::Context;
use libparsec::{InvitationType, ParsecInvitationAddr};

use crate::utils::*;

Expand All @@ -19,44 +17,28 @@ crate::clap_parser_with_shared_opts_builder!(
}
);

pub async fn main(args: Args) -> anyhow::Result<()> {
crate::build_main_with_client!(invite_user);

pub async fn invite_user(args: Args, client: &StartedClient) -> anyhow::Result<()> {
let Args {
email,
send_email,
device,
config_dir,
password_stdin,
email, send_email, ..
} = args;
log::trace!(
"Inviting an user (confdir={}, device={})",
config_dir.display(),
device.as_deref().unwrap_or("N/A")
);
log::trace!("Inviting an user");

let (cmds, device) = load_cmds(&config_dir, device, password_stdin).await?;
let mut handle = start_spinner("Creating user invitation".into());

let rep = cmds
.send(invite_new_user::Req {
claimer_email: email,
send_email,
})
.await?;

let url = match rep {
InviteNewUserRep::Ok { token, .. } => ParsecInvitationAddr::new(
device.organization_addr.clone(),
device.organization_id().clone(),
InvitationType::Device,
token,
)
.to_url(),
rep => {
return Err(anyhow::anyhow!(
"Server refused to create user invitation: {rep:?}"
));
}
};
let (token, _sent_email_status) = client
.new_user_invitation(email, send_email)
.await
.context("Server refused to create user invitation")?;

let url = ParsecInvitationAddr::new(
client.organization_addr().clone(),
client.organization_id().clone(),
InvitationType::Device,
token,
)
.to_url();

handle.stop_with_message(format!("Invitation URL: {YELLOW}{url}{RESET}"));

Expand Down
2 changes: 1 addition & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod commands;
pub mod macro_opts;
pub mod macros;

#[cfg(test)]
#[path = "../tests/integration/mod.rs"]
Expand Down
Loading

0 comments on commit 4ce42f1

Please sign in to comment.