kanidm_cli/system_config/
api.rs

1use crate::{ApiOpt, KanidmClientParser};
2use std::io::IsTerminal;
3
4impl ApiOpt {
5    pub async fn exec(&self, opt: KanidmClientParser) {
6        match self {
7            ApiOpt::DownloadSchema(aopt) => {
8                let client = opt.to_unauth_client();
9                // check if the output file already exists
10                if aopt.filename.exists() {
11                    debug!("Output file {} already exists", aopt.filename.display());
12                    let mut bail = false;
13                    if !aopt.force {
14                        // check if we're in a terminal
15                        if std::io::stdout().is_terminal()
16                            && std::io::stderr().is_terminal()
17                            && std::io::stdin().is_terminal()
18                        {
19                            // validate with the user that it's OK to overwrite
20                            let response = match dialoguer::Confirm::new()
21                                .with_prompt(format!(
22                                    "Output file {} already exists, overwrite?",
23                                    aopt.filename.display()
24                                ))
25                                .interact()
26                            {
27                                Ok(val) => val,
28                                // if it throws an error just trigger false
29                                Err(err) => {
30                                    eprintln!("Failed to get response from user: {err:?}");
31                                    false
32                                }
33                            };
34                            if !response {
35                                bail = true;
36                            }
37                        } else {
38                            debug!("stdin is not a terminal, bailing!");
39                            bail = true;
40                        }
41                        if bail {
42                            error!("Output file {} already exists and user hasn't forced overwrite, can't continue!", aopt.filename.display());
43                            std::process::exit(1);
44                        }
45                    }
46                }
47                let url = client.make_url("/docs/v1/openapi.json");
48                debug!(
49                    "Downloading schema from {} to {}",
50                    url,
51                    aopt.filename.display()
52                );
53                let jsondata: serde_json::Value =
54                    match client.perform_get_request("/docs/v1/openapi.json").await {
55                        Ok(val) => val,
56                        Err(err) => {
57                            error!("Failed to download: {:?}", err);
58                            std::process::exit(1);
59                        }
60                    };
61                let serialized = match serde_json::to_string_pretty(&jsondata) {
62                    Ok(val) => val,
63                    Err(err) => {
64                        error!("Failed to serialize schema: {:?}", err);
65                        std::process::exit(1);
66                    }
67                };
68
69                match std::fs::write(&aopt.filename, serialized.as_bytes()) {
70                    Ok(_) => {
71                        info!("Wrote schema to {}", aopt.filename.display());
72                    }
73                    Err(err) => {
74                        error!(
75                            "Failed to write schema to {}: {:?}",
76                            aopt.filename.display(),
77                            err
78                        );
79                        std::process::exit(1);
80                    }
81                }
82            }
83        }
84    }
85}