1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::common::OpType;
use crate::{handle_client_error, SynchOpt};
use dialoguer::Confirm;

impl SynchOpt {
    pub fn debug(&self) -> bool {
        match self {
            SynchOpt::List(copt) => copt.debug,
            SynchOpt::Get(nopt) => nopt.copt.debug,
            SynchOpt::Create { copt, .. }
            | SynchOpt::GenerateToken { copt, .. }
            | SynchOpt::DestroyToken { copt, .. }
            | SynchOpt::ForceRefresh { copt, .. }
            | SynchOpt::Finalise { copt, .. }
            | SynchOpt::Terminate { copt, .. }
            | SynchOpt::SetYieldAttributes { copt, .. }
            | SynchOpt::SetCredentialPortal { copt, .. } => copt.debug,
        }
    }

    pub async fn exec(&self) {
        match self {
            SynchOpt::List(copt) => {
                let client = copt.to_client(OpType::Read).await;
                match client.idm_sync_account_list().await {
                    Ok(r) => r.iter().for_each(|ent| println!("{}", ent)),

                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            SynchOpt::Get(nopt) => {
                let client = nopt.copt.to_client(OpType::Read).await;
                match client.idm_sync_account_get(nopt.name.as_str()).await {
                    Ok(Some(e)) => println!("{}", e),
                    Ok(None) => println!("No matching entries"),
                    Err(e) => handle_client_error(e, nopt.copt.output_mode),
                }
            }
            SynchOpt::SetCredentialPortal {
                account_id,
                copt,
                url,
            } => {
                let client = copt.to_client(OpType::Write).await;
                match client
                    .idm_sync_account_set_credential_portal(account_id, url.as_ref())
                    .await
                {
                    Ok(()) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            SynchOpt::Create {
                account_id,
                copt,
                description,
            } => {
                let client = copt.to_client(OpType::Write).await;
                match client
                    .idm_sync_account_create(account_id, description.as_deref())
                    .await
                {
                    Ok(()) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            SynchOpt::GenerateToken {
                account_id,
                label,
                copt,
            } => {
                let client = copt.to_client(OpType::Write).await;
                match client
                    .idm_sync_account_generate_token(account_id, label)
                    .await
                {
                    Ok(token) => println!("token: {}", token),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            SynchOpt::DestroyToken { account_id, copt } => {
                let client = copt.to_client(OpType::Write).await;
                match client.idm_sync_account_destroy_token(account_id).await {
                    Ok(()) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            SynchOpt::SetYieldAttributes {
                account_id,
                copt,
                attrs,
            } => {
                let client = copt.to_client(OpType::Write).await;
                match client
                    .idm_sync_account_set_yield_attributes(account_id, attrs)
                    .await
                {
                    Ok(()) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            SynchOpt::ForceRefresh { account_id, copt } => {
                let client = copt.to_client(OpType::Write).await;
                match client.idm_sync_account_force_refresh(account_id).await {
                    Ok(()) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            SynchOpt::Finalise { account_id, copt } => {
                if !Confirm::new()
                    .default(false)
                    .with_prompt("Do you want to continue? This operation can NOT be undone.")
                    .interact()
                    .expect("Failed to get a valid response!")
                {
                    info!("No changes were made");
                    return;
                }

                let client = copt.to_client(OpType::Write).await;
                match client.idm_sync_account_finalise(account_id).await {
                    Ok(()) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
            SynchOpt::Terminate { account_id, copt } => {
                if !Confirm::new()
                    .default(false)
                    .with_prompt("Do you want to continue? This operation can NOT be undone.")
                    .interact()
                    .expect("Failed to get a valid response!")
                {
                    info!("No changes were made");
                    return;
                }

                let client = copt.to_client(OpType::Write).await;
                match client.idm_sync_account_terminate(account_id).await {
                    Ok(()) => println!("Success"),
                    Err(e) => handle_client_error(e, copt.output_mode),
                }
            }
        }
    }
}