1use crate::{handle_client_error, KanidmClientParser, SynchOpt};
2use dialoguer::Confirm;
3use kanidm_proto::cli::OpType;
4
5impl SynchOpt {
6 pub async fn exec(&self, opt: KanidmClientParser) {
7 match self {
8 SynchOpt::List => {
9 let client = opt.to_client(OpType::Read).await;
10 match client.idm_sync_account_list().await {
11 Ok(r) => r.iter().for_each(|ent| println!("{ent}")),
12
13 Err(e) => handle_client_error(e, opt.output_mode),
14 }
15 }
16 SynchOpt::Get(nopt) => {
17 let client = opt.to_client(OpType::Read).await;
18 match client.idm_sync_account_get(nopt.name.as_str()).await {
19 Ok(Some(e)) => println!("{e}"),
20 Ok(None) => println!("No matching entries"),
21 Err(e) => handle_client_error(e, opt.output_mode),
22 }
23 }
24 SynchOpt::SetCredentialPortal { account_id, url } => {
25 let client = opt.to_client(OpType::Write).await;
26 match client
27 .idm_sync_account_set_credential_portal(account_id, url.as_ref())
28 .await
29 {
30 Ok(()) => println!("Success"),
31 Err(e) => handle_client_error(e, opt.output_mode),
32 }
33 }
34 SynchOpt::Create {
35 account_id,
36 description,
37 } => {
38 let client = opt.to_client(OpType::Write).await;
39 match client
40 .idm_sync_account_create(account_id, description.as_deref())
41 .await
42 {
43 Ok(()) => println!("Success"),
44 Err(e) => handle_client_error(e, opt.output_mode),
45 }
46 }
47 SynchOpt::GenerateToken { account_id, label } => {
48 let client = opt.to_client(OpType::Write).await;
49 match client
50 .idm_sync_account_generate_token(account_id, label)
51 .await
52 {
53 Ok(token) => println!("token: {token}"),
54 Err(e) => handle_client_error(e, opt.output_mode),
55 }
56 }
57 SynchOpt::DestroyToken { account_id } => {
58 let client = opt.to_client(OpType::Write).await;
59 match client.idm_sync_account_destroy_token(account_id).await {
60 Ok(()) => opt.output_mode.print_message("Success"),
61 Err(e) => handle_client_error(e, opt.output_mode),
62 }
63 }
64 SynchOpt::SetYieldAttributes { account_id, attrs } => {
65 let client = opt.to_client(OpType::Write).await;
66 match client
67 .idm_sync_account_set_yield_attributes(account_id, attrs)
68 .await
69 {
70 Ok(()) => opt.output_mode.print_message("Success"),
71 Err(e) => handle_client_error(e, opt.output_mode),
72 }
73 }
74 SynchOpt::ForceRefresh { account_id } => {
75 let client = opt.to_client(OpType::Write).await;
76 match client.idm_sync_account_force_refresh(account_id).await {
77 Ok(()) => opt.output_mode.print_message("Success"),
78 Err(e) => handle_client_error(e, opt.output_mode),
79 }
80 }
81 SynchOpt::Finalise { account_id } => {
82 if !Confirm::new()
83 .default(false)
84 .with_prompt("Do you want to continue? This operation can NOT be undone.")
85 .interact()
86 .expect("Failed to get a valid response!")
87 {
88 opt.output_mode.print_message("No changes were made");
89 return;
90 }
91
92 let client = opt.to_client(OpType::Write).await;
93 match client.idm_sync_account_finalise(account_id).await {
94 Ok(()) => opt.output_mode.print_message("Success"),
95 Err(e) => handle_client_error(e, opt.output_mode),
96 }
97 }
98 SynchOpt::Terminate { account_id } => {
99 if !Confirm::new()
100 .default(false)
101 .with_prompt("Do you want to continue? This operation can NOT be undone.")
102 .interact()
103 .expect("Failed to get a valid response!")
104 {
105 opt.output_mode.print_message("No changes were made");
106 return;
107 }
108
109 let client = opt.to_client(OpType::Write).await;
110 match client.idm_sync_account_terminate(account_id).await {
111 Ok(()) => opt.output_mode.print_message("Success"),
112 Err(e) => handle_client_error(e, opt.output_mode),
113 }
114 }
115 }
116 }
117}