kanidm_cli/
synch.rs

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