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
use crate::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, .. } => copt.debug,
        }
    }

    pub async fn exec(&self) {
        match self {
            SynchOpt::List(copt) => {
                let client = copt.to_client().await;
                match client.idm_sync_account_list().await {
                    Ok(r) => r.iter().for_each(|ent| println!("{}", ent)),
                    Err(e) => error!("Error -> {:?}", e),
                }
            }
            SynchOpt::Get(nopt) => {
                let client = nopt.copt.to_client().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) => error!("Error -> {:?}", e),
                }
            }
            SynchOpt::Create {
                account_id,
                copt,
                description,
            } => {
                let client = copt.to_client().await;
                match client
                    .idm_sync_account_create(account_id, description.as_deref())
                    .await
                {
                    Ok(()) => println!("Success"),
                    Err(e) => error!("Error -> {:?}", e),
                }
            }
            SynchOpt::GenerateToken {
                account_id,
                label,
                copt,
            } => {
                let client = copt.to_client().await;
                match client
                    .idm_sync_account_generate_token(account_id, label)
                    .await
                {
                    Ok(token) => println!("token: {}", token),
                    Err(e) => error!("Error -> {:?}", e),
                }
            }
            SynchOpt::DestroyToken { account_id, copt } => {
                let client = copt.to_client().await;
                match client.idm_sync_account_destroy_token(account_id).await {
                    Ok(()) => println!("Success"),
                    Err(e) => error!("Error -> {:?}", e),
                }
            }
            SynchOpt::ForceRefresh { account_id, copt } => {
                let client = copt.to_client().await;
                match client.idm_sync_account_force_refresh(account_id).await {
                    Ok(()) => println!("Success"),
                    Err(e) => error!("Error -> {:?}", e),
                }
            }
            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().await;
                match client.idm_sync_account_finalise(account_id).await {
                    Ok(()) => println!("Success"),
                    Err(e) => error!("Error -> {:?}", e),
                }
            }
            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().await;
                match client.idm_sync_account_terminate(account_id).await {
                    Ok(()) => println!("Success"),
                    Err(e) => error!("Error -> {:?}", e),
                }
            }
        }
    }
}